Moved more of http logic into http manager

This commit is contained in:
Dennis Postma 2025-02-09 02:04:14 +01:00
parent ee080b6987
commit b7748c254f
2 changed files with 30 additions and 26 deletions

View File

@ -1,8 +1,10 @@
import cors from 'cors'
import { createServer as httpServer, Server as HTTPServer } from 'http'
import type { Application } from 'express'
import cors from 'cors'
import express, { type Application } from 'express'
import config from '#application/config'
import Logger, { LoggerType } from '#application/logger.js'
import { AuthController } from '#controllers/auth'
import { AvatarController } from '#controllers/avatar'
import { CacheController } from '#controllers/cache'
@ -12,16 +14,29 @@ import { TexturesController } from '#controllers/textures'
* HTTP manager
*/
class HttpManager {
private readonly app: Application
private readonly http: HTTPServer
private readonly logger = Logger.type(LoggerType.APP)
private readonly authController: AuthController = new AuthController()
private readonly avatarController: AvatarController = new AvatarController()
private readonly texturesController: TexturesController = new TexturesController()
private readonly cacheController: CacheController = new CacheController()
constructor() {
this.app = express()
this.app.use(cors())
this.app.use(express.json())
this.app.use(express.urlencoded({ extended: true }))
this.http = httpServer(this.app)
}
/**
* Initialize HTTP manager
* @param app
*/
public async boot(app: Application) {
this.http.listen(config.PORT, config.HOST)
// Add CORS middleware
app.use(
cors({
@ -34,6 +49,8 @@ class HttpManager {
// Add routes
await this.addRoutes(app)
this.logger.info(`HTTP running on port ${config.PORT}`)
}
private async addRoutes(app: Application) {
@ -58,6 +75,14 @@ class HttpManager {
app.get('/cache/character_types', (req, res) => this.cacheController.characterTypes(req, res))
app.get('/cache/character_hair', (req, res) => this.cacheController.characterHair(req, res))
}
getAppInstance(): Application {
return this.app
}
getHttpInstance(): HTTPServer {
return this.http
}
}
export default new HttpManager()

View File

@ -1,12 +1,4 @@
import 'reflect-metadata'
import { createServer as httpServer, Server as HTTPServer } from 'http'
import cors from 'cors'
import express from 'express'
import type { Application } from 'express'
import config from '#application/config'
import Database from '#application/database'
import Logger, { LoggerType } from '#application/logger'
import ConsoleManager from '#managers/consoleManager'
@ -19,31 +11,17 @@ import UserManager from '#managers/userManager'
import WeatherManager from '#managers/weatherManager'
export class Server {
private readonly app: Application
private readonly http: HTTPServer
private readonly logger = Logger.type(LoggerType.APP)
constructor() {
this.app = express()
this.app.use(cors())
this.app.use(express.json())
this.app.use(express.urlencoded({ extended: true }))
this.http = httpServer(this.app)
}
public async start(): Promise<void> {
try {
// Initialize database
await Database.initialize()
// Start HTTP server
this.http.listen(config.PORT, config.HOST)
this.logger.info(`Server running on port ${config.PORT}`)
// Initialize managers
await Promise.all([
HttpManager.boot(this.app),
SocketManager.boot(this.app, this.http),
HttpManager.boot(HttpManager.getAppInstance()),
SocketManager.boot(HttpManager.getAppInstance(), HttpManager.getHttpInstance()),
QueueManager.boot(),
UserManager.boot(),
MapManager.boot(),
@ -52,6 +30,7 @@ export class Server {
ConsoleManager.boot()
])
} catch (error: any) {
console.error(error)
this.logger.error(`Server failed to start: ${error.message}`)
process.exit(1)
}