This repository has been archived by the owner on Dec 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
157 lines (137 loc) · 4.83 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
#!/usr/bin/env node
'use strict';
/* eslint-disable global-require, no-process-exit */
const Sequelize = require('sequelize');
const Hapi = require('hapi');
const chalk = require('chalk');
const { Line } = require('clui');
const ora = require('ora');
const config = require('./config');
config.init(process.argv);
const loadFixtures = require('./fixtures');
const MODELS_SYNC_ERROR = 2;
const LOADING_FIXTURES_ERROR = 3;
const LOADING_PLUGINS_ERROR = 4;
const GLOBAL_ERROR = 5;
const server = new Hapi.Server();
// 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');
module.exports = server;
// ///////////////////////////////////////////////////////////////////////
server.connection({
host: config.get('/server/host'),
port: config.get('/server/port'),
routes: {
cors: true,
files: { relativeTo: __dirname },
},
});
// //////////////////////////////////////////////////////////////////
// //
// Plugins registration //
// //
// //////////////////////////////////////////////////////////////////
blankLine.output();
pluginsSpinner.start();
server
.register([
{
register: require('hapi-sequelize'),
options: {
name: config.get('/database/name'),
sequelize: new Sequelize(
config.get('/database/credentials/dbName'),
config.get('/database/credentials/user'),
config.get('/database/credentials/pass'),
{
dialect: config.get('/database/credentials/dialect'),
host: config.get('/database/credentials/host'),
port: config.get('/database/credentials/port'),
}
),
models: ['lib/**/model.js', 'lib/**/models/*.js'],
forceSync: config.get('/database/syncForce'),
},
},
{
register: require('good'),
options: config.get('/good'),
},
require('inert'),
require('hapi-auth-jwt2'),
require('./lib/auth-jwt'),
require('./lib/route-test'),
// Add a hapi plugin here by adding a line with require('./lib/my-plugin').
// If you want to provide options while registering a plugin you need to use the following synthax instead:
// {
// Register: require('./lib/my-plugin'),
// Options: { myOpt: 'value' }
// }
])
.catch(err => {
pluginsSpinner.fail(`Loading plugins: ${err.stack}`);
blankLine.output();
process.exit(LOADING_PLUGINS_ERROR);
})
// ///////////////////////////////////////////////////////////////////////
// //
// Plugins configuration //
// //
// ///////////////////////////////////////////////////////////////////////
.then(() => {
pluginsSpinner.succeed();
blankLine.output();
modelsSpinner.start();
const db = server.plugins['hapi-sequelize'][config.get('/database/name')];
// Reload the database when the server is restarted (only in dev mode).
return db.sequelize.sync({ force: config.get('/database/syncForce') });
})
.catch(err => {
modelsSpinner.fail(`Models synchronization: ${err.stack}`);
blankLine.output();
process.exit(MODELS_SYNC_ERROR);
})
// //////////////////////////////////////////////////////////////////
// //
// Start the server //
// //
// //////////////////////////////////////////////////////////////////
.then(() => {
// Success callback for the db.sequelize.sync() function call above.
modelsSpinner.succeed();
blankLine.output();
fixturesSpinner.start();
if (config.get('/database/syncForce') === true) {
return loadFixtures();
}
return null;
})
.catch(({ stderr }) => {
fixturesSpinner.fail(`Loading fixtures: ${stderr}`);
process.exit(LOADING_FIXTURES_ERROR);
})
.then(() => {
if (config.get('/database/syncForce') === true) {
fixturesSpinner.succeed();
} else {
fixturesSpinner.warn();
}
blankLine.output();
serverSpinner.start();
return server.start();
})
.then(() => {
serverSpinner.succeed(
chalk`Server started on port : [{yellowBright ${config.get('/server/port')}}]`
);
blankLine.output();
})
.catch(err => {
serverSpinner.fail(`Started server with errors: ${err.stack}`);
blankLine.output();
process.exit(GLOBAL_ERROR);
});