From a974d1bbf658946d25d0812a1b626236f8ec34e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bjo=CC=88rn=20Dahlgren?= Date: Sat, 1 Apr 2017 11:19:30 +0200 Subject: [PATCH] Update to Express 4 --- app.js | 47 +++++++-------- lib/missions.js | 10 ++++ package.json | 7 ++- public/js/app/views/servers/info.js | 4 +- routes/logs.js | 56 +++++++++--------- routes/missions.js | 89 +++++++++++++++++------------ routes/mods.js | 76 ++++++++++++------------ routes/servers.js | 80 ++++++++++++++------------ routes/settings.js | 12 +++- routes/workshop.js | 16 ------ 10 files changed, 210 insertions(+), 187 deletions(-) delete mode 100644 routes/workshop.js diff --git a/app.js b/app.js index 809cc0d..759ea95 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/lib/missions.js b/lib/missions.js index 035de2e..8eb7095 100644 --- a/lib/missions.js +++ b/lib/missions.js @@ -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; diff --git a/package.json b/package.json index c231aa0..648b545 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/public/js/app/views/servers/info.js b/public/js/app/views/servers/info.js index 440672a..efa111a 100644 --- a/public/js/app/views/servers/info.js +++ b/public/js/app/views/servers/info.js @@ -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(); diff --git a/routes/logs.js b/routes/logs.js index 6cbfcdb..b4a4534 100644 --- a/routes/logs.js +++ b/routes/logs.js @@ -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; }; diff --git a/routes/missions.js b/routes/missions.js index 15bcac0..a417872 100644 --- a/routes/missions.js +++ b/routes/missions.js @@ -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; }; diff --git a/routes/mods.js b/routes/mods.js index 95fe700..51ea7be 100644 --- a/routes/mods.js +++ b/routes/mods.js @@ -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; }; diff --git a/routes/servers.js b/routes/servers.js index b5a028c..22e9399 100644 --- a/routes/servers.js +++ b/routes/servers.js @@ -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; }; diff --git a/routes/settings.js b/routes/settings.js index 10b3f4f..7fd69b7 100644 --- a/routes/settings.js +++ b/routes/settings.js @@ -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; }; diff --git a/routes/workshop.js b/routes/workshop.js deleted file mode 100644 index 7f02ccc..0000000 --- a/routes/workshop.js +++ /dev/null @@ -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}); - } - }); -};