support event & eventBus initialization

This commit is contained in:
SrGooglo 2024-02-20 17:30:59 +00:00
parent 8a1d03ce6c
commit c14ba37d08

View File

@ -1,6 +1,8 @@
const fs = require("fs") const fs = require("fs")
const path = require("path") const path = require("path")
const rtengine = require("./classes/RTEngine").default const rtengine = require("./classes/rtengine").default
const { EventEmitter } = require("@foxify/events")
const tokenizer = require("corenode/libs/tokenizer") const tokenizer = require("corenode/libs/tokenizer")
const { serverManifest, internalConsole } = require("./lib") const { serverManifest, internalConsole } = require("./lib")
@ -42,11 +44,15 @@ const Engines = {
} }
class Server { class Server {
eventBus = new EventEmitter()
constructor(params = {}, controllers = {}, middlewares = {}, headers = {}) { constructor(params = {}, controllers = {}, middlewares = {}, headers = {}) {
if (global.LINEBRIDGE_SERVER_EXPERIMENTAL) {
console.warn("🚧🚧🚧 This version of Linebridge is experimental!")
}
// register aliases // register aliases
this.params = { this.params = {
minimal: false,
no_brand: false,
...global.DEFAULT_SERVER_PARAMS, ...global.DEFAULT_SERVER_PARAMS,
...params, ...params,
} }
@ -94,8 +100,14 @@ class Server {
} }
initialize = async () => { initialize = async () => {
if (!this.params.minimal) { if (this.events) {
this.InternalConsole.info(`🚀 Starting server...`) if (this.events.default) {
this.events = this.events.default
}
for (const [eventName, eventHandler] of Object.entries(this.events)) {
this.eventBus.on(eventName, eventHandler)
}
} }
// initialize engine // initialize engine
@ -105,35 +117,38 @@ class Server {
requireAuth: this.constructor.requireWSAuth, requireAuth: this.constructor.requireWSAuth,
}) })
// before initialize headers, middlewares and controllers, try to execute onInitialize hook.
if (typeof this.onInitialize === "function") { if (typeof this.onInitialize === "function") {
await this.onInitialize() await this.onInitialize()
} }
//* set server defined headers // set server defined headers
this.initializeHeaders() this.initializeHeaders()
//* set server defined middlewares // set server defined middlewares
this.initializeRequiredMiddlewares() this.initializeRequiredMiddlewares()
//* register controllers // register controllers
await this.initializeControllers() await this.initializeControllers()
//* register main index endpoint `/` // register main index endpoint `/`
await this.registerBaseEndpoints() await this.registerBaseEndpoints()
if (typeof this.engine.ws?.initialize !== "function") { if (typeof this.engine.ws?.initialize !== "function") {
console.warn("❌ WebSocket is not supported!") this.InternalConsole.warn("❌ WebSocket is not supported!")
} else { } else {
await this.engine.ws.initialize() await this.engine.ws.initialize({
redisInstance: this.redis
})
}
if (typeof this.beforeInitialize === "function") {
await this.beforeInitialize()
} }
await this.engine.http.listen(this.params.listen_port) await this.engine.http.listen(this.params.listen_port)
this.InternalConsole.info(`✅ Server ready on => ${this.params.listen_ip}:${this.params.listen_port}`) this.InternalConsole.info(`✅ Server ready on => ${this.params.listen_ip}:${this.params.listen_port}`)
if (!this.params.minimal) {
this.outputServerInfo()
}
} }
initializeManifest = () => { initializeManifest = () => {
@ -322,7 +337,6 @@ class Server {
return execs return execs
} }
cleanupProcess = () => { cleanupProcess = () => {
this.InternalConsole.log("🛑 Stopping server...") this.InternalConsole.log("🛑 Stopping server...")
@ -349,7 +363,7 @@ class Server {
return this.params.onRouteError(req, res, error) return this.params.onRouteError(req, res, error)
} else { } else {
if (!global.silentOutputServerErrors) { if (!global.silentOutputServerErrors) {
console.error({ this.InternalConsole.error({
message: "Unhandled route error:", message: "Unhandled route error:",
description: error.stack, description: error.stack,
ref: [endpoint.method, endpoint.route].join("|"), ref: [endpoint.method, endpoint.route].join("|"),
@ -364,16 +378,6 @@ class Server {
} }
} }
// public methods
outputServerInfo = () => {
this.InternalConsole.table({
"linebridge_version": LINEBRIDGE_SERVER_VERSION,
"engine": this.params.engine,
"address": this.params.http_address,
"listen_port": this.params.listen_port,
})
}
toogleEndpointReachability = (method, route, enabled) => { toogleEndpointReachability = (method, route, enabled) => {
if (typeof this.endpoints_map[method] !== "object") { if (typeof this.endpoints_map[method] !== "object") {
throw new Error(`Cannot toogle endpoint, method [${method}] not set!`) throw new Error(`Cannot toogle endpoint, method [${method}] not set!`)