-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
63 lines (52 loc) · 1.32 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
const Hapi = require('hapi');
const swagger = require('hapi-swagger');
const inert = require('inert');
const vision = require('vision');
const routes = require('./lib/routes');
const config = require('./config');
const logger = require('./logger');
const boom = require('boom');
const server = new Hapi.Server();
server.connection({ port : process.env.PORT || config.port });
server.route(routes);
server.register([
inert,
vision,
{
register: swagger,
options: {
swaggerUIPath: '/documentation/swaggerui/',
jsonPath: '/documentation/swagger.json',
schemes: [config.protocol],
host: config.host
}
}
]);
server.ext('onRequest', (request, reply) => {
logger.info({
id: request.id,
method: request.method,
path: request.path
});
if (request.path.startsWith('/documentation')) {
return reply.continue();
}
if (require('./api_keys').indexOf(request.query.api_key) === -1) {
return reply(boom.unauthorized('Invalid api_key'));
}
return reply.continue();
});
server.ext('onPreResponse', (request, reply) => {
logger.info({
id: request.id,
statusCode: request.response.statusCode
});
return reply.continue();
});
server.start((err) => {
if (err) {
throw err;
}
console.log(`Server running at: ${server.info.uri}`);
});
module.exports = server;