-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
80 lines (72 loc) · 2.45 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
/*
This is a demo application implementing some interfaces as described in https://docs.google.com/document/d/1337m6i7Y0GPULKLsKpyHR4NRzRwhoxJnAZNnDFCigkc/edit#
This application demonstrates a service which returns previously inserted data from a MongoDB database.
*/
'use strict';
//This is our webserver framework (instead of express)
const hapi = require('hapi'),
co = require('./common');
//Initiate the webserver with standard or given port
const server = new hapi.Server();
let port = (!co.isEmpty(process.env.APPLICATION_PORT)) ? process.env.APPLICATION_PORT : 3000;
server.connection({
port: port
});
let host = (!co.isEmpty(process.env.VIRTUAL_HOST)) ? process.env.VIRTUAL_HOST : server.info.host;
//Export the webserver to be able to use server.log()
module.exports = server;
//Plugin for sweet server console output
let plugins = [
require('inert'),
require('vision'), {
register: require('good'),
options: {
ops: {
interval: 1000
},
reporters: {
console: [{
module: 'good-squeeze',
name: 'Squeeze',
args: [{
log: '*',
response: '*',
request: '*'
}]
}, {
module: 'good-console'
}, 'stdout']
}
}
}, { //Plugin for swagger API documentation
register: require('hapi-swagger'),
options: {
host: host,
info: {
title: 'Tag Service API',
description: 'Powered by node, hapi, joi, hapi-swaggered, hapi-swaggered-ui and swagger-ui',
version: '0.1.0'
}
}
}
];
const createIndexes = require('./database/createIndexes');
//Register plugins and start webserver
server.register(plugins, (err) => {
if (err) {
console.error(err);
global.process.exit();
} else {
// create any indexes before starting the server
createIndexes().catch((err) => {
console.warn('error creating the indexes on the database collection:');
console.warn(err.message);
}).then(() => {
server.start(() => {
server.log('info', 'Server started at ' + server.info.uri);
//Register routes
require('./routes.js')(server);
});
});
}
});