forked from gclem/ovhtask-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
75 lines (62 loc) · 1.99 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
/*
* Lighweight Web API publishing overwatch-js npm package functionnalities
*/
var express = require('express');
var http = require('http');
var winston = require('winston');
var morgan = require('morgan');
var config = require('./config/default.json');
var socketio = require('socket.io');
var app = express();
var server = http.Server(app);
/* Logger */
var logger = new winston.Logger({
transports: [
new winston.transports.File({
level: 'info',
filename: 'main.log',
handleExceptions: true,
json: false,
maxsize: 5242880, //5MB
maxFiles: 5,
colorize: true
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
});
logger.stream = {
write: function (message, encoding) {
logger.info(message);
}
};
/* Configuration */
app.set('trust proxy', '127.0.0.1');
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.use(morgan('combined', { "stream": logger.stream }));
/* Configuration : SSL */
var privateKey = config.ssl && config.ssl.enabled ? fs.readFileSync(config.ssl.key, 'utf8') : '';
var certificate = config.ssl && config.ssl.enabled ? fs.readFileSync(config.ssl.cert, 'utf8') : '';
var credentials = { key: privateKey, cert: certificate };
/* Setup server */
var port = process.env.PORT || 3001;
var server = (config.ssl && config.ssl.enabled ? http.createServer(credentials, app) : http.createServer(app))
server.listen(port, function () {
logger.log('info', `Starting web server on port : ${port}.`);
});
/* Setup IO */
var io = socketio(server);
var events = require('./routes/events')(io);
/* Routes */
const routes = require('./routes/index');
// Connect all our routes to our application
app.use('/', routes);