Backend cleanup (#146)
* Add ServerRegistration, begin refactoring to match frontend * move graphData logic into ServerRegistration * move ping updates/history into ServerRegistration * start updating main app entry methods * fix default rates.updateMojangStatus * fix record loading delays on freshly booted instances * move database loading logic to method + callback * use data in frontend for type lookup instead of ping * cleanup app.js * reorganize methods to improve flow * avoid useless mojang updates, remove legacy fields * rename legacy fields for consistency * finish restructure around App model * ensure versions are sorted by release order * filter errors sent to frontend to avoid data leaks * fix version listing behavior on frontend * 5.1.0
This commit is contained in:
92
lib/app.js
Normal file
92
lib/app.js
Normal file
@ -0,0 +1,92 @@
|
||||
const Database = require('./database')
|
||||
const MojangUpdater = require('./mojang')
|
||||
const PingController = require('./ping')
|
||||
const Server = require('./server')
|
||||
|
||||
const config = require('../config')
|
||||
const minecraftVersions = require('../minecraft_versions')
|
||||
|
||||
class App {
|
||||
serverRegistrations = []
|
||||
|
||||
constructor () {
|
||||
this.mojangUpdater = new MojangUpdater(this)
|
||||
this.pingController = new PingController(this)
|
||||
this.server = new Server(this.handleClientConnection)
|
||||
}
|
||||
|
||||
loadDatabase (callback) {
|
||||
this.database = new Database(this)
|
||||
|
||||
// Setup database instance
|
||||
this.database.ensureIndexes()
|
||||
|
||||
this.database.loadGraphPoints(config.graphDuration, () => {
|
||||
this.database.loadRecords(callback)
|
||||
})
|
||||
}
|
||||
|
||||
handleReady () {
|
||||
this.server.listen(config.site.ip, config.site.port)
|
||||
|
||||
// Allow individual modules to manage their own task scheduling
|
||||
this.mojangUpdater.schedule()
|
||||
this.pingController.schedule()
|
||||
}
|
||||
|
||||
handleClientConnection = (client) => {
|
||||
if (config.logToDatabase) {
|
||||
client.on('requestHistoryGraph', () => {
|
||||
// Send historical graphData built from all serverRegistrations
|
||||
const graphData = {}
|
||||
const graphPeaks = {}
|
||||
|
||||
this.serverRegistrations.forEach((serverRegistration) => {
|
||||
graphData[serverRegistration.data.name] = serverRegistration.graphData
|
||||
|
||||
// Send current peak, if any
|
||||
const graphPeak = serverRegistration.getGraphPeak()
|
||||
if (graphPeak) {
|
||||
graphPeaks[serverRegistration.data.name] = graphPeak
|
||||
}
|
||||
})
|
||||
|
||||
// Send current peaks, if any
|
||||
// Emit peaks first since graphData may take a while to receive
|
||||
if (Object.keys(graphPeaks).length > 0) {
|
||||
client.emit('peaks', graphPeaks)
|
||||
}
|
||||
|
||||
client.emit('historyGraph', graphData)
|
||||
})
|
||||
}
|
||||
|
||||
client.emit('setPublicConfig', (() => {
|
||||
// Remap minecraftVersion entries into name values
|
||||
const minecraftVersionNames = {}
|
||||
Object.keys(minecraftVersions).forEach(function (key) {
|
||||
minecraftVersionNames[key] = minecraftVersions[key].map(version => version.name)
|
||||
})
|
||||
|
||||
// Send configuration data for rendering the page
|
||||
return {
|
||||
graphDuration: config.graphDuration,
|
||||
servers: this.serverRegistrations.map(serverRegistration => serverRegistration.data),
|
||||
minecraftVersions: minecraftVersionNames,
|
||||
isGraphVisible: config.logToDatabase
|
||||
}
|
||||
})())
|
||||
|
||||
// Send last Mojang update, if any
|
||||
this.mojangUpdater.sendLastUpdate(client)
|
||||
|
||||
// Send pingHistory of all ServerRegistrations
|
||||
client.emit('add', this.serverRegistrations.map(serverRegistration => serverRegistration.getPingHistory()))
|
||||
|
||||
// Always send last
|
||||
// This tells the frontend to do final processing and render
|
||||
client.emit('syncComplete')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = App
|
Reference in New Issue
Block a user