forked from uzysjung/UzysHapiSkeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (51 loc) · 1.94 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
/**
* Created by uzysjung on 17. 12. 13..
*/
'use strict';
const Hapi = require('hapi');
const Config = require('./config');
const server = new Hapi.Server({ port: Config.port, routes: { cors: true , jsonp: 'callback' } });
const main = async () => {
await Promise.all([ require('./src/plugins/inert')(server), require('./src/plugins/vision')(server)]);
await require('./src/plugins/scooter')(server);
await require('./src/plugins/hapi-auth-basic')(server);
await require('./src/plugins/hapi-swagger')(server); //now developing https://github.com/glennjones/hapi-swagger/tree/feature/hapi-17
server.route(require('./src/routes/api'));
//for static file but not recommend due to performance , use nginx.
server.route({ method: 'GET', path: '/public/{path*}', handler: { directory: { path: './public' ,redirectToSlash: true } } });
//for socket.io
const io = require('socket.io')(server.listener);
let cnt = 0;
io.on('connection', function (socket) {
console.log('ok');
socket.emit('who' , { name: 'uzys' });
socket.on('go', function (data) {
socket.emit('come',{ cnt: cnt++ });
console.log('go',data);
});
});
await server.start();
return server;
};
main()
.then( (sv) => {
console.log(['info', 'server'], 'Server environment: ' + Config.type);
console.log(['info', 'server'], 'Server running at: ' + sv.info.uri);
} )
.catch( (err) => {
console.log(['error', 'server'],'Server Error Occured' + err);
console.log('stack - ',err.stack);
process.exit(1);
});
process.on('SIGINT', async () => {
// My process has received a SIGINT signal
// Meaning PM2 is now trying to stop the process
try {
await server.stop({ timeout:1000 });
} catch(e) {
console.error(e);
}
console.log('Hapi server stopped');
process.exit();
});
module.exports = exports = server;