Update to Express 4

This commit is contained in:
Björn Dahlgren 2017-04-01 11:19:30 +02:00
parent 79d6f2409d
commit a974d1bbf6
10 changed files with 210 additions and 187 deletions

47
app.js
View File

@ -1,5 +1,9 @@
var express = require('express'); var express = require('express');
var Resource = require('express-resource'); var basicAuth = require('express-basic-auth')
var bodyParser = require('body-parser');
var morgan = require('morgan');
var path = require('path');
var serveStatic = require('serve-static')
var config = require('./config'); var config = require('./config');
var Manager = require('./lib/manager'); var Manager = require('./lib/manager');
@ -12,14 +16,18 @@ var server = require('http').Server(app);
var io = require('socket.io')(server); var io = require('socket.io')(server);
if (config.auth && config.auth.username && config.auth.password) { if (config.auth && config.auth.username && config.auth.password) {
app.use(express.basicAuth(config.auth.username, config.auth.password)); var basicAuthUsers = {}
basicAuthUsers[config.auth.username] = config.auth.password;
app.use(basicAuth({
challenge: true,
users: basicAuthUsers
}));
} }
app.use(express.logger('dev')); app.use(bodyParser.json());
app.use(express.cookieParser()); app.use(bodyParser.urlencoded());
app.use(express.bodyParser()); app.use(morgan('dev'));
app.use(express.methodOverride()); app.use(serveStatic(path.join(__dirname, 'public')));
app.use(express.static(__dirname + '/public'));
var logs = new Logs(config); var logs = new Logs(config);
@ -30,26 +38,11 @@ var missions = new Missions(config);
var mods = new Mods(config); var mods = new Mods(config);
mods.updateMods(); mods.updateMods();
var logsRoutes = require('./routes/logs')(logs); app.use('/api/logs', require('./routes/logs')(logs));
var serversRoutes = require('./routes/servers')(manager, mods); app.use('/api/missions', require('./routes/missions')(missions));
var missionsRoutes = require('./routes/missions')(missions); app.use('/api/mods', require('./routes/mods')(mods));
var modsRoutes = require('./routes/mods')(mods); app.use('/api/servers', require('./routes/servers')(manager, mods));
app.use('/api/settings', require('./routes/settings')(config));
app.resource('api/logs', logsRoutes);
app.resource('api/missions', missionsRoutes);
app.resource('api/mods', modsRoutes);
var serversResource = app.resource('api/servers', serversRoutes);
app.resource('api/settings', require('./routes/settings'));
app.post('/api/missions/workshop', require('./routes/workshop').mission);
app.post('/api/mods/refresh', modsRoutes.refresh);
app.post('/api/mods/search', modsRoutes.search);
app.get('/api/servers/:server/start', serversRoutes.start);
app.get('/api/servers/:server/stop', serversRoutes.stop);
app.get('/', function (req, res){
res.sendfile(__dirname + '/public/index.html');
});
io.on('connection', function (socket) { io.on('connection', function (socket) {
socket.emit('mods', mods.mods); socket.emit('mods', mods.mods);

View File

@ -2,9 +2,11 @@ var async = require('async');
var filesize = require('filesize'); var filesize = require('filesize');
var fs = require('fs'); var fs = require('fs');
var path = require('path'); var path = require('path');
var SteamWorkshop = require('steam-workshop');
var Missions = function (config) { var Missions = function (config) {
this.config = config; this.config = config;
this.steamWorkshop = new SteamWorkshop(this.missionsPath());
}; };
Missions.prototype.missionsPath = function() { Missions.prototype.missionsPath = function() {
@ -55,4 +57,12 @@ Missions.prototype.delete = function (missionName, cb) {
fs.unlink(path.join(this.missionsPath(), missionName), cb); fs.unlink(path.join(this.missionsPath(), missionName), cb);
}; };
Missions.prototype.downloadSteamWorkshop = function (id, cb) {
if (!id) {
return cb(new Error('Not a valid Steam Workshop ID: ' + id));
}
this.steamWorkshop.downloadFile(id, cb);
}
module.exports = Missions; module.exports = Missions;

View File

@ -12,13 +12,16 @@
"dependencies": { "dependencies": {
"arma-server": "0.0.5", "arma-server": "0.0.5",
"async": "^0.9.0", "async": "^0.9.0",
"express": "3.x", "body-parser": "^1.17.1",
"express-resource": "~1.0.0", "express": "^4.15.2",
"express-basic-auth": "^1.0.1",
"filesize": "^3.1.0", "filesize": "^3.1.0",
"fs.extra": "~1.3.2", "fs.extra": "~1.3.2",
"gamedig": "0.2.25", "gamedig": "0.2.25",
"lodash": "^3.6.0", "lodash": "^3.6.0",
"morgan": "^1.8.1",
"playwithsix": "0.0.11", "playwithsix": "0.0.11",
"serve-static": "^1.12.1",
"slugify": "^1.1.0", "slugify": "^1.1.0",
"socket.io": "^1.0.4", "socket.io": "^1.0.4",
"steam-workshop": "0.0.1", "steam-workshop": "0.0.1",

View File

@ -22,7 +22,7 @@ define(function (require) {
event.preventDefault(); event.preventDefault();
$.ajax({ $.ajax({
url: "/api/servers/" + this.model.get('id') + "/start", url: "/api/servers/" + this.model.get('id') + "/start",
type: 'GET', type: 'POST',
success: function (resp) { success: function (resp) {
self.model.set("pid", resp.pid); self.model.set("pid", resp.pid);
self.render(); self.render();
@ -45,7 +45,7 @@ define(function (require) {
function(){ function(){
$.ajax({ $.ajax({
url: "/api/servers/" + self.model.get('id') + "/stop", url: "/api/servers/" + self.model.get('id') + "/stop",
type: 'GET', type: 'POST',
success: function (resp) { success: function (resp) {
self.model.set("pid", resp.pid); self.model.set("pid", resp.pid);
self.render(); self.render();

View File

@ -1,31 +1,33 @@
module.exports = function (logsManager) { var express = require('express');
return {
index: function(req, res){
logsManager.logFiles(function (err, files) {
if (err) {
res.send(err);
} else {
res.send(files);
}
});
},
show: function(req, res){
var requestedFilename = req.params.log;
if (req.format) {
requestedFilename += "." + req.format;
}
logsManager.getLogFile(requestedFilename, function (err, file) { module.exports = function (logsManager) {
if (err) { var router = express.Router();
res.send(err);
router.get('/', function (req, res) {
logsManager.logFiles(function (err, files) {
if (err) {
res.status(500).send(err);
} else {
res.json(files);
}
});
});
router.get('/:log', function(req, res) {
var requestedFilename = req.params.log;
logsManager.getLogFile(requestedFilename, function (err, file) {
if (err) {
res.status(500).send(err);
} else {
if (file) {
res.download(file.path);
} else { } else {
if (file) { res.status(404).send(new Error("File not found"));
res.download(file.path);
} else {
res.send(404, new Error("File not found"));
}
} }
}); }
}, });
}; });
return router;
}; };

View File

@ -1,41 +1,58 @@
var express = require('express');
module.exports = function (missionsManager) { module.exports = function (missionsManager) {
return { var router = express.Router();
index: function (req, res) {
missionsManager.list(function (err, missions) {
if (err) {
res.send(err);
} else {
res.send(missions);
}
});
},
create: function (req, res) {
var missionFile = req.files.mission;
missionsManager.handleUpload(missionFile, function (err) {
res.send(err);
});
},
show: function(req, res){
var filename = req.params.mission;
if (req.params.format) {
filename += '.' + req.params.format;
}
res.download(missionsManager.missionPath(encodeURI(filename)), decodeURI(filename)); router.get('/', function (req, res) {
}, missionsManager.list(function (err, missions) {
destroy: function(req, res){ if (err) {
var filename = req.params.mission; res.status(500).send(err);
if (req.params.format) { } else {
filename += '.' + req.params.format; res.json(missions);
} }
});
});
missionsManager.delete(filename, function (err) { router.post('/', function (req, res) {
if (err) { var missionFile = req.files.mission;
res.json(500, {success: false}); missionsManager.handleUpload(missionFile, function (err) {
} else { if (err) {
res.json({success: true}); res.status(500).send(err);
} } else {
}); res.status(204);
}, }
}; });
});
router.get('/:mission', function (req, res) {
var filename = req.params.mission;
res.download(missionsManager.missionPath(filename), decodeURI(filename));
});
router.delete('/:mission', function (req, res) {
var filename = req.params.mission;
missionsManager.delete(filename, function (err) {
if (err) {
res.status(500).send(err);
} else {
res.json({success: true});
}
});
});
router.post('/workshop', function (req, res) {
var id = req.body.id;
missionsManager.downloadSteamWorkshop(id, function (err, files) {
if (err) {
res.status(500).send(err);
} else {
res.json({success: true});
}
});
});
return router;
}; };

View File

@ -1,43 +1,47 @@
var express = require('express');
module.exports = function (modsManager) { module.exports = function (modsManager) {
return { var router = express.Router();
index: function(req, res){
res.send(modsManager.mods);
},
create: function(req, res){ router.get('/', function (req, res) {
modsManager.download(req.body.name); res.send(modsManager.mods);
res.send(204, {}); });
},
update: function(req, res){ router.post('/', function (req, res) {
modsManager.download(req.params.mod); modsManager.download(req.body.name);
res.send(204, {}); res.status(204);
}, });
destroy: function(req, res){ router.put('/:mod', function (req, res) {
modsManager.delete(req.params.mod, function (err) { modsManager.download(req.params.mod);
if (err) { res.status(204);
res.send(500, err); });
} else {
res.send(204, {});
}
});
},
refresh: function(req, res){ router.delete('/:mod', function (req, res) {
modsManager.updateMods(); modsManager.delete(req.params.mod, function (err) {
res.send(200, {}); if (err) {
}, res.status(500).send(err);
} else {
res.status(204);
}
});
});
search: function(req, res){ router.post('/refresh', function (req, res) {
var query = req.body.query || ""; modsManager.updateMods();
modsManager.search(query, function (err, mods) { res.status(204);
if (err || !mods) { });
res.send(500, err);
} else { router.post('/search', function (req, res) {
res.send(mods); var query = req.body.query || "";
} modsManager.search(query, function (err, mods) {
}); if (err || !mods) {
} res.status(500).send(err);
}; } else {
res.send(mods);
}
});
});
return router;
}; };

View File

@ -1,46 +1,50 @@
var express = require('express');
module.exports = function (manager, mods) { module.exports = function (manager, mods) {
return { var router = express.Router();
index: function (req, res){
res.send(manager.getServers());
},
create: function (req, res) { router.get('/', function (req, res) {
var server = manager.addServer(req.body); res.json(manager.getServers());
res.send(server); });
},
show: function (req, res){ router.post('/', function (req, res) {
var server = manager.getServer(req.params.server); var server = manager.addServer(req.body);
res.send(server); res.json(server);
}, });
update: function(req, res){ router.get('/:server', function (req, res) {
var server = manager.getServer(req.params.server); var server = manager.getServer(req.params.server);
server.update(req.body); res.json(server);
manager.save(); });
res.send(server);
},
destroy: function(req, res){ router.put('/:server', function (req, res) {
var server = manager.removeServer(req.params.server); var server = manager.getServer(req.params.server);
res.send(server); server.update(req.body);
}, manager.save();
res.json(server);
});
start: function (req, res){ router.delete('/:server', function (req, res) {
var server = manager.getServer(req.params.server); var server = manager.removeServer(req.params.server);
server.start(); res.json(server);
res.send({status:"ok", pid: server.pid}); });
},
stop: function (req, res){ router.post('/:server/start', function (req, res) {
var server = manager.getServer(req.params.server); var server = manager.getServer(req.params.server);
server.stop(function () { server.start();
if (!server.pid) { res.json({status:"ok", pid: server.pid});
res.send({status: true, pid: server.pid}); });
} else {
res.send({status: false, pid: server.pid}); router.post('/:server/stop', function (req, res) {
} var server = manager.getServer(req.params.server);
}); server.stop(function () {
}, if (!server.pid) {
}; res.json({status: true, pid: server.pid});
} else {
res.json({status: false, pid: server.pid});
}
});
});
return router;
}; };

View File

@ -1,5 +1,11 @@
var config = require('./../config'); var express = require('express');
exports.index = function (req, res){ module.exports = function (config) {
res.send(config); var router = express.Router();
router.get('/', function (req, res) {
res.json(config);
});
return router;
}; };

View File

@ -1,16 +0,0 @@
var path = require('path');
var SteamWorkshop = require('steam-workshop');
var config = require('./../config');
exports.mission = function(req, res){
var outputFolder = path.join(config.path, 'mpmissions');
var steamWorkshop = new SteamWorkshop(outputFolder);
steamWorkshop.downloadFile(req.body.id, function (err, files) {
if (err) {
res.json(500, {success: false});
} else {
res.json({success: true});
}
});
};