-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
48 lines (42 loc) · 1.4 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
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
const swagUI = require('swagger-ui-dist');
const fs = require('fs');
const genSwagger = require('./genSwagger');
const dbInstance = require('./connections/mysql');
const bootstrap = require('./utils/bootstrap');
const {
port
} = require('./config');
const abs = swagUI.getAbsoluteFSPath();
const _port = process.env.PORT || port;
const app = express();
app.use((req, res, next) => {
console.log('REQ:', req.url);
next();
})
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use('/health', (req, res) => res.status(200).json({
healthy: true,
time: new Date().getTime()
}));
app.use('/swagger.json', express.static('./swagger.json'));
const indexContent = fs.readFileSync(`${abs}/index.html`).toString().replace("https://petstore.swagger.io/v2/swagger.json", `http://localhost:${port}/swagger.json`);
app.get("/index.html", (req, res) => res.send(indexContent));
app.get("/", (req, res) => res.send(indexContent));
app.use(express.static(abs));
const models = require('./models');
const dbModels = models(dbInstance());
global.DB = dbInstance();
genSwagger(dbModels);
require('./routes')(app);
app.get('/health', (req, res) => res.json({
success: true
}));
app.listen(_port, () => console.log(`API ${_port}! \n\n\n`));
module.exports = app; // for testing