improve gateway

This commit is contained in:
SrGooglo 2025-03-28 22:05:13 +00:00
parent cc57742bc8
commit 6d678b5f00
4 changed files with 57 additions and 74 deletions

View File

@ -59,6 +59,11 @@ export default class Proxy {
proxy.on("proxyReq", (proxyReq, req, res, options) => {
proxyReq.setHeader("x-linebridge-version", pkg.version)
proxyReq.setHeader("x-forwarded-for", req.socket.remoteAddress)
proxyReq.setHeader("x-service-id", serviceId)
proxyReq.setHeader(
"X-Forwarded-Proto",
req.socket.encrypted ? "https" : "http",
)
})
proxy.on("error", (e) => {

View File

@ -1,5 +1,6 @@
import chokidar from "chokidar"
import path from "path"
import { minimatch } from "minimatch"
import spawnService from "../utils/spawnService"
import getIgnoredFiles from "../utils/getIgnoredFiles"
@ -54,7 +55,6 @@ export default class Service {
id: this.id,
service: this.path,
cwd: this.cwd,
onReload: this.handleReload.bind(this),
onClose: this.handleClose.bind(this),
onError: this.handleError.bind(this),
onIPCData: this.handleIPCData.bind(this),
@ -77,7 +77,8 @@ export default class Service {
]
this.fileWatcher = chokidar.watch(this.cwd, {
ignored,
ignored: (path) =>
ignored.some((pattern) => minimatch(path, pattern)),
persistent: true,
ignoreInitial: true,
})
@ -99,15 +100,6 @@ export default class Service {
}
}
/**
* Handle service reload request
* @param {object} params - Reload parameters
*/
async handleReload(params) {
// The actual reload is handled by the reload() method
console.log(`[${this.id}] Handling reload request`)
}
/**
* Handle service closure
* @param {string} id - Service ID
@ -143,8 +135,9 @@ export default class Service {
console.log(`[${this.id}] Reloading service...`)
// Kill the current process if is running
if (this.instance.exitCode !== null) {
await this.instance.kill("SIGINT")
if (this.instance.exitCode === null) {
console.log(`[${this.id}] Killing current process...`)
await this.instance.kill("SIGKILL")
}
// Start a new process
@ -165,7 +158,7 @@ export default class Service {
}
if (this.instance) {
await this.instance.kill("SIGINT")
await this.instance.kill("SIGKILL")
this.instance = null
}
}

View File

@ -3,72 +3,56 @@ import createServiceLogTransformer from "./createServiceLogTransformer"
import Vars from "../vars"
export default async ({
id,
service,
cwd,
onReload,
onClose,
onError,
onIPCData,
}) => {
const instanceEnv = {
...process.env,
lb_service: {
id: service.id,
index: service.index,
},
}
export default async ({ id, service, cwd, onClose, onError, onIPCData }) => {
const instanceEnv = {
...process.env,
lb_service: {
id: service.id,
index: service.index,
},
}
let instance = ChildProcess.fork(Vars.bootloaderBin, [service], {
detached: false,
silent: true,
cwd: cwd,
env: instanceEnv,
killSignal: "SIGKILL",
})
let instance = ChildProcess.fork(Vars.bootloaderBin, [service], {
detached: false,
silent: true,
cwd: cwd,
env: instanceEnv,
killSignal: "SIGTERM",
})
instance.reload = () => {
onReload({
id,
service,
cwd,
})
}
instance.logs = {
stdout: createServiceLogTransformer({ id }),
stderr: createServiceLogTransformer({ id, color: "bgRed" }),
attach: () => {
instance.logs.stdout.pipe(process.stdout)
instance.logs.stderr.pipe(process.stderr)
},
detach: () => {
instance.logs.stdout.unpipe(process.stdout)
instance.logs.stderr.unpipe(process.stderr)
},
}
instance.logs = {
stdout: createServiceLogTransformer({ id }),
stderr: createServiceLogTransformer({ id, color: "bgRed" }),
attach: () => {
instance.logs.stdout.pipe(process.stdout)
instance.logs.stderr.pipe(process.stderr)
},
detach: () => {
instance.logs.stdout.unpipe(process.stdout)
instance.logs.stderr.unpipe(process.stderr)
},
}
instance.logs.stdout.history = []
instance.logs.stderr.history = []
instance.logs.stdout.history = []
instance.logs.stderr.history = []
// push to buffer history
instance.stdout.pipe(instance.logs.stdout)
instance.stderr.pipe(instance.logs.stderr)
// push to buffer history
instance.stdout.pipe(instance.logs.stdout)
instance.stderr.pipe(instance.logs.stderr)
instance.on("message", (data) => {
return onIPCData(id, data)
})
instance.on("message", (data) => {
return onIPCData(id, data)
})
instance.on("error", (err) => {
return onError(id, err)
})
instance.on("error", (err) => {
return onError(id, err)
})
instance.on("close", (code) => {
return onClose(id, code)
})
instance.on("close", (code) => {
return onClose(id, code)
})
global.ipcRouter.register({ id, instance })
global.ipcRouter.register({ id, instance })
return instance
}
return instance
}

View File

@ -26,6 +26,7 @@
"http-proxy": "^1.18.1",
"jsonwebtoken": "^9.0.2",
"linebridge": "^0.24.1",
"minimatch": "^10.0.1",
"minio": "^8.0.1",
"module-alias": "^2.2.3",
"mongoose": "^8.5.3",