mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-09 10:34:17 +00:00
improve readibility
This commit is contained in:
parent
97f38ce73d
commit
d0f9b7c1d6
@ -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)
|
||||
}
|
||||
initializeHeaders = () => {
|
||||
this.httpInterface.use((req, res, next) => {
|
||||
Object.keys(this.headers).forEach((key) => {
|
||||
res.setHeader(key, this.headers[key])
|
||||
})
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
next()
|
||||
})
|
||||
}
|
||||
|
||||
socket.on("ping", () => {
|
||||
socket.emit("pong")
|
||||
})
|
||||
initializeMiddlewares = () => {
|
||||
const useMiddlewares = [...defaultMiddlewares, ...(this.params.middlewares ?? [])]
|
||||
|
||||
socket.on("disconnect", async () => {
|
||||
if (typeof this.params.onWSClientDisconnect === "function") {
|
||||
await this.params.onWSClientDisconnect(socket)
|
||||
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!`)
|
||||
|
Loading…
x
Reference in New Issue
Block a user