-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (40 loc) · 1.34 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
const restify = require('restify'),
restifyPlugins = restify.plugins,
colors = require('colors'),
lib = require('./lib'),
swagger = require('swagger-node-restify'),
config = require('config');
const server = restify.createServer(config.get('server'));
server.use(restifyPlugins.queryParser({
mapParams: true
}));
server.use(restifyPlugins.bodyParser());
restify.defaultResponseHeaders = data => {
this.header('Access-Control-Allow-Origin', '*');
}
server.use((req, res, next) => {
let results = lib.schemaValidator.validateRequest(req);
if(results.valid) {
return next();
}
res.send(400, results);
});
//The swagger-ui is inside the "swagger-ui" folder
server.get(/^\/swagger-ui(\/.*)?/, restifyPlugins.serveStatic({
directory: __dirname + '/',
default: 'index.html'
}));
swagger.addModels(lib.schemas);
swagger.setAppHandler(server);
lib.helpers.setupRoutes(server, swagger, lib);
swagger.configureSwaggerPaths("", "/api-docs", ""); //We remove the {format} of the paths, to swagger.configure('http://localhost:9000', '0.1')
server.listen(config.get('server.port'), () => {
lib.logger.info("Server started successfully...");
lib.db.connect(err => {
if(err) {
lib.logger.error("Error trying to connect to database: ", err);
} else {
lib.logger.info("Database service successfully started")
}
});
});