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:
Nick Krecklow
2020-04-21 17:59:53 -05:00
committed by GitHub
parent 9eda8d6bdb
commit 4d13965e6b
19 changed files with 822 additions and 823 deletions

View File

@ -1,32 +1,64 @@
/**
* THIS IS LEGACY, UNMAINTAINED CODE
* IT MAY (AND LIKELY DOES) CONTAIN BUGS
* USAGE IS NOT RECOMMENDED
*/
const http = require('http')
const io = require('socket.io')
const finalHandler = require('finalhandler')
const finalHttpHandler = require('finalhandler')
const serveStatic = require('serve-static')
const io = require('socket.io')
const util = require('./util')
const logger = require('./logger')
const config = require('../config.json')
const distHandler = serveStatic('dist/')
const faviconsHandler = serveStatic('favicons/')
function onRequest (req, res) {
logger.log('info', '%s requested: %s', util.getRemoteAddr(req), req.url)
distHandler(req, res, function () {
faviconsHandler(req, res, finalHandler(req, res))
})
function getRemoteAddr (req) {
return req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.connection.remoteAddress
}
exports.start = function () {
const server = http.createServer(onRequest)
server.listen(config.site.port, config.site.ip)
exports.io = io.listen(server)
logger.log('info', 'Started on %s:%d', config.site.ip, config.site.port)
class Server {
constructor (clientSocketHandler) {
this._clientSocketHandler = clientSocketHandler
this._connectedSockets = 0
this._http = http.createServer(this.handleHttpRequest)
this._distServeStatic = serveStatic('dist/')
this._faviconsServeStatic = serveStatic('favicons/')
}
listen (host, port) {
this._http.listen(port, host)
this._io = io.listen(this._http)
this._io.on('connect', this.handleClientSocket)
logger.log('info', 'Started on %s:%d', host, port)
}
broadcast (event, payload) {
this._io.sockets.emit(event, payload)
}
handleHttpRequest = (req, res) => {
logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url)
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
// If faviconServeStatic fails, pass to finalHttpHandler to terminate
this._distServeStatic(req, res, () => {
this._faviconsServeStatic(req, res, finalHttpHandler(req, res))
})
}
handleClientSocket = (client) => {
this._connectedSockets++
logger.log('info', '%s connected, total clients: %d', getRemoteAddr(client.request), this._connectedSockets)
// Bind disconnect event for logging
client.on('disconnect', () => {
this._connectedSockets--
logger.log('info', '%s disconnected, total clients: %d', getRemoteAddr(client.request), this._connectedSockets)
})
// Pass client off to proxy handler
this._clientSocketHandler(client)
}
}
module.exports = Server