-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
105 lines (90 loc) · 2.9 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
var express = require('express');
var serveIndex = require('serve-index');
var https = require('https');
var app = express();
var route_manager = require("./utils/route-manager.js");
var scheduler = require("./presenters/schedule-controller.js");
var bodyParser = require('body-parser');
var basic_auth = require('basic-auth');
var FileStreamRotator = require('file-stream-rotator');
var morgan = require('morgan');
var fs = require('fs');
var logDirectory = 'log'
var production = process.argv[2];
/* istanbul ignore if */
if (production === "-p") {
var log_passwd = fs.readFileSync('logpasswd', 'utf-8');
}
scheduler.register_existing_events();
/* istanbul ignore next */
var auth = function (req, res, next) {
function unauthorized(res) {
res.set('WWW-Authenticate', 'Basic realm=Authorization Required');
return res.sendStatus(401);
};
var user = basic_auth(req);
if (!user || !user.name || !user.pass) {
return unauthorized(res);
};
if (user.name === 'logs' && user.pass === log_passwd.trim()) {
return next();
} else {
return unauthorized(res);
};
};
/*istanbul ignore if*/
if (production === "-p") {
// ensure log directory exists
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
// create a rotating write stream
var accessLogStream = FileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: logDirectory + '/access-%DATE%.log',
frequency: 'daily',
verbose: false
})
// setup the logger
app.use(morgan('short', {stream: accessLogStream}))
app.use(morgan('short'));
}
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use('/', route_manager);
app.use('/source', require('magic-window')('/source', { ignore: ['config', 'redis', 'cert.pem', 'key.pem', 'dump.rdb', 'logpasswd'] }))
/* istanbul ignore if */
if (production === "-p") {
app.use('/log', auth, express.static('log'));
app.use('/log', auth, serveIndex('log', {'icons': true}));
}
var server = app.listen(3000, function () {
console.log('Now accepting connections on port 3000.');
});
/* istanbul ignore if */
if (production === "-p") {
var prkey = fs.readFileSync('key.pem');
var certi = fs.readFileSync('cert.pem');
app.listen(80, function() {
console.log('Now accepting connections on port 80.');
});
https.createServer({
key: prkey,
cert: certi
}, app).listen(443, function() {
console.log("Now accepting HTTPS connections on port 443.");
});
}
/* istanbul ignore next */
process.on('SIGINT', function() {
console.log( "\nRecieved Ctrl-C, shutting down." );
process.exit(0);
});
module.exports = {
close: function () {
server.close();
}
};