diff --git a/src/server/index.js b/src/server/index.js index ccc1db1..80835db 100755 --- a/src/server/index.js +++ b/src/server/index.js @@ -94,23 +94,16 @@ class Server { } initialize = async () => { - this.httpInterface.use((req, res, next) => { - Object.keys(this.headers).forEach((key) => { - res.setHeader(key, this.headers[key]) - }) + //* set server defined headers + this.initializeHeaders() - next() - }) - - const useMiddlewares = [...defaultMiddlewares, ...(this.params.middlewares ?? [])] - - useMiddlewares.forEach((middleware) => { - if (typeof middleware === "function") { - this.httpInterface.use(middleware) - } - }) + //* set server defined middlewares + this.initializeMiddlewares() + //* register main index endpoint `/` await this.registerBaseEndpoints() + + //* register controllers await this.initializeControllers() // initialize socket.io @@ -129,41 +122,22 @@ class Server { process.on("exit", this.cleanupProcess) } - handleWSClientConnection = async (socket) => { - socket.res = (...args) => { - socket.emit("response", ...args) - } - socket.err = (...args) => { - socket.emit("responseError", ...args) - } - - if (typeof this.params.onWSClientConnection === "function") { - await this.params.onWSClientConnection(socket) - } - - for await (const [nsp, on, dispatch] of this.wsInterface.eventsChannels) { - socket.on(on, async (...args) => { - try { - await dispatch(socket, ...args).catch((error) => { - socket.err({ - message: error.message, - }) - }) - } catch (error) { - socket.err({ - message: error.message, - }) - } + initializeHeaders = () => { + this.httpInterface.use((req, res, next) => { + Object.keys(this.headers).forEach((key) => { + res.setHeader(key, this.headers[key]) }) - } - socket.on("ping", () => { - socket.emit("pong") + next() }) + } - socket.on("disconnect", async () => { - if (typeof this.params.onWSClientDisconnect === "function") { - await this.params.onWSClientDisconnect(socket) + initializeMiddlewares = () => { + const useMiddlewares = [...defaultMiddlewares, ...(this.params.middlewares ?? [])] + + useMiddlewares.forEach((middleware) => { + if (typeof middleware === "function") { + this.httpInterface.use(middleware) } }) } @@ -249,6 +223,27 @@ class Server { } } + registerBaseEndpoints() { + //* register main index endpoint `/` + // this is the default endpoint, should return the server info and the map of all endpoints (http & ws) + this.registerHTTPEndpoint({ + method: "get", + route: "/", + fn: (req, res) => { + return res.json({ + LINEBRIDGE_SERVER_VERSION: LINEBRIDGE_SERVER_VERSION, + id: this.id, + usid: this.usid, + oskid: this.oskid, + requestTime: new Date().getTime(), + endpointsMap: this.endpointsMap, + wsEndpointsMap: this.wsInterface.map, + }) + } + }) + } + + //* resolvers resolveMiddlewares = (middlewares) => { middlewares = Array.isArray(middlewares) ? middlewares : [middlewares] const middlewaresArray = [] @@ -270,24 +265,54 @@ class Server { return middlewaresArray } - registerBaseEndpoints() { - this.registerHTTPEndpoint({ - method: "get", - route: "/", - fn: (req, res) => { - return res.json({ - LINEBRIDGE_SERVER_VERSION: LINEBRIDGE_SERVER_VERSION, - id: this.id, - usid: this.usid, - oskid: this.oskid, - requestTime: new Date().getTime(), - endpointsMap: this.endpointsMap, - wsEndpointsMap: this.wsInterface.map, - }) + cleanupProcess = () => { + console.log("🔴 Stopping server...") + + this.httpInterface.close() + this.wsInterface.io.close() + } + + // handlers + handleWSClientConnection = async (socket) => { + socket.res = (...args) => { + socket.emit("response", ...args) + } + socket.err = (...args) => { + socket.emit("responseError", ...args) + } + + if (typeof this.params.onWSClientConnection === "function") { + await this.params.onWSClientConnection(socket) + } + + for await (const [nsp, on, dispatch] of this.wsInterface.eventsChannels) { + socket.on(on, async (...args) => { + try { + await dispatch(socket, ...args).catch((error) => { + socket.err({ + message: error.message, + }) + }) + } catch (error) { + socket.err({ + message: error.message, + }) + } + }) + } + + socket.on("ping", () => { + socket.emit("pong") + }) + + socket.on("disconnect", async () => { + if (typeof this.params.onWSClientDisconnect === "function") { + await this.params.onWSClientDisconnect(socket) } }) } + // public methods consoleOutputServerInfo = () => { console.log(`🌐 Server info:`) console.table({ @@ -300,13 +325,6 @@ class Server { }) } - cleanupProcess = () => { - console.log("🔴 Stopping server...") - - this.httpInterface.close() - this.wsInterface.io.close() - } - toogleEndpointReachability = (method, route, enabled) => { if (typeof this.endpointsMap[method] !== "object") { throw new Error(`Cannot toogle endpoint, method [${method}] not set!`)