Many many more improvements

This commit is contained in:
2024-12-28 19:21:15 +01:00
parent bd3bf6f580
commit 918f5141fc
27 changed files with 178 additions and 191 deletions

View File

@ -7,7 +7,7 @@ import { Server as SocketServer } from 'socket.io'
import config from '#application/config'
import Database from '#application/database'
import { appLogger, watchLogs } from '#application/logger'
import { Logger } from '#application/logger'
import { getAppPath } from '#application/storage'
import { TSocket } from '#application/types'
import { HttpRouter } from '#http/router'
@ -23,6 +23,7 @@ export class Server {
private readonly app: Application
private readonly http: HTTPServer
private readonly io: SocketServer
private readonly logger: Logger = new Logger('app')
/**
* Creates an instance of GameServer.
@ -49,22 +50,22 @@ export class Server {
*/
public async start() {
// Read log file and print to console for debugging
watchLogs()
Logger.watch()
// Connect to database
try {
await Database.initialize()
} catch (error: any) {
appLogger.error(`Database connection failed: ${error.message}`)
this.logger.error(`Database connection failed: ${error.message}`)
process.exit(1) // Exit if database connection fails
}
// Start the server
try {
this.http.listen(config.PORT, config.HOST)
appLogger.info(`Socket.IO running on port ${config.PORT}`)
this.logger.info(`Socket.IO running on port ${config.PORT}`)
} catch (error: any) {
appLogger.error(`Socket.IO failed to start: ${error.message}`)
this.logger.error(`Socket.IO failed to start: ${error.message}`)
}
// Load HTTP routes
@ -102,7 +103,7 @@ export class Server {
try {
await this.loadEventHandlers('socketEvents', '', socket)
} catch (error: any) {
appLogger.error(`Failed to load event handlers: ${error.message}`)
this.logger.error(`Failed to load event handlers: ${error.message}`)
}
}
@ -126,7 +127,7 @@ export class Server {
try {
const module = await import(filePath)
if (typeof module.default !== 'function') {
appLogger.warn(`Unrecognized export in ${file.name}`)
this.logger.warn(`Unrecognized export in ${file.name}`)
continue
}
@ -134,11 +135,11 @@ export class Server {
const eventInstance = new EventClass(this.io, socket)
eventInstance.listen()
} catch (error) {
appLogger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Error loading event handler ${file.name}: ${error instanceof Error ? error.message : String(error)}`)
}
}
} catch (error) {
appLogger.error(`Error reading directory: ${error instanceof Error ? error.message : String(error)}`)
this.logger.error(`Error reading directory: ${error instanceof Error ? error.message : String(error)}`)
}
}
}