-
Notifications
You must be signed in to change notification settings - Fork 13
/
server.js
executable file
·174 lines (134 loc) · 5.92 KB
/
server.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Daniel Varga
* ----------------------------------------------------------------------------
*/
/**
* Module dependencies
*/
var nodefs = require("node-fs");
var homePath = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
var appHome = homePath+'/.slingxdcc/';
nodefs.mkdir(appHome+"config",0775,true,function(){
var logger = require("./lib/xdcclogger"),
downloadHandler = require("./lib/downloadHandler"),
express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser'),
methodOverride = require('method-override'),
https = require('https'),
http = require('http'),
path = require('path'),
fs = require('fs'),
io = require('socket.io'),
nconf = require('nconf'),
log4js = require("log4js"),
appLogger = log4js.getLogger('app')
httpLogger = log4js.getLogger('http'),
errors = require('common-errors');
nconf.add('settings', {type: 'file', file: appHome+'/config/settings.json'});
nconf.defaults({
"webserver": {
"port": 3000,
"ssl": true,
"ssl.crt": appHome+"ssl/server.crt",
"ssl.key": appHome+"ssl/server.key"
}
});
nconf.set('webserver',nconf.get('webserver'));
nconf.save();
process.on('SIGINT', function () {
console.log('Shuting down');
downloadHandler.exit();
logger.exit();
process.exit();
});
nconf.load(function(){
var routes = require('./routes'),
api = require('./routes/api');
var app = module.exports = express();
/**
* Configuration
*/
// all environments
app.set('port', nconf.get('webserver:port'));
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
// app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use(errors.middleware.crashProtector());
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));
app.use(log4js.connectLogger(httpLogger, { level: 'auto', format: ':remote-addr - :method :url HTTP/:http-version :status - :response-time ms',nolog: '\\.gif|\\.jpg$' }));
/**
* Routes
*/
app.get('/', routes.index);
app.get('/partials/:name', routes.partials);
// JSON API
app.get('/api/packet/', api.getNumPackets);
app.get('/api/packet/get/:id', api.packet);
app.get('/api/packet/search/:string/', api.packetSearch);
app.get('/api/packet/search/:string/:page', api.packetSearchPaged);
app.get('/api/packet/list/', api.packetList);
app.get('/api/packet/list/:page', api.packetListPaged);
app.get('/api/packet/sorting/', api.getSorting);
app.get('/api/packet/filter/', api.getFilter);
app.get('/api/packet/pagelimit/', api.getPageLimit);
app.get('/api/server/', api.getServer);
app.get('/api/db/compacting/', api.getNextCompacting);
app.get('/api/db/compactingfilter/', api.getCompactingFilter);
app.get('/api/downloads/', api.getDownloads);
app.get('/api/downloads/notifications/', api.getDlNotifications);
app.get('/api/downloads/notifications/count/', api.getDlNotificationCount);
app.put('/api/packet/sorting/', api.setSorting);
app.put('/api/packet/filter/', api.setFilter);
app.put('/api/packet/pagelimit/', api.setPageLimit);
app.put('/api/db/compacting/', api.compactDb);
app.put('/api/db/compactingfilter/', api.setCompactingFilter);
app.put('/api/channel/', api.channels);
app.put('/api/downloads/upqueue/', api.upQueueDownload);
app.put('/api/downloads/downqueue/', api.downQueueDownload);
app.put('/api/downloads/cancel', api.cancelDownload);
app.post('/api/server/', api.addServer);
app.post('/api/downloads/', api.startDownload);
app.post('/api/db/compacting/', api.startCompactCronjob);
app.delete('/api/server/:key', api.removeServer);
app.delete('/api/downloads/notifications/', api.clearDlNotifications);
app.delete('/api/downloads/notifications/count/', api.clearDlNotificationCount);
app.delete('/api/db/compacting', api.stopCompactCronjob);
app.get('*', routes.index);
app.use(errors.middleware.errorHandler);
/**
* Create server
*/
fs.readFile(nconf.get('webserver:ssl.key'), function (errorkey, key){
fs.readFile(nconf.get('webserver:ssl.crt'), function (errorcrt, crt){
var server;
if ((errorcrt || errorkey) && nconf.get('webserver:ssl')){
server = http.createServer(app);
appLogger.info('No key or cert found, !!!Fallback!!! to Http');
}else if(nconf.get('webserver:ssl')){
server = https.createServer({key: key, cert: crt}, app);
}else{
server = http.createServer(app);
}
// Hook Socket.io into Express
io = io.listen(server);
// Socket.io Communication
io.sockets.on('connection', require('./routes/socket'));
/**
* Start Server
*/
server.listen(app.get('port'), function (){
appLogger.info('Server listening on port ' + app.get('port'));
});
});
});
});
});