refactor remove upstream env dependency (#551)
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import type { Context } from 'koa'
|
||||
import { config } from '../../config'
|
||||
import { getGatewayManagerInstance } from '../../services/gateway-bootstrap'
|
||||
import { updateUsage } from '../../db/hermes/usage-store'
|
||||
|
||||
@@ -68,14 +67,14 @@ function resolveProfile(ctx: Context): string {
|
||||
/** Resolve upstream URL for a request based on profile header/query */
|
||||
function resolveUpstream(ctx: Context): string {
|
||||
const mgr = getGatewayManager()
|
||||
if (mgr) {
|
||||
const profile = resolveProfile(ctx)
|
||||
if (profile && profile !== 'default') {
|
||||
return mgr.getUpstream(profile)
|
||||
}
|
||||
return mgr.getUpstream()
|
||||
if (!mgr) {
|
||||
throw new Error('GatewayManager not initialized')
|
||||
}
|
||||
return config.upstream.replace(/\/$/, '')
|
||||
const profile = resolveProfile(ctx)
|
||||
if (profile && profile !== 'default') {
|
||||
return mgr.getUpstream(profile)
|
||||
}
|
||||
return mgr.getUpstream()
|
||||
}
|
||||
|
||||
function buildProxyHeaders(ctx: Context, upstream: string): Record<string, string> {
|
||||
@@ -185,7 +184,14 @@ async function streamSSE(ctx: Context, res: Response, profile: string): Promise<
|
||||
|
||||
export async function proxy(ctx: Context) {
|
||||
const profile = resolveProfile(ctx)
|
||||
const upstream = resolveUpstream(ctx)
|
||||
let upstream: string
|
||||
try {
|
||||
upstream = resolveUpstream(ctx)
|
||||
} catch (e: any) {
|
||||
ctx.status = 503
|
||||
ctx.body = { error: { message: e?.message || 'GatewayManager not initialized' } }
|
||||
return
|
||||
}
|
||||
const upstreamPath = ctx.path.replace(/^\/api\/hermes\/v1/, '/v1').replace(/^\/api\/hermes/, '/api')
|
||||
const params = new URLSearchParams(ctx.search || '')
|
||||
params.delete('token')
|
||||
|
||||
Reference in New Issue
Block a user