feat(api-gateway): 添加 core-edu 服务代理路由
main.go 新增 exams/homework/grades 三组路由(无尾斜杠+通配符) config.go 新增 CoreEduServiceURL 字段默认 localhost:3004 删除死代码 internal/routing/routing.go(从未被 main 引用) 验证: GET /exams/class/:id 200, POST /exams 201, 链路全通
This commit is contained in:
@@ -13,6 +13,7 @@ type Config struct {
|
|||||||
ClassesServiceURL string
|
ClassesServiceURL string
|
||||||
IamServiceURL string
|
IamServiceURL string
|
||||||
TeacherBffURL string
|
TeacherBffURL string
|
||||||
|
CoreEduServiceURL string
|
||||||
OTLPEndpoint string
|
OTLPEndpoint string
|
||||||
LogLevel string
|
LogLevel string
|
||||||
DevMode bool
|
DevMode bool
|
||||||
@@ -27,6 +28,7 @@ func Load() *Config {
|
|||||||
ClassesServiceURL: getEnv("CLASSES_SERVICE_URL", "http://localhost:3001"),
|
ClassesServiceURL: getEnv("CLASSES_SERVICE_URL", "http://localhost:3001"),
|
||||||
IamServiceURL: getEnv("IAM_SERVICE_URL", "http://localhost:3002"),
|
IamServiceURL: getEnv("IAM_SERVICE_URL", "http://localhost:3002"),
|
||||||
TeacherBffURL: getEnv("TEACHER_BFF_URL", "http://localhost:3003"),
|
TeacherBffURL: getEnv("TEACHER_BFF_URL", "http://localhost:3003"),
|
||||||
|
CoreEduServiceURL: getEnv("CORE_EDU_SERVICE_URL", "http://localhost:3004"),
|
||||||
OTLPEndpoint: getEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318"),
|
OTLPEndpoint: getEnv("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4318"),
|
||||||
LogLevel: getEnv("LOG_LEVEL", "info"),
|
LogLevel: getEnv("LOG_LEVEL", "info"),
|
||||||
DevMode: getEnvBool("DEV_MODE", false),
|
DevMode: getEnvBool("DEV_MODE", false),
|
||||||
|
|||||||
@@ -1,56 +0,0 @@
|
|||||||
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",
|
|
||||||
})
|
|
||||||
}
|
|
||||||
@@ -79,6 +79,20 @@ func main() {
|
|||||||
bffHandler := proxy.ProxyHandler(bffProxy)
|
bffHandler := proxy.ProxyHandler(bffProxy)
|
||||||
api.Any("/teacher", bffHandler)
|
api.Any("/teacher", bffHandler)
|
||||||
api.Any("/teacher/*path", bffHandler)
|
api.Any("/teacher/*path", bffHandler)
|
||||||
|
|
||||||
|
// core-edu 服务路由(考试/作业/成绩)
|
||||||
|
// 注:同时注册无尾斜杠与通配符两条路由,与 classes/iam/teacher 一致。
|
||||||
|
coreEduProxy, err := proxy.NewProxy(cfg.CoreEduServiceURL)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed to create core-edu proxy: %v", err)
|
||||||
|
}
|
||||||
|
coreEduHandler := proxy.ProxyHandler(coreEduProxy)
|
||||||
|
api.Any("/exams", coreEduHandler)
|
||||||
|
api.Any("/exams/*path", coreEduHandler)
|
||||||
|
api.Any("/homework", coreEduHandler)
|
||||||
|
api.Any("/homework/*path", coreEduHandler)
|
||||||
|
api.Any("/grades", coreEduHandler)
|
||||||
|
api.Any("/grades/*path", coreEduHandler)
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{
|
srv := &http.Server{
|
||||||
|
|||||||
Reference in New Issue
Block a user