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:
SrGooglo 2025-06-16 23:17:07 +00:00
parent 7db6b06b48
commit 1c4291928a
6 changed files with 31 additions and 21 deletions

View File

@ -50,6 +50,7 @@ export default class Route {
this.server.register.http({
method: method,
route: this.params.route,
filePath: this.params.filePath,
middlewares: this.params.useMiddlewares,
fn: this[method].handler,
})

View File

@ -80,7 +80,10 @@ class RTEngineNG {
const handler = this.events.get(message.event)
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) {
client.emit(`ack_${message.event}`, result)

View File

@ -13,7 +13,7 @@ export default class Engine {
app = null
ws = null
router = new he.Router()
map = new Map()
registers = new Set()
initialize = async () => {
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 () => {
await this.app.listen(this.server.params.listenPort)
}

View File

@ -13,7 +13,10 @@ export default async (startDir, server) => {
await RecursiveRegister({
start: startDir,
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 }) => {
const paths = relativePath.split("/")
@ -51,14 +54,17 @@ export default async (startDir, server) => {
}
}
new Route(server, {
const routeParams = {
route: route,
filePath: absolutePath,
useMiddlewares: fileObj.useMiddlewares,
useContexts: fileObj.useContexts,
handlers: {
[method]: fileObj.fn ?? fileObj,
},
}).register()
}
new Route(server, routeParams).register()
},
})
}

View File

@ -5,8 +5,8 @@ export default async (server) => {
}
// get only the root paths
let paths = Array.from(server.engine.map.keys()).map((key) => {
const root = key.split("/")[1]
let paths = Array.from(server.engine.registers.values()).map((key) => {
const root = key.route.split("/")[1]
return "/" + root
})

View File

@ -99,7 +99,6 @@ class Server {
headers = {}
events = {}
contexts = {}
engine = null
get hasSSL() {
@ -211,6 +210,11 @@ class Server {
await registerServiceToIPC(this)
}
for (const Plugin of this.plugins) {
const pluginInstance = new Plugin(this)
await pluginInstance.initialize()
}
// listen
await this.engine.listen()
@ -226,7 +230,6 @@ class Server {
const lines = [
`- Url: ${this.hasSSL ? "https" : "http"}://${this.params.listenIp}:${this.params.listenPort}`,
`- Websocket: ${this.engine.ws ? this.engine.ws?.config?.path : "Disabled"}`,
`- Routes: ${this.engine.map.size}`,
`- Tooks: ${elapsedTimeInMs.toFixed(2)}ms`,
]
@ -254,18 +257,7 @@ class Server {
`[${obj.method.toUpperCase()}] ${obj.route}`,
)
// set to the endpoints map, used by _map
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,
)
return this.engine.register(obj)
},
ws: (wsEndpointObj) => {},
}