GitHub - xflyhack/micro-api-gateway · GitHub
Skip to content

xflyhack/micro-api-gateway

Folders and files

Repository files navigation

micro-api-gateway

Go 实现的轻量 API Gateway,用于本地开发和微服务编排环境。网关统一处理路由转发、前缀剥离、CORS、JWT 鉴权、Trace ID 生成与透传,以及聚合就绪检查。

GitHub Description: Lightweight Go API gateway with reverse proxy routing, JWT auth, CORS, trace propagation, health checks, and observability hooks.

功能

  • 按 path 前缀转发到不同上游服务。
  • 转发时自动 StripPrefix,例如 /chat/api/v1/... 转为上游 /api/v1/...
  • 统一 CORS 处理。
  • 识别或生成 X-Request-Id,并透传给上游服务。
  • 对需要鉴权的路径校验 Authorization: Bearer <token>
  • 提供 /healthz/readyz 健康检查。

路由

/chat/* -> CHAT_UPSTREAM
/user/* -> USER_UPSTREAM
/kb/*   -> KB_UPSTREAM

默认上游:

CHAT_UPSTREAM=http://127.0.0.1:9002
USER_UPSTREAM=http://127.0.0.1:9001
KB_UPSTREAM=http://127.0.0.1:9003

在当前微服务编排中通常使用:

CHAT_UPSTREAM=http://127.0.0.1:9102
USER_UPSTREAM=http://127.0.0.1:9101
KB_UPSTREAM=http://127.0.0.1:9103

启动

export GW_ADDR=":9999"
export CHAT_UPSTREAM="http://127.0.0.1:9102"
export USER_UPSTREAM="http://127.0.0.1:9101"
export KB_UPSTREAM="http://127.0.0.1:9103"
export JWT_SECRET="your-jwt-secret"

go run ./cmd/gateway

健康检查

curl -i "http://127.0.0.1:9999/healthz"
curl -i "http://127.0.0.1:9999/readyz"
curl -i "http://127.0.0.1:9999/chat/healthz"
curl -i "http://127.0.0.1:9999/user/healthz"
curl -i "http://127.0.0.1:9999/kb/healthz"

本地 hosts

如果希望用域名访问,可以把 api.g.com 指向本机:

127.0.0.1 api.g.com

在微服务编排中使用

该服务已适配根目录 docker-compose.yml。推荐通过上层编排统一启动:

cd ..
cp .env.example .env
./scripts/start.sh

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors