-
Notifications
You must be signed in to change notification settings - Fork 9
/
app.js
39 lines (29 loc) · 868 Bytes
/
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
const express = require('express');
const cors = require('cors');
const openapi = require('express-openapi');
const expressip = require('express-ip');
const getIP = (req, res, next) => {
const xForwardedFor = (req.headers['x-forwarded-for'] || '')
.replace(/:\d+$/, '');
req.remote_ip = xForwardedFor || req.connection.remoteAddress;
next();
};
const createApp = (api) => {
const app = express();
app.use(express.json());
app.use(cors());
app.use(getIP);
app.use(expressip().getIpInfoMiddleware);
openapi.initialize({
apiDoc: api.apiDoc, // require('./api-doc.js'),
app: app,
docsPath: '/api',
paths: api.paths, // path.resolve(__dirname, 'api-routes'),
dependencies: api.dependencies,
});
app.get('/', (req, res) => {
res.send(api.description || '');
});
return app;
};
exports.createApp = createApp;