Moved more socket logic into socket manager for easier DX
This commit is contained in:
@ -1,8 +1,6 @@
|
||||
import { createServer as httpServer, Server as HTTPServer } from 'http'
|
||||
|
||||
import cors from 'cors'
|
||||
import express, { Application } from 'express'
|
||||
import { Server as SocketServer } from 'socket.io'
|
||||
import cors from 'cors'
|
||||
|
||||
import config from '#application/config'
|
||||
import Database from '#application/database'
|
||||
@ -15,77 +13,45 @@ import SocketManager from '#managers/socketManager'
|
||||
import UserManager from '#managers/userManager'
|
||||
import WeatherManager from '#managers/weatherManager'
|
||||
import ZoneManager from '#managers/zoneManager'
|
||||
import { Authentication } from '#middleware/authentication'
|
||||
|
||||
export class Server {
|
||||
private readonly app: Application
|
||||
private readonly http: HTTPServer
|
||||
private readonly io: SocketServer
|
||||
private readonly logger = Logger.type(LoggerType.APP)
|
||||
|
||||
/**
|
||||
* Creates an instance of GameServer.
|
||||
*/
|
||||
constructor() {
|
||||
this.app = express()
|
||||
this.app.use(
|
||||
cors({
|
||||
origin: config.CLIENT_URL,
|
||||
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'], // Add supported methods
|
||||
allowedHeaders: ['Content-Type', 'Authorization'], // Add allowed headers
|
||||
credentials: true
|
||||
})
|
||||
)
|
||||
this.app.use(cors())
|
||||
this.app.use(express.json())
|
||||
this.app.use(express.urlencoded({ extended: true }))
|
||||
this.http = httpServer(this.app)
|
||||
this.io = new SocketServer(this.http)
|
||||
this.io.use(Authentication)
|
||||
}
|
||||
|
||||
/**
|
||||
* Start the server
|
||||
*/
|
||||
public async start() {
|
||||
// Connect to database
|
||||
public async start(): Promise<void> {
|
||||
try {
|
||||
// Initialize database
|
||||
await Database.initialize()
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Database connection failed: ${error.message}`)
|
||||
process.exit(1) // Exit if database connection fails
|
||||
}
|
||||
|
||||
// Start the server
|
||||
try {
|
||||
// Start HTTP server
|
||||
this.http.listen(config.PORT, config.HOST)
|
||||
this.logger.info(`Socket.IO running on port ${config.PORT}`)
|
||||
this.logger.info(`Server running on port ${config.PORT}`)
|
||||
|
||||
// Initialize managers
|
||||
await Promise.all([
|
||||
HttpManager.boot(this.app),
|
||||
SocketManager.boot(this.app, this.http),
|
||||
QueueManager.boot(),
|
||||
UserManager.boot(),
|
||||
// DateManager.boot(SocketManager.getIO()),
|
||||
// WeatherManager.boot(SocketManager.getIO()),
|
||||
ZoneManager.boot(),
|
||||
ConsoleManager.boot()
|
||||
])
|
||||
|
||||
} catch (error: any) {
|
||||
this.logger.error(`Socket.IO failed to start: ${error.message}`)
|
||||
this.logger.error(`Server failed to start: ${error.message}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Load HTTP manager
|
||||
await HttpManager.boot(this.app)
|
||||
|
||||
// Load queue manager
|
||||
await QueueManager.boot(this.io)
|
||||
|
||||
// Load user manager
|
||||
await UserManager.boot()
|
||||
|
||||
// Load date manager
|
||||
// await DateManager.boot(this.io)
|
||||
|
||||
// Load weather manager
|
||||
// await WeatherManager.boot(this.io)
|
||||
|
||||
// Load zoneEditor manager
|
||||
await ZoneManager.boot()
|
||||
|
||||
// Load console manager
|
||||
await ConsoleManager.boot(this.io)
|
||||
|
||||
// Load socket manager
|
||||
await SocketManager.boot(this.io)
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user