-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
125 lines (110 loc) · 4.71 KB
/
app.js
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
#!/usr/bin/env node
/*
Aerostat Beam Engine - Redis-backed highly-scale-able and cloud-fit media beam engine.
Copyright (C) 2019 Streampunk Media Ltd.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
https://www.streampunk.media/ mailto:furnace@streampunk.media
14 Ormiscaig, Aultbea, Achnasheen, IV22 2JJ U.K.
*/
const Koa = require('koa');
// TODO consider swapping out for faster routing middleware
// https://github.com/delvedor/router-benchmark
// Wait for https://www.npmjs.com/package/koa-router-find-my-way to mature?
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
// const Bull = require('bull');
// const producer = new Bull('my-first-queue');
const config = require('./config.js').load();
const routes = require('./lib/routes.js');
const { jobCatcher, closeQueues } = require('./lib/jobCatcher.js');
const Boom = require('boom');
const app = new Koa();
const router = new Router();
app.use(bodyParser());
router
.get('/beams', routes.beamsRoute)
.get('/beams/:fmtSpec', routes.formatRoute) // .json supported
.get('/beams/:fmtSpec/equivalent', routes.equivalentQuery)
.get('/beams/:fmtSpec/equivalent/:relSpec', routes.equivalentQuery)
.get('/beams/:fmtSpec/rendition', routes.renditionQuery)
.get('/beams/:fmtSpec/rendition/:relSpec', routes.renditionQuery)
.get('/beams/:fmtSpec/transformation', routes.transformationQuery)
.get('/beams/:fmtSpec/transformation/:relSpec', routes.transformationQuery)
.get('/beams/:fmtSpec/:streamSpec', routes.streamRoute)
.get('/beams/:fmtSpec/:streamSpec/start', routes.startRedirect)
.get('/beams/:fmtSpec/:streamSpec/(end|latest)', routes.endRedirect)
.get('/beams/:fmtSpec/:streamSpec/packet_:pts(-?\\d+).raw(_0)?', routes.packetDataRoute)
.get('/beams/:fmtSpec/:streamSpec/packet_:pts(-?\\d+)/data', routes.packetDataRoute)
.get('/beams/:fmtSpec/:streamSpec/packet_:pts(-?\\d+)(\\.json)?', routes.packetRoute)
.get('/beams/:fmtSpec/:streamSpec/frame_:pts(-?\\d+)(\\.json)?', routes.frameRoute)
.get('/beams/:fmtSpec/:streamSpec/frame_:pts(-?\\d+).raw(_?):idx(\\d?)', routes.frameDataRoute)
.get('/beams/:fmtSpec/:streamSpec/frame_:pts(-?\\d+)/data(_?):idx(\\d?)', routes.frameDataRoute)
.get('/beams/:fmtSpec/:streamSpec/:mediaSpec', routes.mediaRoute)
.post('/beams', routes.createBeam)
.put('/beams/:fmtSpec', routes.formatUpdate) // TODO
.post('/beams/:fmtSpec/equivalent', routes.createEquivalent)
.post('/beams/:fmtSpec/rendition', routes.createRendition)
.post('/beams/:fmtSpec/transformation', routes.createTransformation)
.delete('/beams/:fmtSpec/equivalent/:relSpec', routes.deleteEquivalent)
.delete('/beams/:fmtSpec/rendition/:relSpec', routes.deleteRendition)
.delete('/beams/:fmtSpec/transformation/:relSpec', routes.deleteTransformation)
.put('/beams/:fmtSpec/:streamSpec/:mediaSpec', routes.mediaUpdate) // .json and .raw(_0) supported
.put('/beams/:fmtSpec/:streamSpec/:mediaSpec/data(_?):idx(\\d?)', routes.dataUpdate);
app
.use(async (ctx, next) => {
try {
await next();
if ((ctx.status === 404) || (ctx.body === undefined)) {
ctx.body = { statusCode: 404, error: 'Not Found',
message: 'Resource not found.' };
ctx.status = 404;
}
} catch (err) {
if (Boom.isBoom(err)) {
ctx.response.headers = err.output.headers;
ctx.body = err.output.payload;
ctx.status = err.output.statusCode;
} else {
ctx.status = 500;
ctx.body = { statusCode: ctx.status, error: 'Internal Server Error',
message: err.message };
}
console.error(err.stack);
}
})
.use(jobCatcher)
.use(router.routes())
.use(router.allowedMethods());
let server = null;
app.closeQueues = closeQueues;
if (!module.parent) {
server = app.listen(config.app.port);
}
app.on('error', (err) => {
console.log(err.stack);
});
if (server) {
server.on('close', () => {
console.log('Closing bull queues.');
closeQueues().then(console.log('Bull queues closed.'));
});
}
function end() {
if (server) {
server.close();
}
}
process.on('SIGINT', end);
process.on('SIGHUP', end);
process.on('SIGTERM', end);
process.on('SIGUSR2', end);
module.exports = app;