-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
75 lines (61 loc) · 2.4 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
"use strict";
var fs = require("fs"),
path = require("path"),
http = require("http");
// var oas3Tools = require('oas3-tools');
var app = require("connect")();
var redirect = require('connect-redirection');
var swaggerTools = require("swagger-tools");
var jsyaml = require("js-yaml");
var serverPort = process.env.PORT || 8080;
let serveStatic = require("serve-static");
let { setupDataLayer } = require("./service/DataLayer");
// swaggerRouter configuration
var options = {
swaggerUi: path.join(__dirname, "/swagger.json"),
controllers: path.join(__dirname, "./controllers"),
useStubs: process.env.NODE_ENV === "development", // Conditionally turn on stubs (mock mode)
};
// The Swagger document (require it, build it programmatically, fetch it from a URL, ...)
var spec = fs.readFileSync(path.join(__dirname, "api/swagger.yaml"), "utf8");
var swaggerDoc = jsyaml.safeLoad(spec);
// 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());
// Validate Swagger requests
app.use(middleware.swaggerValidator());
// Route validated requests to appropriate controller
app.use(middleware.swaggerRouter(options));
// Serve the Swagger documents and Swagger UI
app.use(middleware.swaggerUi());
// Serve the website
app.use(serveStatic(__dirname + "/public"));
// Backend redirections
app.use(redirect())
.use(function(req, res) {
if (req.url == '/backend/main.html') {
res.redirect('/backend')
} else if (req.url == '/docs') {
res.end('/backend/swaggerui');
}
});
// Start the server
setupDataLayer()
.then(() => {
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
);
});
})
.catch(function(error) {
console.log("Starting DataLayer and Server failed: ", error);
});
});