-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
117 lines (91 loc) · 3.81 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
'use strict';
const fs = require('fs'),
path = require('path'),
http = require('http'),
mongoUtils = require('./utils/mongoUtils'),
swaggerUtils = require('./utils/swaggerUtils');
const {TError, TErrorEnum, sendError} = require('./utils/errorUtils');
const app = require('connect')();
const swaggerTools = require('swagger-tools');
const serverPort = 8080;
// Correct the url in swagger-ui-dist that points to some demo (like the petstore)
// And add additional useful options
fs.copyFileSync(path.join(__dirname, './index.html_replacement'),
path.join(__dirname, './node_modules/swagger-ui-dist/index.html'), (err) => {
if(err) {
console.log('Unable to replace swagger-ui-dist/index.html file - something wrong with the installation ??');
process.exit(1);
}
})
// swaggerRouter configuration
const options = {
swaggerUi: path.join(__dirname, '/swagger.json'),
controllers: path.join(__dirname, './controllers'),
useStubs: process.env.NODE_ENV === 'development' // Conditionally turn on stubs (mock mode)
};
const swaggerDoc = swaggerUtils.getSwaggerDoc();
// Initialize the Swagger middleware
swaggerTools.initializeMiddleware(swaggerDoc, function (middleware) {
// Interpret Swagger resources and attach metadata to request - must be first in swagger-tools middleware chain
app.use(middleware.swaggerMetadata());
// app.use(function (req, res, next) {
// res.header("Access-Control-Allow-Origin", "*"); // CORS should be parametrized by configuration
// res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// next();
// });
// Validate Swagger requests
app.use(middleware.swaggerValidator({
validateResponse: false
}));
// Error handling for validation
app.use(errorHandler);
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
// using the more up-to-date swagger-ui-dist - not the default app.use(middleware.swaggerUi())
app.use(middleware.swaggerUi({ swaggerUiDir: path.join(__dirname, 'node_modules', 'swagger-ui-dist') }));
// Start the server
http.createServer(app).listen(serverPort, function () {
console.log('Your server is listening on port %d (http://localhost:%d)', serverPort, serverPort);
console.log('Swagger-ui is available on http://localhost:%d/docs', serverPort);
});
});
// handles timed out requests
function haltOnTimedout(req, res, next) {
if (!req.timedout) {
next();
} else {
debug("\nrequest timed out!\n");
next("the request timed out", null, null, null, 504);
}
}
function errorHandler (err, req, res, next) {
if(err) {
if(err.failedValidation) {
// err is something like
// {"code":"SCHEMA_VALIDATION_FAILED",
// "failedValidation":true,
// "results":{
// "errors":[
// {"code":"INVALID_TYPE",
// "message":"Expected type array but found type object",
// "path":["serviceQualificationItem"]}
// ],
// "warnings":[]},
// "path":["paths","/serviceQualificationManagement/v3/serviceQualification",
// "post","parameters","0"],
// "paramName":"ServiceQualification"}
// rewrite to the TMForum error code format
const message = err.results.errors.map(item => item.message).join(", ");
const error = new TError(TErrorEnum.INVALID_BODY, message);
sendError(res,error);
} else {
// err.status and err.body in case of syntax error in incoming request
// not sending the body back
const error = new TError(TErrorEnum.INVALID_BODY, "Invalid request");
sendError(res,error);
}
} else {
next(err,req,res);
}
};