-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
76 lines (55 loc) · 1.75 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
var express = require('express')
var app = require('express')()
var server = require('http').Server(app)
var path = require('path')
var spawn = require('child_process').spawn
var fs = require('fs')
var ws = require('websocket').server
var args = require('yargs').argv
var port = args.port || process.env.LINUX_DASH_SERVER_PORT || 80
server.listen(port, function() {
console.log('Linux Dash Server Started on port ' + port + '!');
})
app.use(express.static(path.resolve(__dirname + '/../')))
app.get('/', function (req, res) {
res.sendFile(path.resolve(__dirname + '/../index.html'))
})
app.get('/websocket', function (req, res) {
res.send({
websocket_support: true,
})
})
wsServer = new ws({
httpServer: server
})
var nixJsonAPIScript = __dirname + '/linux_json_api.sh'
function getPluginData(pluginName, callback) {
var command = spawn(nixJsonAPIScript, [ pluginName, '' ])
var output = []
command.stdout.on('data', function(chunk) {
output.push(chunk.toString())
})
command.on('close', function (code) {
callback(code, output)
})
}
wsServer.on('request', function(request) {
var wsClient = request.accept('', request.origin)
wsClient.on('message', function(wsReq) {
var moduleName = wsReq.utf8Data
var sendDataToClient = function(code, output) {
if (code === 0) {
var wsResponse = '{ "moduleName": "' + moduleName + '", "output": "'+ output.join('') +'" }'
wsClient.sendUTF(wsResponse)
}
}
getPluginData(moduleName, sendDataToClient)
})
})
app.get('/server/', function (req, res) {
var respondWithData = function(code, output) {
if (code === 0) res.send(output.toString())
else res.sendStatus(500)
}
getPluginData(req.query.module, respondWithData)
})