feat(api-gateway): retire teacher/student/parent-bff, route GraphQL to apollo-router
v2.1 M9+M10: BFF layer and old portals retired.
- api-gateway: remove TeacherBffURL/StudentBffURL/ParentBffURL config
- api-gateway: add ApolloRouterURL config and /api/graphql route
- api-gateway: /api/admin/graphql now proxies to apollo-router
- api-gateway: health checks now ping apollo-router instead of BFF
- deploy.yml: replace teacher-bff service block with apollo-router
- deploy.yml: add config-service service block (M3 dependency)
- deploy.yml: remove teacher-portal and admin-portal service blocks
- source code in services/{teacher,student,parent}-bff/ and apps/*-portal/ retained for rollback
This commit is contained in:
@@ -9,6 +9,9 @@ import (
|
||||
// Config 持有 api-gateway 运行时配置。
|
||||
// P2 起 JWT 验签改 RS256(IAM 签发,Gateway 用 JWKS 公钥校验),
|
||||
// JWTSecret 仅在 DevMode 下作为 mock 密钥保留。
|
||||
//
|
||||
// v2.1 M9:teacher-bff / student-bff / parent-bff 已下线,
|
||||
// 由 apollo-router(GraphQL 联邦)替代 BFF 聚合层。
|
||||
type Config struct {
|
||||
Port string
|
||||
JWKSURL string // RS256 公钥端点(IAM GET /.well-known/jwks.json)
|
||||
@@ -18,9 +21,7 @@ type Config struct {
|
||||
CORSOrigins string
|
||||
ClassesServiceURL string
|
||||
IamServiceURL string
|
||||
TeacherBffURL string
|
||||
StudentBffURL string
|
||||
ParentBffURL string
|
||||
ApolloRouterURL string // v2.1 M9:apollo-router GraphQL 联邦入口
|
||||
CoreEduServiceURL string
|
||||
ContentServiceURL string
|
||||
DataAnaServiceURL string
|
||||
@@ -77,9 +78,7 @@ func Load() *Config {
|
||||
CORSOrigins: getEnv("CORS_ORIGINS", ""),
|
||||
ClassesServiceURL: getEnv("CLASSES_SERVICE_URL", "http://localhost:3001"),
|
||||
IamServiceURL: getEnv("IAM_SERVICE_URL", "http://localhost:3002"),
|
||||
TeacherBffURL: getEnv("TEACHER_BFF_URL", "http://localhost:3003"),
|
||||
StudentBffURL: getEnv("STUDENT_BFF_URL", "http://localhost:3009"),
|
||||
ParentBffURL: getEnv("PARENT_BFF_URL", "http://localhost:3010"),
|
||||
ApolloRouterURL: getEnv("APOLLO_ROUTER_URL", "http://localhost:3000"),
|
||||
CoreEduServiceURL: getEnv("CORE_EDU_SERVICE_URL", "http://localhost:3004"),
|
||||
ContentServiceURL: getEnv("CONTENT_SERVICE_URL", "http://localhost:3005"),
|
||||
DataAnaServiceURL: getEnv("DATA_ANA_SERVICE_URL", "http://localhost:3006"),
|
||||
|
||||
@@ -29,20 +29,20 @@ type downstreamCheck struct {
|
||||
// GET /readyz:并行 ping 下游服务 /healthz,超时 2s。
|
||||
//
|
||||
// 软失败规则(president-final-rulings.md §3.3):
|
||||
// - 必需依赖(iam / teacher-bff,P2 已就绪)失败 → 503
|
||||
// - 可选依赖(P3-P5 未就绪服务)失败 → 200 + degraded 列表
|
||||
// - 必需依赖(iam / apollo-router)失败 → 503
|
||||
// - 可选依赖(core-edu / content / msg / ai / data-ana)失败 → 200 + degraded 列表
|
||||
// - 全部可达 → 200 {"status":"ok"}
|
||||
//
|
||||
// v2.1 M9:teacher-bff / student-bff / parent-bff 已下线,由 apollo-router 替代。
|
||||
func Readyz(cfg *config.Config) gin.HandlerFunc {
|
||||
checks := []downstreamCheck{
|
||||
{name: "iam", url: cfg.IamServiceURL + "/healthz", required: true},
|
||||
{name: "teacher-bff", url: cfg.TeacherBffURL + "/healthz", required: true},
|
||||
{name: "apollo-router", url: cfg.ApolloRouterURL + "/healthz", required: true},
|
||||
{name: "core-edu", url: cfg.CoreEduServiceURL + "/healthz", required: false},
|
||||
{name: "content", url: cfg.ContentServiceURL + "/healthz", required: false},
|
||||
{name: "msg", url: cfg.MsgServiceURL + "/healthz", required: false},
|
||||
{name: "ai", url: cfg.AiServiceURL + "/healthz", required: false},
|
||||
{name: "data-ana", url: cfg.DataAnaServiceURL + "/healthz", required: false},
|
||||
{name: "student-bff", url: cfg.StudentBffURL + "/healthz", required: false},
|
||||
{name: "parent-bff", url: cfg.ParentBffURL + "/healthz", required: false},
|
||||
}
|
||||
|
||||
client := &http.Client{Timeout: 2 * time.Second}
|
||||
|
||||
@@ -79,15 +79,9 @@ func main() {
|
||||
registerProxy(api, "classes", cfg.CoreEduServiceURL)
|
||||
// iam 服务路由(身份与访问管理)
|
||||
registerProxy(api, "iam", cfg.IamServiceURL)
|
||||
// teacher-bff 路由(教师聚合层 GraphQL)
|
||||
// BFF 在 /graphql 提供服务(@Controller("graphql")),需剥离 /api/v1/teacher 前缀
|
||||
// 契约 student-portal_contract.md §2.3:/api/v1/{bff}/* → {bff}:port/*
|
||||
registerBffProxy(api, "teacher", cfg.TeacherBffURL)
|
||||
// student-bff 路由(学生聚合层 GraphQL,P3)
|
||||
registerBffProxy(api, "student", cfg.StudentBffURL)
|
||||
// parent-bff 路由(家长聚合层 GraphQL,P4)
|
||||
// parent-bff 在 /v1/graphql 提供服务(ARB-022 §24.4 ISSUE-003 方案 A)
|
||||
registerBffProxy(api, "parent", cfg.ParentBffURL)
|
||||
// v2.1 M9:teacher-bff / student-bff / parent-bff 已下线,
|
||||
// 前端 GraphQL 查询统一通过 apollo-router 聚邦层。
|
||||
// /api/v1/teacher/* /api/v1/student/* /api/v1/parent/* 路由已移除。
|
||||
// core-edu 域路由(考试/作业/成绩)—— 下游 controller 在 /v1/{domain}/*,仅需剥离 /api
|
||||
registerProxy(api, "exams", cfg.CoreEduServiceURL)
|
||||
registerProxy(api, "homework", cfg.CoreEduServiceURL)
|
||||
@@ -110,25 +104,43 @@ func main() {
|
||||
registerProxy(api, "dashboard", cfg.DataAnaServiceURL)
|
||||
}
|
||||
|
||||
// admin 路由组:admin-portal 入口(契约 admin-portal_contract.md §2.3)
|
||||
// POST /api/admin/graphql → teacher-bff:3003/graphql(admin 命名空间)
|
||||
// 中间件链:熔断 → JWT 鉴权 → admin 角色强制 → 指标 → 反向代理
|
||||
// v2.1 M9:GraphQL 联邦入口(替代旧 BFF 聚合层)
|
||||
// /api/graphql → apollo-router:3000/graphql(统一前端 GraphQL 入口)
|
||||
// /api/admin/graphql → apollo-router:3000/graphql(admin 命名空间)
|
||||
// 中间件链:熔断 → JWT 鉴权 → 指标 → 反向代理(admin 路由额外强制 admin 角色)
|
||||
graphqlGroup := r.Group("/api/graphql")
|
||||
graphqlGroup.Use(middleware.CircuitBreaker("apollo-router"))
|
||||
graphqlGroup.Use(middleware.AuthMiddleware(cfg, fetcher))
|
||||
graphqlGroup.Use(observability.Metrics())
|
||||
{
|
||||
// 路径重写:/api/graphql → /graphql(apollo-router supergraph.path)
|
||||
graphqlProxy, err := proxy.NewProxyRewrite(cfg.ApolloRouterURL, func(p string) string {
|
||||
return strings.TrimPrefix(p, "/api/graphql")
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("failed to create apollo-router graphql proxy", "target", cfg.ApolloRouterURL, "error", err)
|
||||
panic(err)
|
||||
}
|
||||
graphqlGroup.Any("", proxy.ProxyHandler(graphqlProxy))
|
||||
graphqlGroup.Any("/", proxy.ProxyHandler(graphqlProxy))
|
||||
}
|
||||
|
||||
// admin GraphQL 入口(admin-portal 仍走独立路径以强制 admin 角色)
|
||||
admin := r.Group("/api/admin")
|
||||
admin.Use(middleware.CircuitBreaker("teacher-bff-admin"))
|
||||
admin.Use(middleware.CircuitBreaker("apollo-router-admin"))
|
||||
admin.Use(middleware.AuthMiddleware(cfg, fetcher))
|
||||
admin.Use(middleware.AdminRoleMiddleware())
|
||||
admin.Use(observability.Metrics())
|
||||
{
|
||||
// /api/admin/graphql 是唯一入口(契约 §2.3 要求),
|
||||
// 路径重写:/api/admin/graphql → /graphql(teacher-bff @Controller("graphql"))
|
||||
graphqlProxy, err := proxy.NewProxyRewrite(cfg.TeacherBffURL, func(p string) string {
|
||||
// 路径重写:/api/admin/graphql → /graphql(apollo-router)
|
||||
adminGraphqlProxy, err := proxy.NewProxyRewrite(cfg.ApolloRouterURL, func(p string) string {
|
||||
return strings.TrimPrefix(p, "/api/admin")
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("failed to create admin graphql proxy", "target", cfg.TeacherBffURL, "error", err)
|
||||
slog.Error("failed to create admin graphql proxy", "target", cfg.ApolloRouterURL, "error", err)
|
||||
panic(err)
|
||||
}
|
||||
admin.Any("/graphql", proxy.ProxyHandler(graphqlProxy))
|
||||
admin.Any("/graphql", proxy.ProxyHandler(adminGraphqlProxy))
|
||||
}
|
||||
|
||||
srv := &http.Server{
|
||||
@@ -180,22 +192,5 @@ func registerProxy(api *gin.RouterGroup, prefix, targetURL string) {
|
||||
api.Any("/"+prefix+"/*path", handler)
|
||||
}
|
||||
|
||||
// registerBffProxy 创建带路径重写的反向代理,用于 BFF 路由(teacher/student/parent)。
|
||||
// BFF 在 /graphql 或 /v1/graphql 提供服务(@Controller),与下游 controller 在 /v1/{domain}/* 的
|
||||
// 非 BFF 服务不同,需剥离 /api/v1/{prefix} 前缀,仅转发剩余路径到下游。
|
||||
// 例:/api/v1/teacher/graphql → /graphql(teacher-bff:3003/graphql)。
|
||||
// 例:/api/v1/parent/v1/graphql → /v1/graphql(parent-bff:3010/v1/graphql,ARB-022 §24.4 ISSUE-003 方案 A)。
|
||||
// 契约依据:student-portal_contract.md §2.3 /api/v1/{bff}/* → {bff}:port/*
|
||||
func registerBffProxy(api *gin.RouterGroup, prefix, targetURL string) {
|
||||
stripPrefix := "/api/v1/" + prefix
|
||||
p, err := proxy.NewProxyRewrite(targetURL, func(p string) string {
|
||||
return strings.TrimPrefix(p, stripPrefix)
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("failed to create bff proxy", "prefix", prefix, "target", targetURL, "error", err)
|
||||
panic(err)
|
||||
}
|
||||
handler := proxy.ProxyHandler(p)
|
||||
api.Any("/"+prefix, handler)
|
||||
api.Any("/"+prefix+"/*path", handler)
|
||||
}
|
||||
// registerBffProxy 已在 v2.1 M9 移除:teacher-bff / student-bff / parent-bff 由 apollo-router 替代。
|
||||
// 前端 GraphQL 查询统一通过 /api/graphql → apollo-router:3000/graphql。
|
||||
|
||||
Reference in New Issue
Block a user