Added a page to create a Minecraft server; added config module
This commit is contained in:
parent
13e730df7b
commit
8e80151331
@ -8,7 +8,9 @@ class Server {
|
|||||||
|
|
||||||
// read version file
|
// read version file
|
||||||
var versionFilePath = this.rootDir + '/current_version.txt';
|
var versionFilePath = this.rootDir + '/current_version.txt';
|
||||||
|
if (fs.existsSync(versionFilePath)) {
|
||||||
this.version = fs.readFileSync(versionFilePath, {encoding:'utf8', flag:'r'});
|
this.version = fs.readFileSync(versionFilePath, {encoding:'utf8', flag:'r'});
|
||||||
|
}
|
||||||
|
|
||||||
// set server state
|
// set server state
|
||||||
if (fs.existsSync(this.pidFilePath)) {
|
if (fs.existsSync(this.pidFilePath)) {
|
||||||
|
3
config/default.json
Normal file
3
config/default.json
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"server_directory": "/opt/minecraft"
|
||||||
|
}
|
8
index.js
8
index.js
@ -5,11 +5,19 @@ const port = 3000;
|
|||||||
// set template engine to Pug
|
// set template engine to Pug
|
||||||
app.set('view engine', 'pug');
|
app.set('view engine', 'pug');
|
||||||
|
|
||||||
|
// Using express.urlencoded middleware
|
||||||
|
app.use(express.urlencoded({
|
||||||
|
extended: true
|
||||||
|
}));
|
||||||
|
|
||||||
// import route handlers
|
// import route handlers
|
||||||
var homeRoutes = require('./routes/home');
|
var homeRoutes = require('./routes/home');
|
||||||
|
var serverRoutes = require('./routes/server');
|
||||||
|
|
||||||
// define routes
|
// define routes
|
||||||
app.get('/', homeRoutes.getIndex);
|
app.get('/', homeRoutes.getIndex);
|
||||||
|
app.get('/server/create', serverRoutes.getCreate);
|
||||||
|
app.post('/server/create', serverRoutes.postCreate);
|
||||||
|
|
||||||
// set Express to serve static files (ideally this is only used in development)
|
// set Express to serve static files (ideally this is only used in development)
|
||||||
app.use(express.static('./static/'));
|
app.use(express.static('./static/'));
|
||||||
|
@ -1,8 +1,19 @@
|
|||||||
|
const config = require('config');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
const minecraft = require('../app/MinecraftServer');
|
const minecraft = require('../app/MinecraftServer');
|
||||||
|
|
||||||
exports.getIndex = function(req, res) {
|
exports.getIndex = function(req, res) {
|
||||||
var server = new minecraft.Server('/opt/minecraft/bg_w3');
|
// search for minecraft server directories
|
||||||
|
let rootDir = config.get('server_directory');
|
||||||
|
let serverDirs = fs.readdirSync(rootDir);
|
||||||
|
let servers = [];
|
||||||
|
for (let i = 0; i < serverDirs.length; i++) {
|
||||||
|
servers.push(new minecraft.Server(path.join(rootDir, serverDirs[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
// render view
|
||||||
res.render('index', {
|
res.render('index', {
|
||||||
servers: [server],
|
servers: servers,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
45
routes/server.js
Normal file
45
routes/server.js
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
const config = require('config');
|
||||||
|
const fs = require('fs');
|
||||||
|
const https = require('https');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
exports.getCreate = function(req, res) {
|
||||||
|
// render view
|
||||||
|
res.render('create');
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.postCreate = function(req, res) {
|
||||||
|
mcDir = config.get('server_directory');
|
||||||
|
serverDir = path.join(mcDir, req.body.serverName);
|
||||||
|
|
||||||
|
// make server directory
|
||||||
|
fs.mkdirSync(serverDir);
|
||||||
|
|
||||||
|
// grab the server JAR file
|
||||||
|
serverJarUrl = 'https://piston-data.mojang.com/v1/objects/f69c284232d7c7580bd89a5a4931c3581eae1378/server.jar';
|
||||||
|
serverJarName = "server_" + req.body.serverVersion + ".jar";
|
||||||
|
serverJarPath = path.join(serverDir, serverJarName);
|
||||||
|
https.get(serverJarUrl, (res) => {
|
||||||
|
const filePath = fs.createWriteStream(serverJarPath);
|
||||||
|
res.pipe(filePath);
|
||||||
|
filePath.on('finish', () => {
|
||||||
|
filePath.close();
|
||||||
|
console.log('Download Completed');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// create the start.sh shell script
|
||||||
|
scriptFilePath = path.join(serverDir, 'start.sh');
|
||||||
|
scriptContent = "#!/bin/sh\n\ncd " + serverDir + "\njava -Xmx2048M -Xms2048M -jar server_" + req.body.serverVersion + ".jar nogui";
|
||||||
|
fs.writeFileSync(scriptFilePath, scriptContent);
|
||||||
|
fs.chmodSync(scriptFilePath, 0o755);
|
||||||
|
|
||||||
|
// save the current version to a text file - this will hopefully be temporary solution
|
||||||
|
versionFilePath = path.join(serverDir, 'current_version.txt');
|
||||||
|
versionContent = req.body.serverVersion;
|
||||||
|
fs.writeFileSync(versionFilePath, versionContent);
|
||||||
|
fs.chmodSync(versionFilePath, 0o644);
|
||||||
|
|
||||||
|
// redirect the user back to the home page
|
||||||
|
res.redirect('/');
|
||||||
|
};
|
24
views/create.pug
Normal file
24
views/create.pug
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
extends layout.pug
|
||||||
|
|
||||||
|
block content
|
||||||
|
header.row
|
||||||
|
div.columns.twelve
|
||||||
|
h1 Create new server
|
||||||
|
|
||||||
|
section.row
|
||||||
|
div.columns.twelve
|
||||||
|
form(action='/server/create', method='POST')
|
||||||
|
div.row
|
||||||
|
div.columns.six
|
||||||
|
label(for='serverName') Server name:
|
||||||
|
input#serverName.u-full-width(name='serverName', type='text', placeholder='MyServer')
|
||||||
|
div.columns.six
|
||||||
|
label(for='serverVersion') Minecraft version:
|
||||||
|
input#serverVersion.u-full-width(name='serverVersion', type='text', placeholder='1.19.2')
|
||||||
|
|
||||||
|
input(type='submit', value='Submit')
|
||||||
|
|
||||||
|
div.row
|
||||||
|
div.columns.twelve
|
||||||
|
p
|
||||||
|
a(href='/') Back
|
@ -13,7 +13,7 @@ html(lang="en")
|
|||||||
ul
|
ul
|
||||||
li.menu-text MCST
|
li.menu-text MCST
|
||||||
li: a(href='/') Home
|
li: a(href='/') Home
|
||||||
li: a(href='/create') Create
|
li: a(href='/server/create') Create
|
||||||
li: a(href='/status') Status
|
li: a(href='/status') Status
|
||||||
|
|
||||||
// main content
|
// main content
|
||||||
|
Loading…
Reference in New Issue
Block a user