-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
97 lines (87 loc) · 2.64 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
const https = require('https');
const fs = require('fs');
const path = require('path');
const koa = require('koa');
const convert = require('koa-convert');
const bodyParser = require('koa-bodyparser');
const router = require('koa-simple-router');
const error = require('koa-json-error');
const logger = require('koa-logger');
const koaRes = require('koa-res');
const handleError = require('koa-handle-error');
const serve = require('koa-static');
const mount = require('koa-mount');
// const WebSocket = require('ws');
const task = require('./controller/task');
const videoAnalisys = require('./controller/video-analisys');
// const wss = new WebSocket.Server({ port: 8082 });
const port = process.env.PORT || process.env.OPENSHIFT_NODEJS_PORT || 8080;
const ip = process.env.IP || process.env.OPENSHIFT_NODEJS_IP || '0.0.0.0';
const app = new koa();
// error handling
app.use(async (ctx, next) => {
try {
await next();
} catch (err) {
ctx.status = err.status || 500;
ctx.body = err.message;
ctx.app.emit('error', err, ctx);
}
});
app.use(serve('./public'));
// logging
app.use(logger());
// body parsing
app.use(bodyParser({ jsonLimit: '50mb' }));
// format response as JSON
app.use(convert(koaRes()));
// configure router
app.use(
router(_ => {
_.get('/api/v1/dummy/dummyfile.json', async ctx => {
ctx.body = 'hello world';
}),
_.get('/throwerror', async ctx => {
throw new Error('Aghh! An error!');
}),
_.get('/api/v1/biometric-id/sessions.json', videoAnalisys.getSessions),
_.post('/api/v1/biometric-id/save-video', videoAnalisys.saveVideoData),
_.get('*', async (ctx, next) => {
const html = fs.readFileSync(path.resolve('./public/index.html'));
ctx.type = 'html';
ctx.body = html;
});
})
);
// wss.on('connection', ws => {
// console.log('WS Connected on port: 8082');
// ws.on('message', function incoming(message) {
// console.log('received: %s', message);
// });
// });
// var config = {
// domain: 'openshiftapps.com',
// http: {
// port: 8080,
// },
// https: {
// port: 443,
// },
// };
// try {
// const httpsServer = https.createServer(config.https.options, app.callback());
// httpsServer.listen(config.https.port, function(err) {
// if (!!err) {
// console.error('HTTPS server FAIL: ', err, err && err.stack);
// } else {
// console.log(
// `HTTPS server OK: http://${config.domain}:${config.https.port}`
// );
// }
// });
// } catch (ex) {
// console.error('Failed to start HTTPS server\n', ex, ex && ex.stack);
// }
app.listen(port, () => {
console.log('API listening on port 8080');
});