Skip to content

Commit

Permalink
Configurable base url
Browse files Browse the repository at this point in the history
  • Loading branch information
Dahlgren committed Dec 29, 2020
1 parent 239ce6f commit a53d2dc
Show file tree
Hide file tree
Showing 21 changed files with 69 additions and 29 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
24 changes: 16 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand Down
1 change: 1 addition & 0 deletions config.js.example
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 3 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<base href="/" />
<meta charset="utf-8">
<title>Arma Server Admin</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="/favicon.ico" />
<link rel="shortcut icon" href="favicon.ico" />

<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

<script src="/socket.io/socket.io.js"></script>
<script src="socket.io/socket.io.js"></script>
<script src="bundle.js"></script>
</head>
<body></body>
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/collections/logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ var Log = require('app/models/log')
module.exports = Backbone.Collection.extend({
comparator: 'name',
model: Log,
url: '/api/logs/'
url: 'api/logs/'
})
2 changes: 1 addition & 1 deletion public/js/app/collections/missions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
})
2 changes: 1 addition & 1 deletion public/js/app/collections/mods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
})
2 changes: 1 addition & 1 deletion public/js/app/collections/servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/'
})
2 changes: 1 addition & 1 deletion public/js/app/models/mission.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({
name: ''
},
idAttribute: 'name',
urlRoot: '/api/missions/'
urlRoot: 'api/missions/'
})
2 changes: 1 addition & 1 deletion public/js/app/models/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({
name: ''
},
idAttribute: 'name',
urlRoot: '/api/mods/'
urlRoot: 'api/mods/'
})
6 changes: 3 additions & 3 deletions public/js/app/models/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ module.exports = Backbone.Model.extend({
path: '',
type: ''
},
urlRoot: '/api/settings'
urlRoot: 'api/settings'
})
4 changes: 3 additions & 1 deletion public/js/app/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/views/missions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
2 changes: 1 addition & 1 deletion public/js/app/views/missions/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/views/missions/workshop.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion public/js/app/views/mods/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down
4 changes: 2 additions & 2 deletions public/js/tpl/logs/list_item.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<td>
<% if (size < 100 * 1024 * 1024) { %>
<!-- Only show files under 100 MB -->
<a href='/api/logs/<%-name%>/view' target=_blank><%-name%></a>
<a href='api/logs/<%-name%>/view' target=_blank><%-name%></a>
<% } else { %>
<%-name%>
<% } %>
Expand All @@ -10,7 +10,7 @@
<%-formattedSize%>
</td>
<td>
<a class="btn btn-primary btn-xs" href="/api/logs/<%-name%>/download">
<a class="btn btn-primary btn-xs" href="api/logs/<%-name%>/download">
<span class="glyphicon glyphicon-download"></span>
Download
</a>
Expand Down
2 changes: 1 addition & 1 deletion public/js/tpl/missions/list_item.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<td style="width: 100%;">
<a href='/api/missions/<%-encodeURI(name)%>'><%-missionName%></a>
<a href='api/missions/<%-encodeURI(name)%>'><%-missionName%></a>
</td>
<td><%-worldName%></td>
<td class="text-nowrap"><%-sizeFormatted%></td>
Expand Down
23 changes: 23 additions & 0 deletions routes/main.js
Original file line number Diff line number Diff line change
@@ -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('<base href="/" />', '<base href="' + baseUrl + '" />')

return res.send(data)
})
})

return router
}
6 changes: 5 additions & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
@@ -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'),
Expand All @@ -13,7 +17,7 @@ module.exports = {
filename: 'bundle.js',

// Path to use in HTML
publicPath: '/'
publicPath: baseUrl
},

resolve: {
Expand Down

0 comments on commit a53d2dc

Please sign in to comment.