New method for events + TP work

This commit is contained in:
2024-08-26 19:09:35 +02:00
parent ea9639b440
commit e8d100e063
15 changed files with 282 additions and 190 deletions

View File

@ -37,6 +37,18 @@ export class Server {
* Start the server
*/
public async start() {
// Read log file and print to console for debugging
const logFile = path.join(__dirname, '../logs/app.log')
fs.watchFile(logFile, (curr, prev) => {
if (curr.size > prev.size) {
const stream = fs.createReadStream(logFile, { start: prev.size, end: curr.size })
stream.on('data', (chunk) => {
console.log(chunk.toString())
})
}
})
// Check prisma connection
try {
await prisma.$connect()
@ -91,9 +103,25 @@ export class Server {
if (file.isDirectory()) {
await this.loadEventHandlers(fullPath, socket)
} else if (file.isFile()) {
const module = await import(fullPath)
module.default(socket, this.io)
} else if (file.isFile() && file.name.endsWith('.ts')) {
try {
const module = await import(fullPath)
if (typeof module.default === 'function') {
if (module.default.prototype && module.default.prototype.listen) {
// This is a class-based event
const EventClass = module.default
const eventInstance = new EventClass(this.io, socket)
eventInstance.listen()
} else {
// This is a function-based event
module.default(socket, this.io)
}
} else {
logger.warn(`Unrecognized export in ${file.name}`)
}
} catch (error: any) {
logger.error(`Error loading event handler ${file.name}: ${error.message}`)
}
}
}
}