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) => { proxy.on("proxyReq", (proxyReq, req, res, options) => {
proxyReq.setHeader("x-linebridge-version", pkg.version) proxyReq.setHeader("x-linebridge-version", pkg.version)
proxyReq.setHeader("x-forwarded-for", req.socket.remoteAddress) 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) => { proxy.on("error", (e) => {

View File

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

View File

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

View File

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