-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (59 loc) · 2.13 KB
/
server.js
File metadata and controls
70 lines (59 loc) · 2.13 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
/**
* Workr - Universal Job Queue Platform
*/
const http = require('http');
const { router } = require('./src/api');
const { startWorkers } = require('./src/workers');
const queue = require('./src/queue');
const PORT = process.env.PORT || 4002;
const API_KEY = process.env.WORKR_API_KEY || 'dev-key';
// HTTP Server
const server = http.createServer((req, res) => {
// CORS
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
if (req.method === 'OPTIONS') {
res.writeHead(204);
return res.end();
}
// Auth check (except health, admin, events, docs, proxy)
const url = new URL(req.url, `http://localhost:${PORT}`);
const publicPaths = ['/health', '/admin', '/admin/', '/docs', '/proxy', '/api/events', '/api/jobs', '/api/stats'];
const isPublic = publicPaths.some(p => url.pathname === p || url.pathname.startsWith('/api/jobs/'));
if (!isPublic) {
const auth = req.headers.authorization;
if (!auth || auth !== `Bearer ${API_KEY}`) {
res.writeHead(401, { 'Content-Type': 'application/json' });
return res.end(JSON.stringify({ error: 'Unauthorized' }));
}
}
router(req, res);
});
server.listen(PORT, () => {
console.log('');
console.log('========================================');
console.log(' WORKR - Job Queue Platform');
console.log('========================================');
console.log('');
console.log(` Port: ${PORT}`);
console.log(` API: http://localhost:${PORT}/api/jobs`);
console.log('');
console.log(' Job Types:');
console.log(' - thumbnail (video thumbnail)');
console.log(' - webp (image conversion)');
console.log(' - hls (video transcoding)');
console.log(' - download (file download)');
console.log('');
console.log('========================================');
console.log('');
// Start workers
startWorkers();
});
// Graceful shutdown
process.on('SIGINT', () => {
console.log('\n[workr] Shutting down...');
queue.stop();
process.exit(0);
});
module.exports = server;