diff --git a/src/managers/httpManager.ts b/src/managers/httpManager.ts index 0a7ce51..460e85a 100644 --- a/src/managers/httpManager.ts +++ b/src/managers/httpManager.ts @@ -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() diff --git a/src/server.ts b/src/server.ts index 8d76d28..a8ed7b0 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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 { 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) }