2017-08-24 09:15:06 +00:00
|
|
|
var express = require('express')
|
|
|
|
var bodyParser = require('body-parser')
|
|
|
|
var morgan = require('morgan')
|
|
|
|
var path = require('path')
|
2017-04-01 09:19:30 +00:00
|
|
|
var serveStatic = require('serve-static')
|
2017-10-06 11:20:06 +00:00
|
|
|
var webpack = require('webpack')
|
|
|
|
var webpackMiddleware = require('webpack-dev-middleware')
|
2014-02-09 03:19:48 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var config = require('./config')
|
2017-10-06 11:20:06 +00:00
|
|
|
var webpackConfig = require('./webpack.config')
|
2017-09-30 12:13:59 +00:00
|
|
|
var setupBasicAuth = require('./lib/setup-basic-auth')
|
2017-08-24 09:15:06 +00:00
|
|
|
var Manager = require('./lib/manager')
|
|
|
|
var Missions = require('./lib/missions')
|
|
|
|
var Mods = require('./lib/mods')
|
|
|
|
var Logs = require('./lib/logs')
|
2018-08-18 10:31:41 +00:00
|
|
|
var Settings = require('./lib/settings')
|
2014-02-09 03:19:48 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var app = express()
|
|
|
|
var server = require('http').Server(app)
|
|
|
|
var io = require('socket.io')(server)
|
2014-02-09 03:19:48 +00:00
|
|
|
|
2017-09-30 12:13:59 +00:00
|
|
|
setupBasicAuth(config, app)
|
2015-05-30 00:29:48 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
app.use(bodyParser.json())
|
|
|
|
app.use(bodyParser.urlencoded({ extended: false }))
|
2017-09-30 13:39:44 +00:00
|
|
|
|
|
|
|
morgan.token('user', function (req) { return req.auth ? req.auth.user : 'anon' })
|
2017-09-30 12:43:42 +00:00
|
|
|
app.use(morgan(config.logFormat || 'dev'))
|
2017-09-30 13:39:44 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
app.use(serveStatic(path.join(__dirname, 'public')))
|
2014-02-09 03:19:48 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var logs = new Logs(config)
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var manager = new Manager(config, logs)
|
|
|
|
manager.load()
|
2015-10-31 14:27:16 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
var missions = new Missions(config)
|
|
|
|
var mods = new Mods(config)
|
|
|
|
mods.updateMods()
|
2015-01-21 03:00:09 +00:00
|
|
|
|
2018-08-18 10:31:41 +00:00
|
|
|
var settings = new Settings(config)
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
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))
|
2018-08-18 10:31:41 +00:00
|
|
|
app.use('/api/settings', require('./routes/settings')(settings))
|
2014-02-09 03:19:48 +00:00
|
|
|
|
2014-06-04 22:19:54 +00:00
|
|
|
io.on('connection', function (socket) {
|
2018-08-18 11:04:33 +00:00
|
|
|
socket.emit('missions', missions.missions)
|
2017-08-24 09:15:06 +00:00
|
|
|
socket.emit('mods', mods.mods)
|
|
|
|
socket.emit('servers', manager.getServers())
|
2018-08-18 10:31:41 +00:00
|
|
|
socket.emit('settings', settings.getPublicSettings())
|
2017-08-24 09:15:06 +00:00
|
|
|
})
|
2014-06-04 22:19:54 +00:00
|
|
|
|
2018-08-18 11:04:33 +00:00
|
|
|
missions.on('missions', function (missions) {
|
|
|
|
io.emit('missions', missions)
|
|
|
|
})
|
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
mods.on('mods', function (mods) {
|
|
|
|
io.emit('mods', mods)
|
|
|
|
})
|
2015-02-01 04:27:12 +00:00
|
|
|
|
2017-08-24 09:15:06 +00:00
|
|
|
manager.on('servers', function () {
|
|
|
|
io.emit('servers', manager.getServers())
|
|
|
|
})
|
2014-06-04 22:19:54 +00:00
|
|
|
|
2018-12-02 22:06:48 +00:00
|
|
|
if (require.main === module) {
|
|
|
|
var webpackCompiler = webpack(webpackConfig)
|
|
|
|
|
|
|
|
app.use(webpackMiddleware(webpackCompiler, {
|
|
|
|
publicPath: webpackConfig.output.publicPath
|
|
|
|
}))
|
|
|
|
|
|
|
|
server.listen(config.port, config.host)
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = app
|