forked from noxious/server
Moved more of http logic into http manager
This commit is contained in:
parent
ee080b6987
commit
b7748c254f
@ -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 config from '#application/config'
|
||||||
|
import Logger, { LoggerType } from '#application/logger.js'
|
||||||
import { AuthController } from '#controllers/auth'
|
import { AuthController } from '#controllers/auth'
|
||||||
import { AvatarController } from '#controllers/avatar'
|
import { AvatarController } from '#controllers/avatar'
|
||||||
import { CacheController } from '#controllers/cache'
|
import { CacheController } from '#controllers/cache'
|
||||||
@ -12,16 +14,29 @@ import { TexturesController } from '#controllers/textures'
|
|||||||
* HTTP manager
|
* HTTP manager
|
||||||
*/
|
*/
|
||||||
class HttpManager {
|
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 authController: AuthController = new AuthController()
|
||||||
private readonly avatarController: AvatarController = new AvatarController()
|
private readonly avatarController: AvatarController = new AvatarController()
|
||||||
private readonly texturesController: TexturesController = new TexturesController()
|
private readonly texturesController: TexturesController = new TexturesController()
|
||||||
private readonly cacheController: CacheController = new CacheController()
|
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
|
* Initialize HTTP manager
|
||||||
* @param app
|
* @param app
|
||||||
*/
|
*/
|
||||||
public async boot(app: Application) {
|
public async boot(app: Application) {
|
||||||
|
this.http.listen(config.PORT, config.HOST)
|
||||||
|
|
||||||
// Add CORS middleware
|
// Add CORS middleware
|
||||||
app.use(
|
app.use(
|
||||||
cors({
|
cors({
|
||||||
@ -34,6 +49,8 @@ class HttpManager {
|
|||||||
|
|
||||||
// Add routes
|
// Add routes
|
||||||
await this.addRoutes(app)
|
await this.addRoutes(app)
|
||||||
|
|
||||||
|
this.logger.info(`HTTP running on port ${config.PORT}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
private async addRoutes(app: Application) {
|
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_types', (req, res) => this.cacheController.characterTypes(req, res))
|
||||||
app.get('/cache/character_hair', (req, res) => this.cacheController.characterHair(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()
|
export default new HttpManager()
|
||||||
|
@ -1,12 +1,4 @@
|
|||||||
import 'reflect-metadata'
|
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 Database from '#application/database'
|
||||||
import Logger, { LoggerType } from '#application/logger'
|
import Logger, { LoggerType } from '#application/logger'
|
||||||
import ConsoleManager from '#managers/consoleManager'
|
import ConsoleManager from '#managers/consoleManager'
|
||||||
@ -19,31 +11,17 @@ import UserManager from '#managers/userManager'
|
|||||||
import WeatherManager from '#managers/weatherManager'
|
import WeatherManager from '#managers/weatherManager'
|
||||||
|
|
||||||
export class Server {
|
export class Server {
|
||||||
private readonly app: Application
|
|
||||||
private readonly http: HTTPServer
|
|
||||||
private readonly logger = Logger.type(LoggerType.APP)
|
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> {
|
public async start(): Promise<void> {
|
||||||
try {
|
try {
|
||||||
// Initialize database
|
// Initialize database
|
||||||
await Database.initialize()
|
await Database.initialize()
|
||||||
|
|
||||||
// Start HTTP server
|
|
||||||
this.http.listen(config.PORT, config.HOST)
|
|
||||||
this.logger.info(`Server running on port ${config.PORT}`)
|
|
||||||
|
|
||||||
// Initialize managers
|
// Initialize managers
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
HttpManager.boot(this.app),
|
HttpManager.boot(HttpManager.getAppInstance()),
|
||||||
SocketManager.boot(this.app, this.http),
|
SocketManager.boot(HttpManager.getAppInstance(), HttpManager.getHttpInstance()),
|
||||||
QueueManager.boot(),
|
QueueManager.boot(),
|
||||||
UserManager.boot(),
|
UserManager.boot(),
|
||||||
MapManager.boot(),
|
MapManager.boot(),
|
||||||
@ -52,6 +30,7 @@ export class Server {
|
|||||||
ConsoleManager.boot()
|
ConsoleManager.boot()
|
||||||
])
|
])
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
console.error(error)
|
||||||
this.logger.error(`Server failed to start: ${error.message}`)
|
this.logger.error(`Server failed to start: ${error.message}`)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user