-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServer.js
74 lines (65 loc) · 2.4 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
const Hapi = require('hapi');
const Inert = require('inert');
const Vision = require('vision');
const Auth = require('./auth/Auth');
const PluginLoader = require('./utils/PluginLoader');
const ModuleLoader = require('./utils/ModuleLoader');
const {failActionHandler, queryParser} = require('./utils/Util');
const SqlString = require('sqlstring');
class Server {
constructor(config, databaseConnection) {
this.config = config.getConfig();
this.databaseConnection = databaseConnection;
this.databaseConnection.loadConfig(this.config.DATABASE);
this.server = Hapi.server({
port: this.config.SERVER.PORT,
host: this.config.SERVER.HOST,
routes: {
validate: {
failAction: failActionHandler
}
}
});
}
async registerRoutes() {
new ModuleLoader(this.server).apply();
}
async registerAuth() {
await new Auth(this.server).registerAuth();
}
async registerPlugins() {
await this.server.register([
Inert,
Vision
]);
await new PluginLoader(this.server).register();
}
async decorateServer() {
this.connectionPool = await this.databaseConnection.getConnection();
this.server.decorate('toolkit', 'sql', this.connectionPool);
this.server.decorate('toolkit', 'parse', queryParser(SqlString.escape));
this.server.decorate('toolkit', 'escape', SqlString.escape);
// allows query to be available in handlers on h.sql.query(`select 1+1 as two`)
// console.log(await this.connectionPool.query(queryParser(this.connectionPool.escape)`select 1+1 as two`));
const preResponse = function (request, h) {
const response = request.response;
if (response.isBoom) {
console.log(response);
if (response.code) {
request.response.output.payload.errorCode = response.code;
}
}
return h.continue;
};
this.server.ext('onPreResponse', preResponse);
}
async startServer() {
await this.decorateServer();
await this.registerAuth();
await this.registerRoutes();
await this.registerPlugins();
await this.server.start();
console.info(`Server running at: ${this.server.info.uri}`);
}
}
module.exports = Server;