-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
110 lines (91 loc) · 2.52 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
'use strict';
const Hapi = require('hapi');
const HapiSwagger = require('hapi-swagger');
const Models = require('./models');
const Routes = require('./routes');
const Pack = require('./package');
const Fixtures = require('sequelize-fixtures');
var config = {
port: process.env.PORT || '3000'
};
if ('production' !== process.env.NODE_ENV){
config.host = 'localhost';
};
const server = Hapi.server(config);
async function start() {
await Fixtures.loadFile('import.yaml', Models); // import test data
await server.register(require('hapi-auth-jwt2'));
server.auth.strategy('jwt', 'jwt', {
key: Pack.my_app.secret_key, // your secret key, dont share with others
urlKey: false,
cookieKey: false,
verifyOptions: {
algorithms: ['HS256']
},
validate: async function (decoded, request) {
const user = await Models.User.findOne({
where : {
id : decoded.id
}
});
return {
isValid: user != null
};
}
});
server.auth.default('jwt');
for (const i in Routes) {
server.route(Routes[i]);
}
await server.register({
plugin: require('good'),
options: {
ops: {
interval: 1000
},
reporters: {
myConsoleReporter: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{ log: '*', response: '*' }]
}, {
module: 'good-console'
}, 'stdout']
}
}
});
var swaggerOptions = {
info: {
title: 'API Documentation',
version: Pack.version,
},
securityDefinitions: {
'jwt': {
'type': 'apiKey',
'name': 'Authorization',
'in': 'header'
}
},
security: [{ 'jwt': [] }]
};
if ('production' === process.env.NODE_ENV){
swaggerOptions.host = process.env.HEROKU_APP_NAME + '.herokuapp.com';
};
await server.register([
require('inert'),
require('vision'),
{
plugin: HapiSwagger,
options: swaggerOptions
}
]);
try {
await server.start();
}
catch (err) {
console.log(err);
process.exit(1);
}
console.log('Server running at:', server.info.uri);
}
Models.sequelize.sync().then(start);