-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
89 lines (68 loc) · 2.86 KB
/
app.js
File metadata and controls
89 lines (68 loc) · 2.86 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require('express');
const exegesisExpress = require('exegesis-express');
const http = require('http');
const path = require('path');
const exegesisSwaggerUIPlugin = require( 'exegesis-plugin-swagger-ui-express' );
const pjson = require('./package.json');
const morgan = require('morgan');
async function createServer() {
const app = express();
//Log requests to console
app.use(morgan('short', {
// log only 4xx and 5xx responses to console
// skip: function (req, res) { return (res.statusCode < 400 && (req.headers.accept && req.headers.accept.indexOf("html") == -1 )) }
}))
app.use((request, response, next) => {
response.header('Access-Control-Allow-Origin', '*');
response.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With');
response.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
//TODO: Don't cache on debug
response.header('Cache-Control', 'public; max-age: 700');
return next();
});
// See https://github.com/exegesis-js/exegesis/blob/master/docs/Options.md
const options = {
controllers: path.resolve(__dirname, './controllers'),
allErrors: true,
plugins: [
exegesisSwaggerUIPlugin({
// Express app (required)
app: app,
path:"/v3/docs",
// Options to pass to Swagger UI
swaggerUIOptions: {
explorer: true
}
})
]
};
const exegesisMiddleware = await exegesisExpress.middleware(
path.resolve(__dirname, './swagger.json'),
options
);
// If you have any body parsers, this should go before them.
app.use(exegesisMiddleware);
app.use((req, res) => {
res.status(404).json({
es: `Aquí no hay nada. Por favor revisa la documentación: https://qqwapi-elastic.readthedocs.io/es/latest/`,
en: `There's nothing here. Please check the docs: https://qqwapi-elastic.readthedocs.io/es/latest/ (only in spanish)`,
version: pjson.version,
generated: new Date()
});
});
app.use((err, req, res, next) => {
console.error("Internal error",req.route.path, err.meta ? err.meta.meta.request.params :"", err.meta ? err.meta.body ? err.meta.body.error : "" : "", err);
res.status(500).json({"Internal error": err.message});
//If connection is lost to database, kill API process
if (err.name == 'ConnectionError') {
process.exit(-1);
}
});
const server = http.createServer(app);
const port = process.env.PORT || 10010;
const l = server.listen(port, () => {
process.stdout.write(`Listening on http://localhost:${l.address().port}/\n`);
});
}
console.log("QQWAPI",pjson.version);
createServer();