P2 阶段交付物: - services/iam: 完整身份认证服务(users/roles/permissions/refresh_tokens 6 表 schema) - register/login/refresh/getUserInfo 4 个核心 API - bcrypt 密码哈希 + JWT 双 Token(access + refresh) - 复用 classes 黄金模板(errors/observability/middleware 三件套) - services/teacher-bff: 教师聚合 BFF - Promise.allSettled 并行聚合 IAM + classes 数据 - /teacher/dashboard 单一聚合端点 - packages/shared-proto/proto/iam.proto: IamService 契约(Register/Login/RefreshToken/GetUserInfo) - api-gateway: 新增 IamServiceURL/TeacherBffURL 配置 + /iam/* + /teacher/* 路由
57 lines
1.4 KiB
Go
57 lines
1.4 KiB
Go
package routing
|
|
|
|
import (
|
|
"github.com/edu-cloud/api-gateway/internal/config"
|
|
"github.com/edu-cloud/api-gateway/internal/middleware"
|
|
"github.com/edu-cloud/api-gateway/internal/proxy"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Setup 配置路由
|
|
func Setup(cfg *config.Config) *gin.Engine {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.New()
|
|
|
|
// 中间件
|
|
r.Use(middleware.RequestIDMiddleware())
|
|
r.Use(gin.Recovery())
|
|
|
|
// 健康检查(无需鉴权)
|
|
r.GET("/healthz", healthz)
|
|
|
|
// API v1 组(需要鉴权)
|
|
api := r.Group("/api/v1")
|
|
api.Use(middleware.AuthMiddleware(cfg))
|
|
{
|
|
// classes 服务路由
|
|
classesProxy, err := proxy.NewProxy(cfg.ClassesServiceURL)
|
|
if err != nil {
|
|
panic("failed to create classes proxy: " + err.Error())
|
|
}
|
|
api.Any("/classes/*path", proxy.ProxyHandler(classesProxy))
|
|
|
|
// IAM 服务路由(身份与访问管理)
|
|
iamProxy, err := proxy.NewProxy(cfg.IamServiceURL)
|
|
if err != nil {
|
|
panic("failed to create iam proxy: " + err.Error())
|
|
}
|
|
api.Any("/iam/*path", proxy.ProxyHandler(iamProxy))
|
|
|
|
// Teacher BFF 路由(教师聚合层)
|
|
bffProxy, err := proxy.NewProxy(cfg.TeacherBffURL)
|
|
if err != nil {
|
|
panic("failed to create teacher-bff proxy: " + err.Error())
|
|
}
|
|
api.Any("/teacher/*path", proxy.ProxyHandler(bffProxy))
|
|
}
|
|
|
|
return r
|
|
}
|
|
|
|
func healthz(c *gin.Context) {
|
|
c.JSON(200, gin.H{
|
|
"status": "ok",
|
|
"service": "api-gateway",
|
|
})
|
|
}
|