-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (48 loc) · 1.44 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
/**
* REST server implementation with Node.js / Express and Sequelize ORM.
*
* @author C. Mayer (meggsimum)
*/
import process from 'process';
import express from 'express';
import bodyParser from 'body-parser';
import swaggerUi from 'swagger-ui-express';
import swaggerJsonDoc from 'swagger-jsdoc';
import swaggerGlobalOpts from './swagger/global-opts.js';
import db from './config/db.config.js';
import placesRoute from './route/place.route.js';
import logger from './util/logger.js';
const port = process.env.REST_PORT || 8888;
const initDb = process.env.REST_INIT_DB || false;
// REST server
const app = express();
app.use(bodyParser.json());
placesRoute(app);
// DB / ORM
db.sequelize.sync({ force: initDb }).then(() => {
logger.framed(`Drop and resync with { force: ${initDb} }`);
if (initDb) {
// Init data for 'places' table in database
// TODO check if data init is possible with Sequelize
const Places = db.places;
Places.bulkCreate([
{ name: 'Mutterstadt', lat: 49.433333, lon: 8.35 }
]);
logger.info('Created initial dataset(s)');
}
});
// Swagger
const specs = swaggerJsonDoc(swaggerGlobalOpts);
app.use('/docs', swaggerUi.serve);
app.get(
'/docs',
swaggerUi.setup(specs, {
explorer: false
})
);
// start server and export the instance (for unit tests mainly)
app.listen(port, () =>
logger.info(`REST server listening on port ${port}!`)
);
// export the instance (for unit tests mainly)
export default app;