mirror of
https://github.com/ragestudio/linebridge.git
synced 2025-06-20 07:54:18 +00:00
Refactor route registration and plugin initialization
- Add filePath to route registration for better traceability - Restrict HTTP file route matching to method-named files - Replace engine.map with engine.registers (Set) for endpoint tracking - Refactor IPC service registration to use engine.registers - Move plugin initialization before engine.listen - Delegate route registration to engine.register method
This commit is contained in:
parent
7db6b06b48
commit
1c4291928a
@ -50,6 +50,7 @@ export default class Route {
|
|||||||
this.server.register.http({
|
this.server.register.http({
|
||||||
method: method,
|
method: method,
|
||||||
route: this.params.route,
|
route: this.params.route,
|
||||||
|
filePath: this.params.filePath,
|
||||||
middlewares: this.params.useMiddlewares,
|
middlewares: this.params.useMiddlewares,
|
||||||
fn: this[method].handler,
|
fn: this[method].handler,
|
||||||
})
|
})
|
||||||
|
@ -80,7 +80,10 @@ class RTEngineNG {
|
|||||||
const handler = this.events.get(message.event)
|
const handler = this.events.get(message.event)
|
||||||
|
|
||||||
if (typeof handler === "function") {
|
if (typeof handler === "function") {
|
||||||
const result = await handler(client, message.data)
|
const result = await handler(client, message.data, {
|
||||||
|
senders: this.senders,
|
||||||
|
find: this.find,
|
||||||
|
})
|
||||||
|
|
||||||
if (message.ack === true) {
|
if (message.ack === true) {
|
||||||
client.emit(`ack_${message.event}`, result)
|
client.emit(`ack_${message.event}`, result)
|
||||||
|
@ -13,7 +13,7 @@ export default class Engine {
|
|||||||
app = null
|
app = null
|
||||||
ws = null
|
ws = null
|
||||||
router = new he.Router()
|
router = new he.Router()
|
||||||
map = new Map()
|
registers = new Set()
|
||||||
|
|
||||||
initialize = async () => {
|
initialize = async () => {
|
||||||
this.app = new he.Server({
|
this.app = new he.Server({
|
||||||
@ -73,6 +73,14 @@ export default class Engine {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
register = (obj) => {
|
||||||
|
// set to the endpoints map, used by _map
|
||||||
|
this.registers.add(obj)
|
||||||
|
|
||||||
|
// register endpoint to http interface router
|
||||||
|
this.router[obj.method](obj.route, ...obj.middlewares, obj.fn)
|
||||||
|
}
|
||||||
|
|
||||||
listen = async () => {
|
listen = async () => {
|
||||||
await this.app.listen(this.server.params.listenPort)
|
await this.app.listen(this.server.params.listenPort)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,10 @@ export default async (startDir, server) => {
|
|||||||
await RecursiveRegister({
|
await RecursiveRegister({
|
||||||
start: startDir,
|
start: startDir,
|
||||||
match: async (filePath) => {
|
match: async (filePath) => {
|
||||||
return filePath.endsWith(".js") || filePath.endsWith(".ts")
|
// Only match files named as HTTP methods (get.js, post.ts, etc.)
|
||||||
|
const httpMethodRegex =
|
||||||
|
/\/(get|post|put|delete|patch|options|head)\.(js|ts)$/i
|
||||||
|
return httpMethodRegex.test(filePath)
|
||||||
},
|
},
|
||||||
onMatch: async ({ absolutePath, relativePath }) => {
|
onMatch: async ({ absolutePath, relativePath }) => {
|
||||||
const paths = relativePath.split("/")
|
const paths = relativePath.split("/")
|
||||||
@ -51,14 +54,17 @@ export default async (startDir, server) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
new Route(server, {
|
const routeParams = {
|
||||||
route: route,
|
route: route,
|
||||||
|
filePath: absolutePath,
|
||||||
useMiddlewares: fileObj.useMiddlewares,
|
useMiddlewares: fileObj.useMiddlewares,
|
||||||
useContexts: fileObj.useContexts,
|
useContexts: fileObj.useContexts,
|
||||||
handlers: {
|
handlers: {
|
||||||
[method]: fileObj.fn ?? fileObj,
|
[method]: fileObj.fn ?? fileObj,
|
||||||
},
|
},
|
||||||
}).register()
|
}
|
||||||
|
|
||||||
|
new Route(server, routeParams).register()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -5,8 +5,8 @@ export default async (server) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get only the root paths
|
// get only the root paths
|
||||||
let paths = Array.from(server.engine.map.keys()).map((key) => {
|
let paths = Array.from(server.engine.registers.values()).map((key) => {
|
||||||
const root = key.split("/")[1]
|
const root = key.route.split("/")[1]
|
||||||
|
|
||||||
return "/" + root
|
return "/" + root
|
||||||
})
|
})
|
||||||
|
@ -99,7 +99,6 @@ class Server {
|
|||||||
headers = {}
|
headers = {}
|
||||||
events = {}
|
events = {}
|
||||||
contexts = {}
|
contexts = {}
|
||||||
|
|
||||||
engine = null
|
engine = null
|
||||||
|
|
||||||
get hasSSL() {
|
get hasSSL() {
|
||||||
@ -211,6 +210,11 @@ class Server {
|
|||||||
await registerServiceToIPC(this)
|
await registerServiceToIPC(this)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (const Plugin of this.plugins) {
|
||||||
|
const pluginInstance = new Plugin(this)
|
||||||
|
await pluginInstance.initialize()
|
||||||
|
}
|
||||||
|
|
||||||
// listen
|
// listen
|
||||||
await this.engine.listen()
|
await this.engine.listen()
|
||||||
|
|
||||||
@ -226,7 +230,6 @@ class Server {
|
|||||||
const lines = [
|
const lines = [
|
||||||
`- Url: ${this.hasSSL ? "https" : "http"}://${this.params.listenIp}:${this.params.listenPort}`,
|
`- Url: ${this.hasSSL ? "https" : "http"}://${this.params.listenIp}:${this.params.listenPort}`,
|
||||||
`- Websocket: ${this.engine.ws ? this.engine.ws?.config?.path : "Disabled"}`,
|
`- Websocket: ${this.engine.ws ? this.engine.ws?.config?.path : "Disabled"}`,
|
||||||
`- Routes: ${this.engine.map.size}`,
|
|
||||||
`- Tooks: ${elapsedTimeInMs.toFixed(2)}ms`,
|
`- Tooks: ${elapsedTimeInMs.toFixed(2)}ms`,
|
||||||
]
|
]
|
||||||
|
|
||||||
@ -254,18 +257,7 @@ class Server {
|
|||||||
`[${obj.method.toUpperCase()}] ${obj.route}`,
|
`[${obj.method.toUpperCase()}] ${obj.route}`,
|
||||||
)
|
)
|
||||||
|
|
||||||
// set to the endpoints map, used by _map
|
return this.engine.register(obj)
|
||||||
this.engine.map.set(obj.route, {
|
|
||||||
method: obj.method,
|
|
||||||
path: obj.route,
|
|
||||||
})
|
|
||||||
|
|
||||||
// register endpoint to http interface router
|
|
||||||
this.engine.router[obj.method](
|
|
||||||
obj.route,
|
|
||||||
...obj.middlewares,
|
|
||||||
obj.fn,
|
|
||||||
)
|
|
||||||
},
|
},
|
||||||
ws: (wsEndpointObj) => {},
|
ws: (wsEndpointObj) => {},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user