use universal service registration via ipc

This commit is contained in:
SrGooglo 2025-03-31 23:42:55 +00:00
parent 0d5b9be68c
commit e1c383df53
3 changed files with 49 additions and 98 deletions

View File

@ -78,54 +78,6 @@ export default class HyperExpressEngineNG {
} }
listen = async () => { listen = async () => {
if (process.env.lb_service) {
let pathOverrides = Object.keys(this.router.map).map((key) => {
return key.split("/")[1]
})
// remove duplicates
pathOverrides = [...new Set(pathOverrides)]
// remove "" and _map
pathOverrides = pathOverrides.filter((key) => {
if (key === "" || key === "_map") {
return false
}
return true
})
if (this.ctx.constructor.enableWebsockets === true) {
process.send({
type: "router:ws:register",
id: process.env.lb_service.id,
index: process.env.lb_service.index,
data: {
namespace: this.ctx.constructor.refName,
ws_path: this.ctx.constructor.wsPath ?? "/",
listen_port: this.ctx.constructor.listen_port,
},
})
}
if (process.send) {
// try to send router map to host
process.send({
type: "router:register",
id: process.env.lb_service.id,
index: process.env.lb_service.index,
data: {
router_map: this.router.map,
path_overrides: pathOverrides,
listen: {
ip: this.ctx.constructor.listen_ip,
port: this.ctx.constructor.listen_port,
},
},
})
}
}
await this.app.listen(this.ctx.constructor.listen_port) await this.app.listen(this.ctx.constructor.listen_port)
} }

View File

@ -75,56 +75,6 @@ export default class Engine {
} }
listen = async () => { listen = async () => {
if (process.env.lb_service) {
let pathOverrides = Object.keys(this.router.map).map((key) => {
return key.split("/")[1]
})
// remove duplicates
pathOverrides = [...new Set(pathOverrides)]
// remove "" and _map
pathOverrides = pathOverrides.filter((key) => {
if (key === "" || key === "_map") {
return false
}
return true
})
if (this.ctx.constructor.enableWebsockets) {
process.send({
type: "router:ws:register",
id: process.env.lb_service.id,
index: process.env.lb_service.index,
data: {
namespace: this.ctx.constructor.refName,
listen_port: this.ctx.constructor.listen_port,
ws_path:
this.ctx.constructor.wsPath ??
this.ctx.constructor.refName,
},
})
}
if (process.send) {
// try to send router map to host
process.send({
type: "router:register",
id: process.env.lb_service.id,
index: process.env.lb_service.index,
data: {
router_map: this.router.map,
path_overrides: pathOverrides,
listen: {
ip: this.ctx.constructor.listen_ip,
port: this.ctx.constructor.listen_port,
},
},
})
}
}
await this.app.listen(this.ctx.constructor.listen_port) await this.app.listen(this.ctx.constructor.listen_port)
} }

View File

@ -209,6 +209,7 @@ class Server {
// if is a linebridge service then initialize IPC Channels // if is a linebridge service then initialize IPC Channels
if (process.env.lb_service) { if (process.env.lb_service) {
await this.initializeIpc() await this.initializeIpc()
await this.registerServiceToIPC()
} }
// try to execute beforeInitialize hook. // try to execute beforeInitialize hook.
@ -326,6 +327,54 @@ class Server {
return execs return execs
} }
registerServiceToIPC = () => {
if (!process.env.lb_service || !process.send) {
console.error("IPC not available")
return null
}
// get only the root paths
let paths = Object.keys(this.engine.router.map).map((key) => {
const root = key.split("/")[1]
return "/" + root
})
// remove duplicates
paths = [...new Set(paths)]
// remove "" and _map
paths = paths.filter((key) => {
if (key === "/" || key === "/_map") {
return false
}
return true
})
process.send({
type: "service:register",
id: process.env.lb_service.id,
index: process.env.lb_service.index,
register: {
namespace: this.constructor.refName,
http: {
enabled: true,
paths: paths,
proto: this.ssl?.key && this.ssl?.cert ? "https" : "http",
},
websocket: {
enabled: this.constructor.enableWebsockets,
path: this.constructor.wsPath ?? "/",
},
listen: {
ip: this.params.listen_ip,
port: this.params.listen_port,
},
},
})
}
} }
module.exports = Server module.exports = Server