Very big changes to help custom config paths

This commit is contained in:
Daniel Vestøl committed 2018-07-13 22:56:10 +02:00
1 parent 02c545c650
commit 98131ee66e
5 files changed
+37 -22

No files matched your search

+3
View File
@@ -454,6 +454,9 @@ write-data=${ path.resolve(config.instanceDirectory, instance) }\r\n
confirmedOrders = []; confirmedOrders = [];
lastSignalCheck = Date.now(); lastSignalCheck = Date.now();
}); });
} else {
console.error("Invalid arguments, quitting.");
process.exit(1);
} }
// ensure instancemanagement only ever runs once // ensure instancemanagement only ever runs once
+6 -4
View File
@@ -1,8 +1,10 @@
const jwt = require('jsonwebtoken'); const jwt = require('jsonwebtoken');
const config = require("./../config"); // const config = require("./../config");
var middleware = function (req, res, next) { module.exports = function(config){
return {
middleware: function (req, res, next) {
var token = req.headers['x-access-token']; var token = req.headers['x-access-token'];
if(!token) return res.status(401).send({ auth: false, message: 'No token provided.', endpoint: req.route.path}); if(!token) return res.status(401).send({ auth: false, message: 'No token provided.', endpoint: req.route.path});
@@ -10,6 +12,6 @@ var middleware = function (req, res, next) {
if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.', endpoint: req.route.path}); if(err) return res.status(500).send({ auth: false, message: 'Failed to authenticate token.', endpoint: req.route.path});
next(); next();
}); });
},
} }
};
module.exports = {middleware};
+4 -2
View File
@@ -1,7 +1,8 @@
const fileOps = require("_app/fileOps"); const fileOps = require("_app/fileOps");
const config = require("./../config"); // const config = require("./../config");
module.exports = { module.exports = function(config) {
return {
sync: function(){ sync: function(){
let instances; let instances;
try { try {
@@ -11,5 +12,6 @@ module.exports = {
instances = []; instances = [];
} }
return instances; return instances;
},
} }
} }
+18 -12
View File
@@ -14,9 +14,8 @@ const sLog = require("single-line-log").stdout;
const query = require('cli-interact').getYesNo; const query = require('cli-interact').getYesNo;
const fileOps = require("_app/fileOps"); const fileOps = require("_app/fileOps");
const config = require("./../../config"); // const config = require("./../../config");
const instances = require("./../getInstances.js").sync();
function ellipse(str, max){ function ellipse(str, max){
return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str; return str.length > (max - 3) ? str.substring(0,max-3) + '...' : str;
@@ -24,6 +23,12 @@ function ellipse(str, max){
// https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript // https://stackoverflow.com/questions/15900485/correct-way-to-convert-size-in-bytes-to-kb-mb-gb-in-javascript
function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]}; function formatBytes(a,b){if(0==a)return"0 Bytes";var c=1024,d=b||2,e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],f=Math.floor(Math.log(a)/Math.log(c));return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f]};
module.exports = function(config){
if(!config) config = require("./../../config");
const instances = require("./../getInstances.js")(config).sync();
return {
/** /**
Lists mods for an instance as a table. Warning, handles printing internally. Lists mods for an instance as a table. Warning, handles printing internally.
@param {string} instance - Name of the instance or "shared" @param {string} instance - Name of the instance or "shared"
@@ -35,7 +40,7 @@ modManager.listMods("testInstance");
modManager.listMods("shared"); modManager.listMods("shared");
// console.logs mods located in /sharedMods as a nice table // console.logs mods located in /sharedMods as a nice table
*/ */
module.exports.listMods = function(instance){ listMods: function(instance){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(instance && instances.includes(instance)){ if(instance && instances.includes(instance)){
factorioAPI.init(false, config.instanceDirectory+'/'+instance+'/instanceMods', config.instanceDirectory+'/'+instance+'/saves', config.factorio_version); factorioAPI.init(false, config.instanceDirectory+'/'+instance+'/instanceMods', config.instanceDirectory+'/'+instance+'/saves', config.factorio_version);
@@ -92,14 +97,14 @@ module.exports.listMods = function(instance){
console.log(JSON.parse(res.getBody())) console.log(JSON.parse(res.getBody()))
*/ */
}); });
} },
/** /**
Searches mods.factorio.com for mods matching a search term and prints Searches mods.factorio.com for mods matching a search term and prints
the results to console as a neat looking table the results to console as a neat looking table
@param {string} searchTerm What to search for @param {string} searchTerm What to search for
*/ */
module.exports.findMods = function(searchTerm){ findMods: function(searchTerm){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
factorioAPI.init(false); factorioAPI.init(false);
factorioAPI.authenticate({ factorioAPI.authenticate({
@@ -127,9 +132,8 @@ module.exports.findMods = function(searchTerm){
reject(new Error(e)); reject(new Error(e));
}); });
}); });
} },
addMod: function(term, instance){
module.exports.addMod = function(term, instance){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(instance == "shared"){ // modDirectory has to end in / if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/"; var modDirectory = "sharedMods/";
@@ -204,8 +208,8 @@ module.exports.addMod = function(term, instance){
reject(new Error("Invalid search term")); reject(new Error("Invalid search term"));
} }
}); });
} },
module.exports.removeMod = function removeMod(term, instance, force = false){ removeMod: function removeMod(term, instance, force = false){
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if(instance == "shared"){ // modDirectory has to end in / if(instance == "shared"){ // modDirectory has to end in /
var modDirectory = "sharedMods/"; var modDirectory = "sharedMods/";
@@ -237,7 +241,7 @@ module.exports.removeMod = function removeMod(term, instance, force = false){
reject(new Error("Invalid search term")); reject(new Error("Invalid search term"));
} }
}); });
} },
// 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases' // 'https://api.github.com/repos/Danielv123/factorioClusterioMod/releases'
/** /**
@@ -246,7 +250,7 @@ the latest release, saving it to /sharedMods
@param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases @param {string} url a github repo URL, for example https://api.github.com/repos/Danielv123/factorioClusterioMod/releases. Must link to /releases
*/ */
module.exports.downloadFromGitURL = function(url){ downloadFromGitURL: function(url){
console.log("Downloading mod from "+url); console.log("Downloading mod from "+url);
// get JSON data about releases // get JSON data about releases
let res = syncRequest('GET', url, {"headers":{"User-Agent":"Please don't require setting a user agent"}}); let res = syncRequest('GET', url, {"headers":{"User-Agent":"Please don't require setting a user agent"}});
@@ -261,6 +265,8 @@ module.exports.downloadFromGitURL = function(url){
console.log("Downloaded "+name); console.log("Downloaded "+name);
}); });
} }
},
}
} }
/* /*
module.exports.findUpdates = function(){ module.exports.findUpdates = function(){
+6 -4
View File
@@ -52,7 +52,7 @@ const stringUtils = require("./lib/stringUtils");
const fileOps = require("_app/fileOps"); const fileOps = require("_app/fileOps");
// homemade express middleware for token auth // homemade express middleware for token auth
const authenticate = require("./lib/authenticate"); const authenticate = require("./lib/authenticate")(config);
/** Sync */ /** Sync */
function randomStringAsBase64Url(size) { function randomStringAsBase64Url(size) {
@@ -279,7 +279,9 @@ db.items.removeItem = function(object) {
} }
// store slaves and inventory in a .json full of JSON data // store slaves and inventory in a .json full of JSON data
process.on('SIGINT', async function () { process.on('SIGINT', shutdown); // ctrl + c
process.on('SIGHUP', shutdown); // terminal closed
async function shutdown() {
console.log('Ctrl-C...'); console.log('Ctrl-C...');
let exitStartTime = Date.now(); let exitStartTime = Date.now();
// set insane limit to slave length, if its longer than this we are probably being ddosed or something // set insane limit to slave length, if its longer than this we are probably being ddosed or something
@@ -304,8 +306,8 @@ process.on('SIGINT', async function () {
} }
} }
console.log("Exited in "+(Date.now()-exitStartTime)+"ms"); console.log("Exited in "+(Date.now()-exitStartTime)+"ms");
process.exit(2); process.exit(0);
}); }
/** /**
POST World ID management. Slaves post here to tell the server they exist POST World ID management. Slaves post here to tell the server they exist