|
|
@@ -1,4 +1,7 @@
|
|
1
|
1
|
import Hapi from '@hapi/hapi'
|
|
|
2
|
+import Sequelize from 'sequelize'
|
|
|
3
|
+
|
|
|
4
|
+import hapiSequelizejs from 'hapi-sequelizejs'
|
|
2
|
5
|
|
|
3
|
6
|
import { routes } from './routes/index.js'
|
|
4
|
7
|
import { plugins } from './plugins/index.js'
|
|
|
@@ -10,11 +13,28 @@ import { config } from '../config/dev.js'
|
|
10
|
13
|
const server = Hapi.server({
|
|
11
|
14
|
port: config.port,
|
|
12
|
15
|
host: config.host,
|
|
13
|
|
- routes: {
|
|
|
16
|
+ routes: {
|
|
14
|
17
|
cors: config.corsSupported
|
|
15
|
18
|
}
|
|
16
|
19
|
})
|
|
17
|
20
|
|
|
|
21
|
+const dbOpts = {
|
|
|
22
|
+ name: config.db, // identifier
|
|
|
23
|
+ models: ['./models/**/*.js'], // paths/globs to model files
|
|
|
24
|
+ ignoredModels: ['./models/**/*.js'], // OPTIONAL: paths/globs to ignore files
|
|
|
25
|
+ sequelize: new Sequelize(
|
|
|
26
|
+ config.db, // db name
|
|
|
27
|
+ 'root', // U
|
|
|
28
|
+ 'root', // P
|
|
|
29
|
+ {
|
|
|
30
|
+ host: config.host,
|
|
|
31
|
+ dialect: config.dbDialect
|
|
|
32
|
+ }
|
|
|
33
|
+ ),
|
|
|
34
|
+ sync: true, // sync models - default false
|
|
|
35
|
+ forceSync: false, // force sync (drops tables) - default false
|
|
|
36
|
+}
|
|
|
37
|
+
|
|
18
|
38
|
const init = async () => {
|
|
19
|
39
|
try {
|
|
20
|
40
|
routes.forEach(route => server.route(route))
|
|
|
@@ -24,10 +44,22 @@ const init = async () => {
|
|
24
|
44
|
routes: { prefix: `/${config.apiBase}` }
|
|
25
|
45
|
})
|
|
26
|
46
|
}
|
|
27
|
|
-
|
|
|
47
|
+
|
|
28
|
48
|
await server.register(docs)
|
|
29
|
49
|
|
|
30
|
|
- await server.start();
|
|
|
50
|
+ /** Register the Sequlize instance as a plugin */
|
|
|
51
|
+ await server.register({ plugin: hapiSequelizejs, options: dbOpts })
|
|
|
52
|
+
|
|
|
53
|
+ /** Uncomment to print the Sequlize instance */
|
|
|
54
|
+ // console.log(server.plugins['hapi-sequelizejs'][config.db])
|
|
|
55
|
+
|
|
|
56
|
+ /** Double-check the database connection */
|
|
|
57
|
+ console.log(`\n[SIIMEE API] Database: ${config.db} | Attempting to connect on port ${config.dbPort}...`)
|
|
|
58
|
+ await server.plugins['hapi-sequelizejs'][config.db].sequelize.authenticate()
|
|
|
59
|
+ console.log(`[SIIMEE API] Database: ${config.db} | Connection has been established on port ${config.dbPort}!`)
|
|
|
60
|
+
|
|
|
61
|
+ /** Lift Off! */
|
|
|
62
|
+ await server.start()
|
|
31
|
63
|
console.log('\n[SIIMEE API] Server running on %s', server.info.uri)
|
|
32
|
64
|
}
|
|
33
|
65
|
|