-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
78 lines (66 loc) · 1.7 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
// Hapi & friends
const Hapi = require('hapi');
const HapiRouter = require('hapi-router');
const HapiSwagger = require('hapi-swaggered');
const HapiSwaggerUi = require('hapi-swaggered-ui');
const Good = require('good');
const Boom = require('boom');
const Inert = require('inert');
const Vision = require('vision');
async function init(environment) {
const { logging, secret, server: options, services, swagger, tracker } = environment;
// Create a server instance.
const server = Hapi.server(options);
// Extend app context.
server.app.secret = secret;
server.app.services = services;
server.app.tracker = tracker;
// Extend Hapi response toolkit and request interfaces.
server.decorate('toolkit', 'error', (message, ...args) => {
throw new Boom(message, ...args);
});
server.decorate('toolkit', 'assert', function assert(value, err, ...args) {
if (!value) this.error(err, ...args);
});
// Register logging plugin.
if (logging) {
await server.register([
{
plugin: Good,
options: logging,
},
]);
}
const swaggerUiOptions = {
title: 'Lizenzgenerator API',
path: '/docs',
};
if (swagger.basePath) {
swaggerUiOptions.basePath = swagger.basePath;
}
// Register router & swagger plugins.
await server.register([
Inert,
Vision,
{
plugin: HapiSwaggerUi,
options: swaggerUiOptions,
},
{
plugin: HapiRouter,
options: {
routes: 'routes/*.js',
ignore: ['routes/*.test.js'],
cwd: __dirname,
},
},
{
plugin: HapiSwagger,
options: swagger,
},
]);
// Prepare for server start.
await server.initialize();
return server;
}
module.exports = init;