mcst/routes/server.js

46 lines
1.5 KiB
JavaScript

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('/');
};