lib/server/index.js   A
last analyzed

Complexity

Total Complexity 10
Complexity/F 1.11

Size

Lines of Code 77
Function Count 9

Duplication

Duplicated Lines 0
Ratio 0 %

Test Coverage

Coverage 3.03%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 77
ccs 1
cts 33
cp 0.0303
crap 0
rs 10
wmc 10
mnd 1
bc 7
fnc 9
bpm 0.7776
cpm 1.1111
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B index.js ➔ ??? 0 37 1
1
"use strict";
2
3
const config = require('config');
4
const Hapi = require('hapi');
5
const Inert = require('inert');
6
const Vision = require('vision');
7
const HapiSwagger = require('hapi-swagger');
8
const taskLogger = require('../logger/taskLogger')('Server');
9
const pkg = require('../../package.json');
10
const routeList = require('./routes');
11
12
const server = new Hapi.Server();
13
const wrapper = {};
14
let serverStarted = false;
15
16
server.connection({
17
    host: config.server.host,
18
    port: config.server.port
19
});
20
21
/**
22
 * @returns {Promise<null|Error>}
23
 */
24
wrapper.start = () => {
25
    taskLogger.starting();
26
27
    routeList.forEach(route => {
28
        server.route(route);
29
    });
30
31
    const options = {
32
        info: {
33
            title: pkg.name,
34
            version: pkg.version
35
        }
36
    };
37
38
    return server.register([
39
        Inert,
40
        Vision,
41
        {
42
            register: HapiSwagger,
43
            options: options
44
        }]
45
    )
46
        .then(() => server.start())
47
        .then(() => {
48
            serverStarted = true;
49
            taskLogger.started();
50
            console.log(`Running at ${server.info.uri}`);
51
        })
52
        .catch(error => {
53
            taskLogger.error('Stopping after an error');
54
            return wrapper.stop()
55
                .then(() => {
56
                    return Promise.reject(error);
57
                })
58
            ;
59
        });
60
};
61
62
/**
63
 * @returns {Promise<null|Error>}
64
 */
65
wrapper.stop = () => {
66 2
    if (serverStarted === false) {
67
        return Promise.resolve(null);
68
    }
69
    taskLogger.stopping();
70
71
    return server.stop()
72
        .then(() => taskLogger.stopped())
73
        .then(() => null)
74
    ;
75
};
76
77
module.exports = wrapper;
78