Browse Source

Add config server

mods_upload_support
Johannes Zellner 6 years ago
parent
commit
04e3c749d2
5 changed files with 178 additions and 0 deletions
  1. +2
    -0
      .gitignore
  2. +7
    -0
      .jshintrc
  3. +66
    -0
      index.html
  4. +84
    -0
      index.js
  5. +19
    -0
      package.json

+ 2
- 0
.gitignore View File

@ -0,0 +1,2 @@
node_modules/
world/

+ 7
- 0
.jshintrc View File

@ -0,0 +1,7 @@
{
"node": true,
"browser": true,
"unused": true,
"globalstrict": true,
"predef": [ "angular", "$" ]
}

+ 66
- 0
index.html View File

@ -0,0 +1,66 @@
<html>
<head>
<title> Minecraft server configuration </title>
<script src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
function refreshStatus() {
$.getJSON('/status', function (result) {
if (result.running) $('#statusLabel').text('running');
else $('#statusLabel').text('stopped');
setTimeout(refreshStatus, 1000);
});
}
$(function() {
refreshStatus();
$('#startButton').click(function () {
$.post('/start', function (result) {
console.log(result);
});
});
$('#stopButton').click(function () {
$.post('/stop', function (result) {
console.log(result);
});
});
$('#configButton').click(function () {
$.post('/config', { config: $('#configTextarea').val() }, function (result) {
console.log(result);
});
});
$.get('/config', function (result) {
$('#configTextarea').val(result);
});
});
</script>
</head>
<body>
<h4>Server <span id="statusLabel"></span></h4>
<p>
<button id="startButton">Start</button>
<button id="stopButton">Stop</button>
</p>
<p>
<h4>Configuration:</h4>
<textarea id="configTextarea" cols="100" rows="30"></textarea>
<br/>
<button id="configButton">Save and Restart</button>
</p>
</body>
</html>

+ 84
- 0
index.js View File

@ -0,0 +1,84 @@
'use strict';
var express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
fs = require('fs');
var minecraft = null;
function startMinecraft() {
console.log('start minecraft server');
minecraft = require('child_process').spawn('java', ['-Xmx1024M', '-Xms1024M', '-jar', path.join(__dirname, 'minecraft_server.jar'), 'nogui']);
minecraft.stdout.pipe(process.stdout);
minecraft.stderr.pipe(process.stderr);
minecraft.on('close', function () {
minecraft = null;
});
}
function stopMinecraft(callback) {
console.log('stop minecraft server');
if (!minecraft) return callback();
minecraft.kill();
minecraft.on('close', function () {
minecraft = null;
callback();
});
}
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
app.get('/config', function (req, res) {
fs.readFile(path.join(__dirname, 'server.properties'), function (error, result) {
if (error) return res.send(500, error);
res.send(200, result);
});
});
app.post('/config', function (req, res) {
stopMinecraft(function () {
fs.writeFile(path.join(__dirname, 'server.properties'), req.body.config, function (error) {
if (error) res.send(500, error);
startMinecraft();
res.send(200);
});
});
});
app.get('/status', function (req, res) {
res.send(200, { running: !!minecraft });
});
app.post('/start', function (req, res) {
startMinecraft();
res.send(200);
});
app.post('/stop', function (req, res) {
stopMinecraft(function () {
res.send(200);
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Configuration server listening at http://%s:%s', host, port);
});

+ 19
- 0
package.json View File

@ -0,0 +1,19 @@
{
"name": "minecraft-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "ssh://git@gitlab-yellowtent.cloudron.me:6000/yellowtent/minecraft-app.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.12.3",
"express": "^4.12.3"
}
}

Loading…
Cancel
Save