-
Notifications
You must be signed in to change notification settings - Fork 64
/
app.js
108 lines (95 loc) · 2.39 KB
/
app.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
// This file is part of Pa11y Webservice.
//
// Pa11y Webservice is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pa11y Webservice is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Pa11y Webservice. If not, see <http://www.gnu.org/licenses/>.
'use strict';
const async = require('async');
const Hapi = require('@hapi/hapi');
const {MongoClient} = require('mongodb');
const {dim} = require('kleur');
function initApp(config, callback) {
const app = {
server: new Hapi.Server({
host: config.host,
port: config.port
}),
db: null,
client: null,
model: {},
config
};
const client = new MongoClient(
config.database,
{
useNewUrlParser: true,
useUnifiedTopology: true
}
);
client.on('timeout', () => {
console.log('mongodb: connection timeout');
});
client.on('connect', () => {
console.log(dim('mongodb: connected'));
});
client.on('close', () => {
console.log(dim('mongodb: connection closed'));
});
client.on('reconnect', () => {
console.log(dim('mongodb: connection reestablished'));
});
async.series(
[
next => {
client.connect(error => {
app.client = client;
app.db = client.db();
next(error);
});
},
next => {
require('./model/result')(app, (error, model) => {
app.model.result = model;
next(error);
});
},
next => {
require('./model/task')(app, (error, model) => {
app.model.task = model;
next(error);
});
},
next => {
if (!config.dbOnly && process.env.NODE_ENV !== 'test') {
require('./task/pa11y')(config, app);
}
next();
},
next => {
if (config.dbOnly) {
return next();
}
require('./route/index')(app);
require('./route/tasks')(app);
require('./route/task')(app);
app.server.start()
.then(
() => next(),
error => next(error)
);
console.log(`Server running at: ${app.server.info.uri}`);
}
],
error => callback(error, app)
);
}
module.exports = initApp;