-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
192 lines (166 loc) · 6.2 KB
/
index.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict';
const Hapi = require('hapi');
const Sequelize = require('sequelize');
const HSequelize = require('hapi-sequelize');
const Good = require('good');
const Hoek = require('hoek');
const chalk = require('chalk');
const { Line } = require('clui');
const ora = require('ora');
// CLI pretty things
const blankLine = new Line().fill();
const pluginsSpinner = ora('Loading plugins');
const modelsSpinner = ora('Models synchronization');
const fixturesSpinner = ora('Loading fixtures');
const serverSpinner = ora('Starting server');
/**
* Constructor for the StarterKit object.
*/
module.exports = function StarterKit() {
this.config = null;
this.server = null;
this.sequelize = null;
this.initialized = false;
// Default config for the init() method.
const defaultConfigs = {
database: {
modelPaths: ['lib/**/model.js', 'lib/**/models/*.js'],
},
};
/**
* Initializes the Hapi server with the config parameter.
* @param {Object} config - Configuration object for the Hapi server
* @param {string} config.server.host - Hapi server host
* @param {integer} config.server.port - Hapi server port
* @param {string} config.database.name - Database instance name
* @param {string} config.database.credentials.dbName - Database name (connection)
* @param {string} config.database.credentials.user - Database user
* @param {string} config.database.credentials.pass - Database password
* @param {string} config.database.credentials.dialect - Database type (mysql, posqtgres, ...)
* @param {string} config.database.credentials.host - Database connection host
* @param {integer} config.database.credentials.port - Database connection port
* @param {boolean} config.database.syncForce - Whether to reload the database each time the server restarts.
* @param {Array<string>} [config.database.modelPaths] - An array of path from where to load your Sequelize Models.
* Default to ['lib/../model.js', 'lib/../models/*.js']
* @param {Object} config.good - Config object for the Good plugin (logs)
*
* @return {StarterKit} The current object in order to make chain calls.
*/
this.init = config => {
if (!config) {
throw new Error(`The config parameter is mandatory.`);
}
Hoek.assert(this.initialized === false, 'You should call init() only once.');
const server = new Hapi.Server();
const mergedConfig = Object.assign(defaultConfigs, config);
// Server configuration
server.connection({
host: config.server.host,
port: config.server.port,
routes: {
cors: { additionalExposedHeaders: ['Content-Disposition'] },
files: {
relativeTo: __dirname,
},
},
});
// Init the database configurations.
this.sequelize = new Sequelize(
mergedConfig.database.credentials.dbName,
mergedConfig.database.credentials.user,
mergedConfig.database.credentials.pass,
{
dialect: mergedConfig.database.credentials.dialect,
host: mergedConfig.database.credentials.host,
port: mergedConfig.database.credentials.port,
}
);
this.config = mergedConfig;
this.server = server;
this.initialized = true;
return this;
};
/**
* Registers the provided plugins and starts the Hapi server.
* @param {Array} [plugins] - An array of plugins to register in the Hapi server. Default [].
* @param {function} [loadFixtures] - An async function that loads the fixtures (must return a Promise object). Default null.
*
* @return {Promise} A promise chain resulting from the loading of the plugins, the models and the start of the server.
*/
this.start = (plugins = [], loadFixtures = null) => {
Hoek.assert(this.initialized === true, 'init() must be called before start().');
blankLine.output();
pluginsSpinner.start();
// By default, we use the Sequelize plugin to handle database transactions.
const mergedPlugins = [
{
register: HSequelize,
options: {
name: this.config.database.name,
sequelize: this.sequelize,
models: this.config.database.modelPaths,
},
},
// Logger config
{
register: Good,
options: this.config.good,
},
].concat(plugins);
return this.server
.register(mergedPlugins)
.catch(err => {
pluginsSpinner.fail(`Loading plugins: ${err.stack}`);
blankLine.output();
process.exit(4); // eslint-disable-line no-process-exit
})
.then(() => {
pluginsSpinner.succeed();
blankLine.output();
modelsSpinner.start();
const db = this.server.plugins['hapi-sequelize'][this.config.database.name];
// Reload the database when the server is restarted (only in dev mode).
return db.sequelize.sync({ force: this.config.database.syncForce });
})
.catch(err => {
modelsSpinner.fail(`Models synchronization: ${err.stack}`);
blankLine.output();
process.exit(2); // eslint-disable-line no-process-exit
})
.then(() => {
// Success callback for the db.sequelize.sync() function call above.
modelsSpinner.succeed();
blankLine.output();
fixturesSpinner.start();
if (this.config.database.syncForce === true && loadFixtures !== null) {
return loadFixtures();
}
return null;
})
.catch(({ stderr }) => {
fixturesSpinner.fail(`Loading fixtures: ${stderr}`);
process.exit(3); // eslint-disable-line no-process-exit
})
.then(() => {
if (this.config.database.syncForce === true) {
fixturesSpinner.succeed();
} else {
fixturesSpinner.warn();
}
blankLine.output();
serverSpinner.start();
return this.server.start();
})
.then(() => {
serverSpinner.succeed(
chalk`Server started on port : [{yellowBright ${this.config.server.port}}]`
);
blankLine.output();
})
.catch(err => {
serverSpinner.fail(`Started server with errors: ${err.stack}`);
blankLine.output();
process.exit(5); // eslint-disable-line no-process-exit
});
};
};