This repository has been archived by the owner on Jun 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·63 lines (51 loc) · 1.57 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
#!/usr/bin/env node
var argv = require('optimist')
.usage('usage: geologist --port [port] --minecrafthost [host] --queryport [query port] --rconport [rcon port] --rconpassword [rcon password]')
.demand(['port', 'minecrafthost', 'queryport', 'rconport', 'rconpassword'])
.argv;
var express = require('express');
var app = express.createServer();
app.configure(function() {
app.use(express.bodyParser());
});
var Query = require('./vendor/mcquery');
var query = new Query();
var Rcon = require('./vendor/node-rcon').newHandle;
var rcon = new Rcon();
app.get('/stats', function (req, res) {
res.contentType('application/json');
query.startSession(argv.minecrafthost, argv.queryport, function (err, session) {
if (err) {
res.status(504);
res.send({"error": "unable to connect to server"});
return;
}
query.full_stat(session, function (err, stat) {
if (err) {
res.status(502);
res.send({"error": "unable to retrieve stats"});
return;
}
res.send(stat);
});
});
});
app.post('/command', function (req, res) {
res.contentType('application/json');
rcon.connect(argv.minecrafthost, argv.rconport, argv.rconpassword, function (err, response) {
if (err) {
res.status(504);
res.send({"error": "unable to connect to server"});
return;
}
rcon.sendCommand(req.body.command, function (err, response) {
if (err) {
res.status(502);
res.send({"error": "unable send rcon command"});
return;
}
res.send(response);
});
});
});
app.listen(argv.port);