From 20cbf5059656592b197d8e5e19a48104d8585fe9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Dahlgren?= Date: Sat, 10 Jul 2021 15:54:55 +0200 Subject: [PATCH] Configurable base url --- README.md | 1 + app.js | 24 ++++++++++++++++-------- config.js.example | 1 + public/index.html | 5 +++-- public/js/app/collections/logs.js | 2 +- public/js/app/collections/missions.js | 2 +- public/js/app/collections/mods.js | 2 +- public/js/app/collections/servers.js | 2 +- public/js/app/models/mission.js | 2 +- public/js/app/models/mod.js | 2 +- public/js/app/models/server.js | 6 +++--- public/js/app/models/settings.js | 2 +- public/js/app/router.js | 4 +++- public/js/app/views/missions/index.js | 2 +- public/js/app/views/missions/upload.js | 2 +- public/js/app/views/missions/workshop.js | 2 +- public/js/app/views/mods/list.js | 2 +- public/js/tpl/logs/list_item.html | 4 ++-- public/js/tpl/missions/list_item.html | 2 +- routes/main.js | 23 +++++++++++++++++++++++ webpack.config.js | 6 +++++- 21 files changed, 69 insertions(+), 29 deletions(-) create mode 100644 routes/main.js diff --git a/README.md b/README.md index a6463c2..f765e67 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ Key | Description --- | --- game | Which game server to launch, see above path | Folder path to game server +baseUrl | URL path used to serve the application, default is '/'. Must end with `/` port | Web port to use host | IP or Hostname to listen on type | Which kind of server to use, can be 'linux', 'windows' or 'wine' diff --git a/app.js b/app.js index e8ddd73..3ceb4c9 100644 --- a/app.js +++ b/app.js @@ -17,7 +17,6 @@ var Settings = require('./lib/settings') var app = express() var server = require('http').Server(app) -var io = require('socket.io')(server) setupBasicAuth(config, app) @@ -27,8 +26,6 @@ app.use(bodyParser.urlencoded({ extended: false })) morgan.token('user', function (req) { return req.auth ? req.auth.user : 'anon' }) app.use(morgan(config.logFormat || 'dev')) -app.use(serveStatic(path.join(__dirname, 'public'))) - var logs = new Logs(config) var manager = new Manager(config, logs) @@ -40,11 +37,22 @@ mods.updateMods() var settings = new Settings(config) -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')(settings)) +var baseUrl = config.baseUrl || '/' +var router = express.Router() + +router.use('/api/logs', require('./routes/logs')(logs)) +router.use('/api/missions', require('./routes/missions')(missions)) +router.use('/api/mods', require('./routes/mods')(mods)) +router.use('/api/servers', require('./routes/servers')(manager, mods)) +router.use('/api/settings', require('./routes/settings')(settings)) +router.use('/', require('./routes/main')(baseUrl)) +router.use(serveStatic(path.join(__dirname, 'public'))) + +app.use(baseUrl, router) + +var io = require('socket.io')(server, { + path: baseUrl + 'socket.io' +}) io.on('connection', function (socket) { socket.emit('missions', missions.missions) diff --git a/config.js.example b/config.js.example index 2731db8..2a8a22b 100644 --- a/config.js.example +++ b/config.js.example @@ -1,6 +1,7 @@ module.exports = { game: 'arma3', // arma3, arma2oa, arma2, arma1, cwa, ofpresistance, ofp path: 'path-to-arma3-directory', + baseUrl: '/', port: 3000, host: '0.0.0.0', // Can be either an IP or a Hostname type: 'linux', // Can be either linux, windows or wine diff --git a/public/index.html b/public/index.html index 7673fc9..a37ca65 100644 --- a/public/index.html +++ b/public/index.html @@ -1,17 +1,18 @@ + Arma Server Admin - + - + diff --git a/public/js/app/collections/logs.js b/public/js/app/collections/logs.js index a5b5170..c1c0ab8 100644 --- a/public/js/app/collections/logs.js +++ b/public/js/app/collections/logs.js @@ -7,5 +7,5 @@ module.exports = Backbone.Collection.extend({ return b.get('created').localeCompare(a.get('created')) // Descending order }, model: Log, - url: '/api/logs/' + url: 'api/logs/' }) diff --git a/public/js/app/collections/missions.js b/public/js/app/collections/missions.js index bf4b05c..f96625a 100644 --- a/public/js/app/collections/missions.js +++ b/public/js/app/collections/missions.js @@ -7,5 +7,5 @@ module.exports = Backbone.Collection.extend({ return a.get('name').toLowerCase().localeCompare(b.get('name').toLowerCase()) }, model: Mission, - url: '/api/missions/' + url: 'api/missions/' }) diff --git a/public/js/app/collections/mods.js b/public/js/app/collections/mods.js index 57d0cf3..3507d16 100644 --- a/public/js/app/collections/mods.js +++ b/public/js/app/collections/mods.js @@ -7,5 +7,5 @@ module.exports = Backbone.Collection.extend({ return a.get('name').toLowerCase().localeCompare(b.get('name').toLowerCase()) }, model: Mod, - url: '/api/mods/' + url: 'api/mods/' }) diff --git a/public/js/app/collections/servers.js b/public/js/app/collections/servers.js index 894116d..7941910 100644 --- a/public/js/app/collections/servers.js +++ b/public/js/app/collections/servers.js @@ -7,5 +7,5 @@ module.exports = Backbone.Collection.extend({ return a.get('title').toLowerCase().localeCompare(b.get('title').toLowerCase()) }, model: Server, - url: '/api/servers/' + url: 'api/servers/' }) diff --git a/public/js/app/models/mission.js b/public/js/app/models/mission.js index 36fb26c..2eb9731 100644 --- a/public/js/app/models/mission.js +++ b/public/js/app/models/mission.js @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({ name: '' }, idAttribute: 'name', - urlRoot: '/api/missions/' + urlRoot: 'api/missions/' }) diff --git a/public/js/app/models/mod.js b/public/js/app/models/mod.js index f5a278f..02b318d 100644 --- a/public/js/app/models/mod.js +++ b/public/js/app/models/mod.js @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({ name: '' }, idAttribute: 'name', - urlRoot: '/api/mods/' + urlRoot: 'api/mods/' }) diff --git a/public/js/app/models/server.js b/public/js/app/models/server.js index 7d13dda..306b687 100644 --- a/public/js/app/models/server.js +++ b/public/js/app/models/server.js @@ -23,11 +23,11 @@ module.exports = Backbone.Model.extend({ von: false, verify_signatures: false }, - urlRoot: '/api/servers/', + urlRoot: 'api/servers/', start: function (cb) { var self = this $.ajax({ - url: '/api/servers/' + self.get('id') + '/start', + url: 'api/servers/' + self.get('id') + '/start', type: 'POST', success: function (resp) { self.set('pid', resp.pid) @@ -47,7 +47,7 @@ module.exports = Backbone.Model.extend({ stop: function (cb) { var self = this $.ajax({ - url: '/api/servers/' + self.get('id') + '/stop', + url: 'api/servers/' + self.get('id') + '/stop', type: 'POST', success: function (resp) { self.set('pid', resp.pid) diff --git a/public/js/app/models/settings.js b/public/js/app/models/settings.js index 9364119..ffd1f77 100644 --- a/public/js/app/models/settings.js +++ b/public/js/app/models/settings.js @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({ path: '', type: '' }, - urlRoot: '/api/settings' + urlRoot: 'api/settings' }) diff --git a/public/js/app/router.js b/public/js/app/router.js index e3647db..2f24041 100644 --- a/public/js/app/router.js +++ b/public/js/app/router.js @@ -37,7 +37,9 @@ module.exports = Backbone.Router.extend({ var initialized = false /* global io */ - var socket = io.connect() + var socket = io({ + path: window.location.pathname + 'socket.io' + }) socket.on('missions', function (_missions) { missions.set(_missions) }) diff --git a/public/js/app/views/missions/index.js b/public/js/app/views/missions/index.js index f23f3c2..94d0ded 100644 --- a/public/js/app/views/missions/index.js +++ b/public/js/app/views/missions/index.js @@ -45,7 +45,7 @@ module.exports = Marionette.LayoutView.extend({ refresh: function (event) { event.preventDefault() $.ajax({ - url: '/api/missions/refresh', + url: 'api/missions/refresh', type: 'POST', success: function (resp) { diff --git a/public/js/app/views/missions/upload.js b/public/js/app/views/missions/upload.js index 5300b0e..9bafe32 100644 --- a/public/js/app/views/missions/upload.js +++ b/public/js/app/views/missions/upload.js @@ -21,7 +21,7 @@ module.exports = Marionette.ItemView.extend({ var laddaBtn = Ladda.create($uploadBtn.get(0)) laddaBtn.start() - $.ajax('/api/missions', { + $.ajax('api/missions', { success: function (data) { laddaBtn.stop() self.render() diff --git a/public/js/app/views/missions/workshop.js b/public/js/app/views/missions/workshop.js index 10f8db8..71dc8f9 100644 --- a/public/js/app/views/missions/workshop.js +++ b/public/js/app/views/missions/workshop.js @@ -25,7 +25,7 @@ module.exports = Marionette.ItemView.extend({ laddaBtn.start() $.ajax({ - url: '/api/missions/workshop', + url: 'api/missions/workshop', type: 'POST', data: { id: $form.find('input.workshop').val() diff --git a/public/js/app/views/mods/list.js b/public/js/app/views/mods/list.js index a752ce3..2c09b78 100644 --- a/public/js/app/views/mods/list.js +++ b/public/js/app/views/mods/list.js @@ -19,7 +19,7 @@ module.exports = Marionette.CompositeView.extend({ refresh: function (event) { event.preventDefault() $.ajax({ - url: '/api/mods/refresh', + url: 'api/mods/refresh', type: 'POST', success: function (resp) { diff --git a/public/js/tpl/logs/list_item.html b/public/js/tpl/logs/list_item.html index 576b923..a26a9b7 100644 --- a/public/js/tpl/logs/list_item.html +++ b/public/js/tpl/logs/list_item.html @@ -1,7 +1,7 @@ <% if (size < 100 * 1024 * 1024) { %> - <%-name%> + <%-name%> <% } else { %> <%-name%> <% } %> @@ -16,7 +16,7 @@ <%-formattedSize%> - + Download diff --git a/public/js/tpl/missions/list_item.html b/public/js/tpl/missions/list_item.html index adcc36c..1e1c173 100644 --- a/public/js/tpl/missions/list_item.html +++ b/public/js/tpl/missions/list_item.html @@ -1,5 +1,5 @@ - <%-missionName%> + <%-missionName%> <%-worldName%> <%-sizeFormatted%> diff --git a/routes/main.js b/routes/main.js new file mode 100644 index 0000000..7eca4b5 --- /dev/null +++ b/routes/main.js @@ -0,0 +1,23 @@ +var express = require('express') +var fs = require('fs') +var path = require('path') + +var indexPath = path.join(__dirname, '..', 'public', 'index.html') + +module.exports = function (baseUrl) { + var router = express.Router() + + router.get('/', function (req, res) { + fs.readFile(indexPath, 'utf-8', function (err, data) { + if (err) { + return res.status(404).send() + } + + data = data.replace('', '') + + return res.send(data) + }) + }) + + return router +} diff --git a/webpack.config.js b/webpack.config.js index 500f26c..dd17611 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,6 +1,10 @@ var path = require('path') var webpack = require('webpack') +var config = require('./config') + +var baseUrl = config.baseUrl || '/' + module.exports = { // Entry point for static analyzer entry: path.join(__dirname, 'public', 'js', 'app.js'), @@ -13,7 +17,7 @@ module.exports = { filename: 'bundle.js', // Path to use in HTML - publicPath: '/' + publicPath: baseUrl }, resolve: {