refactor remove upstream env dependency (#551)

This commit is contained in:
ekko
2026-05-08 20:46:22 +08:00
committed by GitHub
parent bba4920fee
commit 51fde26797
13 changed files with 90 additions and 165 deletions
+4 -2
View File
@@ -2,7 +2,6 @@ import { existsSync, readFileSync } from 'fs'
import { resolve } from 'path'
import * as hermesCli from '../services/hermes/hermes-cli'
import { getGatewayManagerInstance } from '../services/gateway-bootstrap'
import { config } from '../config'
declare const __APP_VERSION__: string
@@ -73,7 +72,10 @@ export async function healthCheck(ctx: any) {
let gatewayOk = false
try {
const mgr = getGatewayManagerInstance()
const upstream = mgr?.getUpstream() || config.upstream
const upstream = mgr?.getUpstream()
if (!upstream) {
throw new Error('GatewayManager not initialized')
}
const res = await fetch(`${upstream.replace(/\/$/, '')}/health`, { signal: AbortSignal.timeout(5000) })
gatewayOk = res.ok
} catch { }
+13 -3
View File
@@ -1,10 +1,12 @@
import type { Context } from 'koa'
import { getGatewayManagerInstance } from '../../services/gateway-bootstrap'
import { config } from '../../config'
function getUpstream(profile: string): string {
const mgr = getGatewayManagerInstance()
return mgr ? mgr.getUpstream(profile) : config.upstream.replace(/\/$/, '')
if (!mgr) {
throw new Error('GatewayManager not initialized')
}
return mgr.getUpstream(profile)
}
function getApiKey(profile: string): string | null {
@@ -54,7 +56,15 @@ async function readUpstreamError(res: Response): Promise<unknown> {
async function proxyRequest(ctx: Context, upstreamPath: string, method?: string): Promise<void> {
const profile = resolveProfile(ctx)
const upstream = getUpstream(profile)
let upstream: string
try {
upstream = getUpstream(profile)
} catch (e: any) {
ctx.status = 503
ctx.set('Content-Type', 'application/json')
ctx.body = { error: { message: e?.message || 'GatewayManager not initialized' } }
return
}
const params = new URLSearchParams(ctx.search || '')
params.delete('token')
const search = params.toString()
@@ -1,4 +1,3 @@
import { emitWebhook } from '../services/hermes/hermes'
import { logger } from '../services/logger'
export async function handleWebhook(ctx: any) {
@@ -9,6 +8,5 @@ export async function handleWebhook(ctx: any) {
return
}
logger.info('Received webhook event: %s', payload.event)
emitWebhook(payload)
ctx.body = { ok: true }
}