-
Notifications
You must be signed in to change notification settings - Fork 0
/
expressServer.js
99 lines (89 loc) · 3.58 KB
/
expressServer.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
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
90
91
92
93
94
95
96
97
98
99
const http = require('http');
const fs = require('fs');
const path = require('path');
const swaggerUI = require('swagger-ui-express');
const jsYaml = require('js-yaml');
const express = require('express');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const OpenApiValidator = require('express-openapi-validator');
const logger = require('./logger');
const config = require('./config');
class ExpressServer {
constructor(port, openApiYaml) {
this.port = port;
this.app = express();
this.openApiPath = openApiYaml;
try {
this.schema = jsYaml.safeLoad(fs.readFileSync(openApiYaml));
} catch (e) {
logger.error('failed to start Express Server', e.message);
}
this.setupMiddleware();
}
setupMiddleware() {
this.app.use(cors());
this.app.use(bodyParser.json({ limit: '14MB' }));
this.app.use(express.json());
this.app.use(express.urlencoded({ extended: false }));
this.app.use(cookieParser());
// Leaving these here as they are useful for testing
//Simple test to see that the server is up and responding
//this.app.get('/hello', (req, res) => res.send(`Hello World. path: ${this.openApiPath}`));
//Send the openapi document *AS GENERATED BY THE GENERATOR*
//this.app.get('/openapi', (req, res) => res.sendFile((path.join(__dirname, 'api', 'openapi.yaml'))));
//View the openapi document in a visual interface. Should be able to test from this page
//this.app.use('/api-docs', swaggerUI.serve, swaggerUI.setup(this.schema));
// Install OpenAPIValidator middleware
// NOTES version 5.0.4:
// There is a bug in this version that causes the validation error:
// "reference \"id\" resolves to more than one schema"
// This is caused by the "example" definitions where the id field
// is only populated with example values e.g 'id1' and *should not*
// be expecte to be unique in the file. Whereas, the current version
// of the OpenApiValidator sends the entire dataset to AJV to validate
// and it returns the above error. To get around this I strip out all
// the example data from the YAML file in index.js and generate a
// "openapi.yaml.sans_example" file to use here.
this.app.use(
OpenApiValidator.middleware({
apiSpec: this.openApiPath + ".sans_example",
operationHandlers: path.join(__dirname),
validateResponses: false, // this causes "reference \"id1a\" resolves to more than one schema"
validateApiSpec: true,
validateRequests: true, // this causes "reference \"id1a\" resolves to more than one schema"
validateSecurity: true,
validateFormats: true,
fileUploader: { dest: config.FILE_UPLOAD_PATH },
}),
);
this.app.get('/login-redirect', (req, res) => {
res.status(200);
res.json(req.query);
});
this.app.get('/oauth2-redirect.html', (req, res) => {
res.status(200);
res.json(req.query);
});
// Setup error handler
this.app.use((err, req, res, next) => {
//console.error(err); // dump error to console for debug
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
}
launch() {
http.createServer(this.app).listen(this.port);
logger.info(`Listening on port ${this.port}`);
}
async close() {
if (this.server !== undefined) {
await this.server.close();
logger.info(`Server on port ${this.port} shut down`);
}
}
}
module.exports = ExpressServer;