-
Notifications
You must be signed in to change notification settings - Fork 15
/
app.js
46 lines (38 loc) · 1.36 KB
/
app.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
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const cacheControl = require('express-cache-controller');
const { createPool, createPoolCluster } = require('mariadb');
const swaggerUi = require('swagger-ui-express');
const config = require('./api/config');
const routes = require('./api/routes');
const swaggerDocument = require('./swagger.json');
swaggerDocument.servers[0].url =
process.env.NODE_ENV === 'development'
? 'https://api.khajana.org/v2/'
: 'https://api.banidb.com/v2';
const app = express();
const port = process.env.NODE_ENV === 'development' ? '3001' : '3000';
// database
if (config.length > 1) {
const dbCluster = createPoolCluster();
config.forEach(dbConfig => dbCluster.add(dbConfig.host, dbConfig));
app.locals.pool = dbCluster.of(/.*?/, 'ORDER');
} else {
app.locals.pool = createPool(config[0]);
}
// app
app.use(cors());
app.use(cacheControl({ maxAge: 21600 }));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use('/v2', routes);
app.use('/v2/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use((req, res) => {
res.cacheControl = { noCache: true };
res.status(404).send({ url: `${req.originalUrl} not found` });
});
app.listen(port, () => {
console.log(`BaniDB API start on port ${port}`);
});
module.exports = app;