forked from appirio-tech/lc1-challenge-service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
86 lines (70 loc) · 2.2 KB
/
app.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
/**
* Copyright (c) 2014 TopCoder, Inc. All rights reserved.
*/
'use strict';
var dotenv = require('dotenv');
dotenv.load();
// New relic
if (process.env.NODE_ENV === 'production') {
require('newrelic');
}
var a127 = require('a127-magic');
var express = require('express');
var config = require('config');
var datasource = require('./datasource');
var routeHelper = require('serenity-route-helper');
var resopnseHelper = require('serenity-partial-response-helper');
var partialResponseHelper = null;
var bodyParser = require('body-parser');
var auth = require('serenity-auth');
var morgan = require('morgan');
var winston = require('winston');
var app = express();
a127.init(function (swaggerConfig) {
app.use(morgan('dev'));
app.use(bodyParser.json());
//app.use(errors.middleware.crashProtector());
// central point for all authentication
auth.auth(app, config, auth.requireAuth);
/**
* Serenity-datasource module standard configuration
* This configuration is defined in global application configuration
* which is exposed node config module
* For more information about serenity-datasource module configuration
* see module documentation
*/
// @TODO add try/catch logic
datasource.init(config);
partialResponseHelper = new resopnseHelper(datasource);
var port;
if (config.has('app.port')) {
port = config.get('app.port');
} else {
port = 10010;
}
app.use(partialResponseHelper.parseFields);
// a127 middlewares
app.use(a127.middleware(swaggerConfig));
// Serve the Swagger documents and Swagger UI
if (config.has('app.loadDoc') && config.get('app.loadDoc')) {
// adding ui options
var swaggerTools = swaggerConfig['a127.magic'].swaggerTools;
app.use(swaggerTools.swaggerUi({
swaggerUi: config.ui.swaggerUi,
apiDocs: config.ui.apiDocs
}));
}
// Add logging
app.use(function(err, req, res, next) {
if (err) {
winston.error(err.stack || JSON.stringify(err));
routeHelper.middleware.errorHandler(err, req, res, next);
} else {
next();
}
});
// render response data as JSON
app.use(routeHelper.middleware.renderJson);
app.listen(port);
winston.info('Express server listening on port ' + port);
});