-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
107 lines (98 loc) · 3.25 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
/**
* @module server
*/
//standard libraries
var http = require('http');
var Path = require('path');
//external dependencies
var express = require('express');
var bodyParser = require('body-parser');
var Logger = require('log4js');
//custom modules
var Configer = require(Path.join(__dirname, 'configer.js'));
var WebSocket = require(Path.join(__dirname, 'websockets.js'));
var RemoteLogs = require(Path.join(__dirname, 'log4javascript_to_log4js.js'));
var DirLister = require(Path.join(__dirname, 'dirlister.js'));
var ComputerControl = require(Path.join(__dirname, 'computer_control.js'));
// set up logging
Logger.configure(require('./log4js_conf.json'));
var logger = Logger.getLogger('Server');
/**
* The station this is serving and configured for
* @type {string}
*/
var station = 'demo';
var hostname = '0.0.0.0';
var port = 8080;
var argv = process.argv;
for (var argI = 0; argI < argv.length; argI++){
switch(argv[argI]){
case '--station':
station = argv[++argI];
logger.debug('configured as station:',station);
}
}
/**
* handles persistent configs
* @type {Configer}
*/
var config = new Configer(Logger.getLogger('Configer'));
//load this station's config
config.load('static/'+station+'/config.json').
catch( reason =>
logger.error('could not load configuration file', reason) ).
then( function resolved(){
logger.debug('[resolved] config done loading');
// set up HTTP handlers
/**
* Express server for simple middleware-based message handling
* @type {Express}
*/
var app = express();
app.use(Logger.connectLogger(
Logger.getLogger('express', {level: 'auto'})));
app.use(bodyParser.urlencoded({extended:true}));
if (config.getConfig('allowControl')){
app.use('/comp', ComputerControl.control());
hostname = '127.0.0.1';
}
app.use(RemoteLogs.log(Logger));
{
//create full webpath <-> filepath mapping for directory lister
//JSON dance to achieve deep-copy
var staticList = JSON.parse(
JSON.stringify(
config.getConfig('additionalStatic') || []));
staticList.push({diskPath: Path.join(__dirname, 'static'),
webPath: '/'});
app.use('/list', DirLister.list(staticList));
}
//setup additional static directories
{
var statics = config.getConfig('additionalStatic');
if (statics){
statics.forEach(function(item){
logger.debug('[resolved] setting up static hosting of'
,item.diskPath,'via',item.webPath);
app.use(item.webPath, express.static(item.diskPath));
});
}
}
//setup standard static directory
app.use(express.static('static', {index:station+'.html'}));
/**
* server for accepting connections on the given port
* @type {http.Server}
*/
var server = http.createServer(app);
/*eslint-disable no-unused-vars*/
/**
* Websocket server to handle websocket communication
*/
var wss = new WebSocket(server, config, Logger.getLogger('websocket'));
/*eslint-enable*/
// listen to connections on the given port/interface
server.listen(port, hostname, function(){
logger.info('listening on port',port);
});
});