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 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 Manager = require('./lib/manager');
@ -12,14 +16,18 @@ var server = require('http').Server(app);
var io = require('socket.io')(server);
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(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(morgan('dev'));
app.use(serveStatic(path.join(__dirname, 'public')));
var logs = new Logs(config);
@ -30,26 +38,11 @@ var missions = new Missions(config);
var mods = new Mods(config);
mods.updateMods();
var logsRoutes = require('./routes/logs')(logs);
var serversRoutes = require('./routes/servers')(manager, mods);
var missionsRoutes = require('./routes/missions')(missions);
var modsRoutes = require('./routes/mods')(mods);
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');
});
app.use('/api/logs', require('./routes/logs')(logs));
app.use('/api/missions', require('./routes/missions')(missions));
app.use('/api/mods', require('./routes/mods')(mods));
app.use('/api/servers', require('./routes/servers')(manager, mods));
app.use('/api/settings', require('./routes/settings')(config));
io.on('connection', function (socket) {
socket.emit('mods', mods.mods);

View File

@ -2,9 +2,11 @@ var async = require('async');
var filesize = require('filesize');
var fs = require('fs');
var path = require('path');
var SteamWorkshop = require('steam-workshop');
var Missions = function (config) {
this.config = config;
this.steamWorkshop = new SteamWorkshop(this.missionsPath());
};
Missions.prototype.missionsPath = function() {
@ -55,4 +57,12 @@ Missions.prototype.delete = function (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;

View File

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

View File

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

View File

@ -1,31 +1,33 @@
module.exports = function (logsManager) {
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;
}
var express = require('express');
logsManager.getLogFile(requestedFilename, function (err, file) {
if (err) {
res.send(err);
module.exports = function (logsManager) {
var router = express.Router();
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 {
if (file) {
res.download(file.path);
} else {
res.send(404, new Error("File not found"));
}
res.status(404).send(new Error("File not found"));
}
});
},
};
}
});
});
return router;
};

View File

@ -1,41 +1,58 @@
var express = require('express');
module.exports = function (missionsManager) {
return {
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;
}
var router = express.Router();
res.download(missionsManager.missionPath(encodeURI(filename)), decodeURI(filename));
},
destroy: function(req, res){
var filename = req.params.mission;
if (req.params.format) {
filename += '.' + req.params.format;
router.get('/', function (req, res) {
missionsManager.list(function (err, missions) {
if (err) {
res.status(500).send(err);
} else {
res.json(missions);
}
});
});
missionsManager.delete(filename, function (err) {
if (err) {
res.json(500, {success: false});
} else {
res.json({success: true});
}
});
},
};
router.post('/', function (req, res) {
var missionFile = req.files.mission;
missionsManager.handleUpload(missionFile, function (err) {
if (err) {
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) {
return {
index: function(req, res){
res.send(modsManager.mods);
},
var router = express.Router();
create: function(req, res){
modsManager.download(req.body.name);
res.send(204, {});
},
router.get('/', function (req, res) {
res.send(modsManager.mods);
});
update: function(req, res){
modsManager.download(req.params.mod);
res.send(204, {});
},
router.post('/', function (req, res) {
modsManager.download(req.body.name);
res.status(204);
});
destroy: function(req, res){
modsManager.delete(req.params.mod, function (err) {
if (err) {
res.send(500, err);
} else {
res.send(204, {});
}
});
},
router.put('/:mod', function (req, res) {
modsManager.download(req.params.mod);
res.status(204);
});
refresh: function(req, res){
modsManager.updateMods();
res.send(200, {});
},
router.delete('/:mod', function (req, res) {
modsManager.delete(req.params.mod, function (err) {
if (err) {
res.status(500).send(err);
} else {
res.status(204);
}
});
});
search: function(req, res){
var query = req.body.query || "";
modsManager.search(query, function (err, mods) {
if (err || !mods) {
res.send(500, err);
} else {
res.send(mods);
}
});
}
};
router.post('/refresh', function (req, res) {
modsManager.updateMods();
res.status(204);
});
router.post('/search', function (req, res) {
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) {
return {
index: function (req, res){
res.send(manager.getServers());
},
var router = express.Router();
create: function (req, res) {
var server = manager.addServer(req.body);
res.send(server);
},
router.get('/', function (req, res) {
res.json(manager.getServers());
});
show: function (req, res){
var server = manager.getServer(req.params.server);
res.send(server);
},
router.post('/', function (req, res) {
var server = manager.addServer(req.body);
res.json(server);
});
update: function(req, res){
var server = manager.getServer(req.params.server);
server.update(req.body);
manager.save();
res.send(server);
},
router.get('/:server', function (req, res) {
var server = manager.getServer(req.params.server);
res.json(server);
});
destroy: function(req, res){
var server = manager.removeServer(req.params.server);
res.send(server);
},
router.put('/:server', function (req, res) {
var server = manager.getServer(req.params.server);
server.update(req.body);
manager.save();
res.json(server);
});
start: function (req, res){
var server = manager.getServer(req.params.server);
server.start();
res.send({status:"ok", pid: server.pid});
},
router.delete('/:server', function (req, res) {
var server = manager.removeServer(req.params.server);
res.json(server);
});
stop: function (req, res){
var server = manager.getServer(req.params.server);
server.stop(function () {
if (!server.pid) {
res.send({status: true, pid: server.pid});
} else {
res.send({status: false, pid: server.pid});
}
});
},
};
router.post('/:server/start', function (req, res) {
var server = manager.getServer(req.params.server);
server.start();
res.json({status:"ok", 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){
res.send(config);
module.exports = function (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});
}
});
};