-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
159 lines (141 loc) · 3.46 KB
/
server.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
const Hapi = require('hapi');
const config = require('config');
const routes = require('hapi-auto-routes');
const mongoose = require('mongoose');
const mongoload = require('mongoload');
const server = new Hapi.Server();
const Pack = require('./package');
const _ = require('lodash');
const Path = require('path');
const Boom = require('boom');
mongoose.connect(config.database.uri);
if (process.env.NODE_ENV !== 'production') {
mongoose.set('debug', true);
}
let db = mongoose.connection;
mongoload.bind(mongoose).load({
pattern: __dirname + '/models/*.js'
});
db.on('error', function () {
console.error('Connection to db failed!');
process.exit(0);
});
db.on('connected', function callback() {
console.log('Connected to db...');
startServer();
});
db.on('disconnected', function (err) {
console.error('Connection teminated to db');
process.exit(0);
});
function startServer() {
server.connection({
host: config.server.host,
port: config.server.port,
routes: {
cors: true
}
});
let requiredHapiPlugins = [
require('hapi-auth-jwt2'),
require('inert'),
require('vision'), {
'register': require('hapi-swagger'),
'options': {
info: {
'title': 'API Documentation',
'contact': {
'name': 'Adil'
},
'version': Pack.version
}
}
}, {
'register': require('good'),
'options': {
reporters: [{
reporter: require('good-console'),
config: {
format: 'MMMM Do YYYY h:mm:ss a',
utc: false,
color: true
},
events: {
response: '*',
request: '*',
log: '*',
error: '*',
info: '*',
db: '*'
}
}, {
reporter: require('good-file'),
events: {
response: '*',
request: '*',
log: '*',
error: '*',
ops: '*',
db: '*'
},
config: {
path: __dirname + '/logs',
rotate: 'daily'
}
}]
}
}
];
const jwtValidate = function (decoded, request, callback) {
let User = mongoose.model('user');
User.findById(decoded._id, function (err, user) {
if (err) {
return callback(null, false);
} else {
if (user && user.isEnabled) {
return callback(null, true);
} else {
return callback(null, false);
}
}
});
};
server.register(requiredHapiPlugins, function (err) {
if (err) {
throw err;
process.exit(0);
} else {
server.auth.strategy('jwt', 'jwt', {
key: config.JWTConfig.secret,
validateFunc: jwtValidate,
verifyOptions: { algorithms: ['HS256'] }
});
server.views({
engines: {
html: require('handlebars')
},
relativeTo: __dirname,
path: 'static'
});
server.route({
method: 'GET',
path: '/{filename*}',
handler: {
directory: {
path: __dirname + '/static',
listing: false,
index: false
}
}
});
routes.bind(server).register({
pattern: __dirname + '/routes/**/*.js'
});
server.start(function () {
console.log('Server started... at: ' + server.info.uri);
});
}
});
}
module.exports = server;