feat(tenant): 增加租户基础与域名解析
This commit is contained in:
@@ -2,6 +2,7 @@ package router
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"hfb_sys/backend/internal/config"
|
||||
@@ -43,6 +44,7 @@ import (
|
||||
"hfb_sys/backend/internal/modules/user"
|
||||
"hfb_sys/backend/internal/modules/wallet"
|
||||
"hfb_sys/backend/internal/modules/withdrawal"
|
||||
"hfb_sys/backend/internal/tenancy"
|
||||
|
||||
_ "hfb_sys/backend/docs" // Swagger 文档
|
||||
|
||||
@@ -64,6 +66,12 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
engine.Use(middleware.RequestID())
|
||||
engine.Use(middleware.RequestLogger(logger))
|
||||
engine.Use(middleware.Recovery(logger))
|
||||
var tenantRepo *tenancy.Repository
|
||||
if deps.DB != nil {
|
||||
tenantRepo = tenancy.NewRepository(deps.DB)
|
||||
engine.Use(middleware.TenantContext(tenantRepo, cfg.AppEnv, logger))
|
||||
}
|
||||
tenantHandler := tenancy.NewHandler()
|
||||
jwtManager := auth.NewJWTManager(cfg.JWTSecret)
|
||||
|
||||
health := handler.NewHealthHandler(newReadinessChecks(deps))
|
||||
@@ -462,6 +470,33 @@ func New(cfg config.Config, deps Dependencies, logger *zap.Logger) *gin.Engine {
|
||||
api.GET("/home-announcements", systemConfigHandler.HomeAnnouncements)
|
||||
api.GET("/mobile-home-config", systemConfigHandler.HomeConfig)
|
||||
api.GET("/public/files/object", fileHandler.PublicObject)
|
||||
api.GET("/site/context", tenantHandler.SiteContext)
|
||||
api.GET("/site/check-domain", func(c *gin.Context) {
|
||||
domain := strings.TrimSpace(c.Query("domain"))
|
||||
if domain == "" {
|
||||
c.String(http.StatusBadRequest, "domain required")
|
||||
return
|
||||
}
|
||||
if tenantRepo != nil {
|
||||
allowed, err := tenantRepo.IsActiveDomain(c.Request.Context(), domain)
|
||||
if err != nil {
|
||||
c.String(http.StatusServiceUnavailable, "tenant domain check failed")
|
||||
return
|
||||
}
|
||||
if allowed {
|
||||
c.String(http.StatusOK, "ok")
|
||||
return
|
||||
}
|
||||
}
|
||||
if distributionService != nil {
|
||||
info, err := distributionService.GetPublicSiteInfo(c.Request.Context(), "", domain)
|
||||
if err == nil && info != nil && info.Status == "active" {
|
||||
c.String(http.StatusOK, "ok")
|
||||
return
|
||||
}
|
||||
}
|
||||
c.String(http.StatusNotFound, "domain not allowed")
|
||||
})
|
||||
// Gin 同一前缀只能有一种通配符名;管理端已用 /sites/:id/*,公开列表必须同名。
|
||||
api.GET("/distribution/sites/:id/listings", distributionHandler.ListPublicSiteListings)
|
||||
api.GET("/distribution/site-info", distributionHandler.GetPublicSiteInfo)
|
||||
|
||||
Reference in New Issue
Block a user