-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbootstrap-api-service.sh
More file actions
executable file
·782 lines (687 loc) · 18.7 KB
/
bootstrap-api-service.sh
File metadata and controls
executable file
·782 lines (687 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
#!/bin/bash
# Bootstrap API Service Script
# This script creates the API-only service structure (no frontend)
echo "🚀 Starting API Service Bootstrap..."
# Create directory structure
echo "📁 Creating API directory structure..."
mkdir -p src/api/{routes,middleware,controllers,services,utils}
mkdir -p src/workers
mkdir -p src/shared/types
mkdir -p tests/{api,workers,integration,e2e}
mkdir -p docs/api
# Install API dependencies
echo "📦 Installing API dependencies..."
npm install --save \
express \
cors \
helmet \
compression \
dotenv \
jsonwebtoken \
bcryptjs \
express-rate-limit \
express-validator \
bull \
ioredis \
socket.io \
@sentry/node \
winston \
swagger-ui-express \
swagger-jsdoc
npm install --save-dev \
@types/express \
@types/cors \
@types/compression \
@types/jsonwebtoken \
@types/bcryptjs \
@types/bull \
@types/swagger-ui-express \
@types/swagger-jsdoc \
concurrently \
nodemon
# Create environment template
echo "🔐 Creating environment template..."
cat << 'EOF' > .env.api
# API Configuration
NODE_ENV=development
PORT=4000
API_URL=http://localhost:4000
API_VERSION=v1
# Database (existing Supabase)
DATABASE_URL=your_existing_supabase_url
SUPABASE_URL=your_existing_supabase_url
SUPABASE_ANON_KEY=your_existing_anon_key
SUPABASE_SERVICE_KEY=your_existing_service_key
# Redis Configuration
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_PASSWORD=
# API Key Settings
API_KEY_SALT=your_salt_here_change_in_production
API_KEY_LENGTH=32
# Rate Limiting
RATE_LIMIT_WINDOW_MS=900000
RATE_LIMIT_MAX_REQUESTS=100
RATE_LIMIT_STRICT_WINDOW_MS=60000
RATE_LIMIT_STRICT_MAX_REQUESTS=5
# Worker Configuration
MAX_WORKERS=3
WORKER_CONCURRENCY=5
JOB_TIMEOUT_MS=300000
MAX_BROWSERS=5
# WebSocket Configuration
WS_HEARTBEAT_INTERVAL=30000
WS_MAX_CONNECTIONS=1000
# Monitoring
SENTRY_DSN=
LOG_LEVEL=info
LOG_DIR=./logs
# CORS Settings
CORS_ORIGINS=http://localhost:3000,http://localhost:3001
CORS_CREDENTIALS=true
# Webhook Settings
WEBHOOK_TIMEOUT=10000
WEBHOOK_MAX_RETRIES=3
WEBHOOK_SIGNATURE_SECRET=your_webhook_secret_here
EOF
# Create API server with integrated documentation
echo "🖥️ Creating API server with Swagger documentation..."
cat << 'EOF' > src/api/server.ts
import express from 'express';
import cors from 'cors';
import helmet from 'helmet';
import compression from 'compression';
import { createServer } from 'http';
import { Server } from 'socket.io';
import dotenv from 'dotenv';
import swaggerUi from 'swagger-ui-express';
import swaggerJsdoc from 'swagger-jsdoc';
import { rateLimiter } from './middleware/rateLimiter';
import { errorHandler } from './middleware/errorHandler';
import { apiKeyAuth } from './middleware/apiKeyAuth';
import { logger } from './utils/logger';
// Load environment variables
dotenv.config({ path: '.env.api' });
const app = express();
const httpServer = createServer(app);
const io = new Server(httpServer, {
cors: {
origin: process.env.CORS_ORIGINS?.split(',') || ['*'],
credentials: true
}
});
// Swagger configuration
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'Automation API',
version: '1.0.0',
description: 'API for browser automation and content submission',
},
servers: [
{
url: process.env.API_URL || 'http://localhost:4000',
description: 'Development server',
},
],
components: {
securitySchemes: {
ApiKeyAuth: {
type: 'apiKey',
in: 'header',
name: 'X-API-Key',
},
},
},
},
apis: ['./src/api/routes/*.ts', './src/api/routes/*.js'],
};
const swaggerDocs = swaggerJsdoc(swaggerOptions);
// Global middleware
app.use(helmet());
app.use(cors({
origin: process.env.CORS_ORIGINS?.split(',') || ['*'],
credentials: true
}));
app.use(compression());
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
// API Documentation
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocs));
// Health check
app.get('/health', (req, res) => {
res.json({
status: 'healthy',
timestamp: new Date().toISOString(),
uptime: process.uptime(),
environment: process.env.NODE_ENV,
version: process.env.API_VERSION || 'v1'
});
});
// API version endpoint
app.get('/api', (req, res) => {
res.json({
name: 'Automation API Service',
version: process.env.API_VERSION || 'v1',
documentation: `${process.env.API_URL}/api-docs`,
endpoints: {
health: '/health',
profiles: '/api/v1/profiles',
campaigns: '/api/v1/campaigns',
submissions: '/api/v1/submissions',
webhooks: '/api/v1/webhooks',
analytics: '/api/v1/analytics'
}
});
});
// Apply rate limiting to API routes
app.use('/api/', rateLimiter);
// API routes (to be implemented)
const apiPrefix = '/api/v1';
// app.use(`${apiPrefix}/auth`, require('./routes/auth.routes'));
// app.use(`${apiPrefix}/profiles`, apiKeyAuth, require('./routes/profile.routes'));
// app.use(`${apiPrefix}/campaigns`, apiKeyAuth, require('./routes/campaign.routes'));
// app.use(`${apiPrefix}/submissions`, apiKeyAuth, require('./routes/submission.routes'));
// app.use(`${apiPrefix}/webhooks`, apiKeyAuth, require('./routes/webhook.routes'));
// app.use(`${apiPrefix}/analytics`, apiKeyAuth, require('./routes/analytics.routes'));
// Error handling
app.use(errorHandler);
// Socket.IO connection handling
io.on('connection', (socket) => {
logger.info(`Client connected: ${socket.id}`);
socket.on('authenticate', async (apiKey) => {
// Validate API key
const isValid = await validateApiKey(apiKey);
if (isValid) {
socket.join(`user:${apiKey}`);
socket.emit('authenticated', { success: true });
} else {
socket.emit('authenticated', { success: false });
socket.disconnect();
}
});
socket.on('subscribe-submission', (submissionId) => {
socket.join(`submission:${submissionId}`);
});
socket.on('unsubscribe-submission', (submissionId) => {
socket.leave(`submission:${submissionId}`);
});
socket.on('subscribe-campaign', (campaignId) => {
socket.join(`campaign:${campaignId}`);
});
socket.on('disconnect', () => {
logger.info(`Client disconnected: ${socket.id}`);
});
});
// Export for worker access
export { io };
// Placeholder for API key validation
async function validateApiKey(apiKey: string): Promise<boolean> {
// TODO: Implement API key validation
return true;
}
// Start server
const PORT = process.env.PORT || 4000;
httpServer.listen(PORT, () => {
logger.info(`
🚀 API Server is running!
🔊 Listening on port ${PORT}
📚 API Documentation: http://localhost:${PORT}/api-docs
🏥 Health check: http://localhost:${PORT}/health
📡 API Base: http://localhost:${PORT}/api/v1
🔌 WebSocket ready for connections
`);
});
// Graceful shutdown
process.on('SIGTERM', () => {
logger.info('SIGTERM signal received: closing HTTP server');
httpServer.close(() => {
logger.info('HTTP server closed');
process.exit(0);
});
});
export { app, httpServer };
EOF
# Create API key authentication middleware
echo "🔑 Creating API key authentication..."
cat << 'EOF' > src/api/middleware/apiKeyAuth.ts
import { Request, Response, NextFunction } from 'express';
import { createHash } from 'crypto';
import { logger } from '../utils/logger';
export interface AuthenticatedRequest extends Request {
apiKey?: string;
userId?: string;
permissions?: string[];
}
export const apiKeyAuth = async (
req: AuthenticatedRequest,
res: Response,
next: NextFunction
) => {
const apiKey = req.headers['x-api-key'] as string || req.query.api_key as string;
if (!apiKey) {
return res.status(401).json({
error: 'API key required',
code: 'MISSING_API_KEY'
});
}
try {
// Hash the API key for secure comparison
const hashedKey = createHash('sha256').update(apiKey).digest('hex');
// TODO: Validate API key from database
// const keyData = await db.apiKeys.findOne({ hashedKey });
// For now, accept any key for development
req.apiKey = apiKey;
req.userId = 'dev-user';
req.permissions = ['*'];
// Log API usage
logger.info(`API request: ${req.method} ${req.path} by ${req.apiKey.substring(0, 8)}...`);
next();
} catch (error) {
logger.error('API key validation error:', error);
return res.status(401).json({
error: 'Invalid API key',
code: 'INVALID_API_KEY'
});
}
};
EOF
# Create logger utility
echo "📝 Creating logger utility..."
cat << 'EOF' > src/api/utils/logger.ts
import winston from 'winston';
import path from 'path';
import fs from 'fs';
// Create logs directory if it doesn't exist
const logDir = process.env.LOG_DIR || './logs';
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
const logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json()
),
defaultMeta: { service: 'automation-api' },
transports: [
// Write all logs to console
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
}),
// Write all logs to file
new winston.transports.File({
filename: path.join(logDir, 'combined.log')
}),
// Write errors to separate file
new winston.transports.File({
filename: path.join(logDir, 'error.log'),
level: 'error'
})
]
});
export { logger };
EOF
# Create sample route with Swagger documentation
echo "📋 Creating sample profile route..."
cat << 'EOF' > src/api/routes/profile.routes.ts
import { Router } from 'express';
import { AuthenticatedRequest } from '../middleware/apiKeyAuth';
const router = Router();
/**
* @swagger
* /api/v1/profiles:
* get:
* summary: List all profiles
* tags: [Profiles]
* security:
* - ApiKeyAuth: []
* parameters:
* - in: query
* name: platform
* schema:
* type: string
* enum: [twitter, linkedin, producthunt, hot100ai]
* description: Filter by platform
* - in: query
* name: limit
* schema:
* type: integer
* default: 10
* description: Number of profiles to return
* responses:
* 200:
* description: List of profiles
* 401:
* description: Unauthorized
*/
router.get('/', async (req: AuthenticatedRequest, res) => {
// TODO: Implement profile listing
res.json({
profiles: [],
total: 0,
page: 1,
limit: 10
});
});
/**
* @swagger
* /api/v1/profiles:
* post:
* summary: Create a new profile
* tags: [Profiles]
* security:
* - ApiKeyAuth: []
* requestBody:
* required: true
* content:
* application/json:
* schema:
* type: object
* required:
* - name
* - platform
* properties:
* name:
* type: string
* platform:
* type: string
* enum: [twitter, linkedin, producthunt, hot100ai]
* credentials:
* type: object
* responses:
* 201:
* description: Profile created successfully
* 400:
* description: Invalid request
* 401:
* description: Unauthorized
*/
router.post('/', async (req: AuthenticatedRequest, res) => {
// TODO: Implement profile creation
const { name, platform, credentials } = req.body;
res.status(201).json({
id: 'profile-123',
name,
platform,
createdAt: new Date()
});
});
export default router;
EOF
# Create worker process
echo "👷 Creating worker process..."
cat << 'EOF' > src/workers/automation.worker.ts
import dotenv from 'dotenv';
import { Job } from 'bull';
import Queue from 'bull';
import { logger } from '../api/utils/logger';
import { io } from '../api/server';
dotenv.config({ path: '.env.api' });
// Create queue
const submissionQueue = new Queue('submissions', {
redis: {
host: process.env.REDIS_HOST || 'localhost',
port: parseInt(process.env.REDIS_PORT || '6379'),
password: process.env.REDIS_PASSWORD
}
});
// Process submissions
submissionQueue.process(async (job: Job) => {
const { type, payload } = job.data;
logger.info(`Processing job ${job.id}: ${type}`);
// Send progress update via WebSocket
const updateProgress = (progress: number, message?: string) => {
job.progress(progress);
io.to(`submission:${payload.id}`).emit('submission-update', {
submissionId: payload.id,
progress,
status: 'processing',
message
});
};
try {
updateProgress(10, 'Initializing browser...');
// TODO: Implement actual automation logic
await new Promise(resolve => setTimeout(resolve, 2000));
updateProgress(50, 'Submitting content...');
await new Promise(resolve => setTimeout(resolve, 2000));
updateProgress(90, 'Finalizing...');
await new Promise(resolve => setTimeout(resolve, 1000));
updateProgress(100, 'Completed!');
// Send completion via WebSocket
io.to(`submission:${payload.id}`).emit('submission-update', {
submissionId: payload.id,
progress: 100,
status: 'completed'
});
return { success: true, submissionId: payload.id };
} catch (error) {
logger.error(`Job ${job.id} failed:`, error);
// Send failure via WebSocket
io.to(`submission:${payload.id}`).emit('submission-update', {
submissionId: payload.id,
status: 'failed',
error: error.message
});
throw error;
}
});
logger.info('🚀 Automation worker started');
// Graceful shutdown
process.on('SIGTERM', async () => {
logger.info('Worker shutting down...');
await submissionQueue.close();
process.exit(0);
});
EOF
# Create development script
echo "🔧 Creating development script..."
cat << 'EOF' > start-api.sh
#!/bin/bash
# API Development Script
echo "🚀 Starting API Development Environment..."
# Check if Redis is installed
if ! command -v redis-cli &> /dev/null; then
echo "⚠️ Redis is not installed. Starting with Docker..."
docker run -d --name redis-api -p 6379:6379 redis:7-alpine
else
# Start Redis if not running
if ! pgrep -x redis-server > /dev/null; then
echo "Starting Redis..."
redis-server --daemonize yes
fi
fi
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "Installing dependencies..."
npm install
fi
# Build SDK if it doesn't exist
if [ ! -d "sdk/dist" ]; then
echo "Building SDK..."
cd sdk && npm install && npm run build && cd ..
fi
# Start services
echo "Starting API server and workers..."
npx concurrently -n "API,WORKER" -c "blue,green" \
"npx nodemon --watch src/api --watch src/shared -e ts --exec 'ts-node' src/api/server.ts" \
"npx nodemon --watch src/workers --watch src/shared -e ts --exec 'ts-node' src/workers/automation.worker.ts"
EOF
chmod +x start-api.sh
# Create Docker setup for production
echo "🐳 Creating Docker configuration..."
cat << 'EOF' > Dockerfile.api
FROM node:18-slim
# Install Playwright dependencies
RUN apt-get update && apt-get install -y \
wget \
ca-certificates \
fonts-liberation \
libappindicator3-1 \
libasound2 \
libatk-bridge2.0-0 \
libatk1.0-0 \
libcups2 \
libdbus-1-3 \
libgbm1 \
libgtk-3-0 \
libnspr4 \
libnss3 \
libx11-6 \
libx11-xcb1 \
libxcomposite1 \
libxdamage1 \
libxrandr2 \
lsb-release \
xdg-utils \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# Copy package files
COPY package*.json ./
COPY sdk/package*.json ./sdk/
# Install dependencies
RUN npm ci --production
RUN npx playwright install chromium
# Copy source code
COPY . .
# Build the application
RUN npm run build:api
EXPOSE 4000
CMD ["node", "dist/api/server.js"]
EOF
# Create docker-compose for complete stack
echo "🚢 Creating docker-compose configuration..."
cat << 'EOF' > docker-compose.api.yml
version: '3.8'
services:
redis:
image: redis:7-alpine
ports:
- "6379:6379"
volumes:
- redis_data:/data
restart: unless-stopped
api:
build:
context: .
dockerfile: Dockerfile.api
ports:
- "4000:4000"
environment:
- NODE_ENV=production
- REDIS_HOST=redis
- DATABASE_URL=${DATABASE_URL}
- SUPABASE_URL=${SUPABASE_URL}
- SUPABASE_ANON_KEY=${SUPABASE_ANON_KEY}
depends_on:
- redis
restart: unless-stopped
worker:
build:
context: .
dockerfile: Dockerfile.api
environment:
- NODE_ENV=production
- REDIS_HOST=redis
- DATABASE_URL=${DATABASE_URL}
depends_on:
- redis
- api
command: node dist/workers/automation.worker.js
deploy:
replicas: 3
restart: unless-stopped
volumes:
redis_data:
EOF
# Create Postman collection
echo "📮 Creating Postman collection template..."
cat << 'EOF' > docs/api/postman-collection.json
{
"info": {
"name": "Automation API",
"description": "API collection for the Automation Service",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"auth": {
"type": "apikey",
"apikey": [
{
"key": "key",
"value": "X-API-Key"
},
{
"key": "value",
"value": "{{api_key}}"
}
]
},
"item": [
{
"name": "Health Check",
"request": {
"method": "GET",
"header": [],
"url": "{{base_url}}/health"
}
},
{
"name": "List Profiles",
"request": {
"method": "GET",
"header": [],
"url": "{{base_url}}/api/v1/profiles"
}
}
],
"variable": [
{
"key": "base_url",
"value": "http://localhost:4000"
},
{
"key": "api_key",
"value": "your-api-key"
}
]
}
EOF
echo "✅ API Service Bootstrap Complete!"
echo ""
echo "Next steps:"
echo ""
echo "1. Configure your environment:"
echo " cp .env.api .env"
echo " # Edit .env with your Supabase credentials"
echo ""
echo "2. Start Redis (if not using Docker):"
echo " redis-server"
echo ""
echo "3. Start the API development server:"
echo " ./start-api.sh"
echo ""
echo "4. View API documentation:"
echo " Open http://localhost:4000/api-docs"
echo ""
echo "5. Test the API:"
echo " curl http://localhost:4000/health"
echo ""
echo "6. Build and publish the SDK:"
echo " cd sdk"
echo " npm install"
echo " npm run build"
echo " npm publish"
echo ""
echo "📚 Documentation:"
echo " - API Service Plan: API_SERVICE_AND_SDK_PLAN.md"
echo " - SDK Documentation: sdk/README.md"
echo " - API Documentation: http://localhost:4000/api-docs"
echo ""
echo "🐳 Production deployment:"
echo " docker-compose -f docker-compose.api.yml up -d"