Files
Your Name 3b70fe1865 P4-A: 三服务共享auth/logging能力 - 共享包边界定义/golden测试/契约测试
- gateway/internal/shared/: 新建 shared/auth 和 shared/logging 包
- shared/logging: LogEntry/Logger/NewLogger/sanitizeFields, 7个golden output测试
- shared/auth: ExtractBearerToken/HasExternalQueryKey/WriteAuthError/AuditEvent, 8个契约测试
- docs/plans/2026-04-21-shared-auth-logging-analysis.md: P4-A完整分析文档

迁移顺序: logging(第一步) -> auth基础(第二步) -> audit(第三步) -> 契约测试(第四步)
共享边界: JWT验证/token状态查询/授权策略/BruteForce保持服务特有
2026-04-21 19:00:25 +08:00
..

Gateway

OpenAI 兼容入口网关,负责接入、鉴权、限流、上游路由与基础审计。

当前真实状态

  • 服务入口是 cmd/gateway/main.go
  • 当前对外暴露的主要接口是 /v1/chat/completions/v1/completions/v1/models,以及对应的 /api/v1/* 兼容路径。
  • /v1/models 现在返回当前已注册 provider 的模型并集,按模型 ID 去重后排序,不再返回硬编码静态列表。
  • 鉴权运行时支持两种模式:
    • inmemory
    • remote_introspection
  • inmemory 只允许用于 dev 本地联调;非 dev 环境必须使用 remote_introspection,不得在 gateway 本地承载 token authority。
  • provider 注册已经从配置装配;如果未显式配置 provider启动时会基于环境变量生成默认 OpenAI provider。
  • 审计发射器支持 PostgreSQL 与内存实现;数据库未配置时会显式回退到内存实现。
  • 生产环境必须显式提供 PASSWORD_ENCRYPTION_KEYGATEWAY_CORS_ALLOW_ORIGINS;缺省密钥和 * CORS 只允许在开发态使用。

与其他服务的边界

  • gateway 不签发业务 token。
  • gatewayremote_introspection 模式下依赖 platform-token-runtime 提供 token introspection。
  • gateway 只承载入口控制,不承载 supply-apiplatform-token-runtime 的业务逻辑。

本地运行

cd "/home/long/project/立交桥/gateway"
export OPENAI_API_KEY="..."
export OPENAI_BASE_URL="https://api.openai.com"
export OPENAI_MODELS="gpt-4,gpt-3.5-turbo"
export GATEWAY_ENV="dev"
export GATEWAY_TOKEN_RUNTIME_MODE="inmemory"
go run ./cmd/gateway

如果要切到远程 token 校验:

export GATEWAY_TOKEN_RUNTIME_MODE="remote_introspection"
export GATEWAY_TOKEN_RUNTIME_URL="http://127.0.0.1:18081"

dev 环境约束:

  1. GATEWAY_ENV 只要不是 devGATEWAY_TOKEN_RUNTIME_MODE 就必须设置为 remote_introspection
  2. stagingproduction 和其他共享环境不得使用 inmemory
  3. token 生命周期、状态解释和 introspection 字段以 platform-token-runtime 为单一真源。

默认监听 0.0.0.0:8080

生产环境额外要求:

export GATEWAY_ENV="production"
export PASSWORD_ENCRYPTION_KEY="32-byte-production-secret........"
export GATEWAY_CORS_ALLOW_ORIGINS="https://console.example.com,https://app.example.com"

验证命令

模块级验证:

cd "/home/long/project/立交桥/gateway"
go test -count=1 ./...

仓库级统一验证:

cd "/home/long/project/立交桥"
bash scripts/ci/repo_integrity_check.sh

关键目录

  • internal/config/:环境变量配置与 provider 配置加载。
  • internal/handler/OpenAI 兼容 HTTP handler。
  • internal/middleware/鉴权、CORS、远程 introspection。
  • internal/router/provider 路由、打分与 fallback。

路由策略说明

  • 主启动链路当前只接入 latencyround_robinweightedavailability 四种策略。
  • internal/router/strategy/cost_based.gointernal/router/strategy/cost_aware.gointernal/router/fallback/ 仍属于实验性模块,没有接入 BuildServer 的运行时装配。
  • 如果配置里填入未接入的策略名,启动链路会回退到 latency