feat(tenant): 选号网域名随部署自动登记为 dachui 租户域名
选号网 hfb_show 与主站共用后端,生产环境的 Unknown Host 保护会把未登记的 xh.hao858.com 以 site_not_found 拒绝,导致列表接口 404。 后端启动时读取 CADDY_SHOW_DOMAIN,把选号网域名登记为 dachui 的 showcase 租户域名;与主站/解忧域名重合时保留原类型。避免换库或重建后依赖人工补 SQL。
This commit is contained in:
@@ -16,6 +16,7 @@ OPS_DOMAIN=
|
||||
# OCR sidecar,生产 Compose 默认值为 http://ocr:8787。
|
||||
# OCR_SERVICE_URL=http://ocr:8787
|
||||
# 选号网(hfb_show)独立域名,与主站同机;不部署选号网可留空。
|
||||
# 后端启动时会自动登记为 dachui 的 showcase 租户域名。
|
||||
# CADDY_SHOW_DOMAIN=show.example.com
|
||||
# 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show
|
||||
# HFB_SHOW_DIR=../hfb_show
|
||||
|
||||
@@ -88,6 +88,7 @@ func main() {
|
||||
if err := tenancy.BootstrapDomains(context.Background(), db, tenancy.BootstrapConfig{
|
||||
PrimaryDomain: cfg.PrimaryDomain,
|
||||
JieyouDomain: cfg.TenantJieyouDomain,
|
||||
ShowDomain: cfg.ShowDomain,
|
||||
}); err != nil {
|
||||
logger.Fatal("经营租户域名初始化失败", zap.Error(err))
|
||||
}
|
||||
|
||||
@@ -27,8 +27,11 @@ type Config struct {
|
||||
BootstrapAdminNickname string
|
||||
PrimaryDomain string
|
||||
TenantJieyouDomain string
|
||||
OpsDomain string
|
||||
DistributionAdminDomain string
|
||||
// ShowDomain 是选号网(hfb_show)的独立域名。启动时登记为 dachui 的 showcase
|
||||
// 租户域名,否则生产环境的 Unknown Host 保护会以 site_not_found 拒绝其 /api 请求。
|
||||
ShowDomain string
|
||||
OpsDomain string
|
||||
DistributionAdminDomain string
|
||||
// FrontendShellURL 指向 Caddy 内网只读的构建后 index.html;后端据此按 Host 注入租户标题和 SEO 信息。
|
||||
FrontendShellURL string
|
||||
Storage StorageConfig
|
||||
@@ -115,6 +118,7 @@ func Load() Config {
|
||||
BootstrapAdminNickname: getEnv("ADMIN_BOOTSTRAP_NICKNAME", "超级管理员"),
|
||||
PrimaryDomain: primaryDomain,
|
||||
TenantJieyouDomain: strings.TrimSpace(getEnv("TENANT_JIEYOU_DOMAIN", "")),
|
||||
ShowDomain: strings.TrimSpace(getEnv("CADDY_SHOW_DOMAIN", "")),
|
||||
OpsDomain: opsDomain(appEnv, getEnv("OPS_DOMAIN", ""), primaryDomain),
|
||||
DistributionAdminDomain: distributionAdminDomain(
|
||||
appEnv,
|
||||
|
||||
@@ -148,6 +148,9 @@ func (r *Repository) cacheHost(host string, scope *Scope) {
|
||||
type BootstrapConfig struct {
|
||||
PrimaryDomain string
|
||||
JieyouDomain string
|
||||
// ShowDomain 是选号网(hfb_show)域名,登记为 dachui 的 showcase 域名。
|
||||
// 选号网与主站共用后端,未登记时 RequireKnownApplicationHost 会拒绝其 /api 请求。
|
||||
ShowDomain string
|
||||
}
|
||||
|
||||
func BootstrapDomains(ctx context.Context, db *gorm.DB, cfg BootstrapConfig) error {
|
||||
@@ -158,19 +161,32 @@ func BootstrapDomains(ctx context.Context, db *gorm.DB, cfg BootstrapConfig) err
|
||||
primary := sitecode.ExtractHost(cfg.PrimaryDomain)
|
||||
jieyou := sitecode.ExtractHost(cfg.JieyouDomain)
|
||||
|
||||
registered := make(map[string]struct{}, 4)
|
||||
if primary != "" {
|
||||
if err := repo.UpsertDomain(ctx, "dachui", primary, "storefront"); err != nil {
|
||||
return err
|
||||
}
|
||||
registered[primary] = struct{}{}
|
||||
h5 := "h5." + primary
|
||||
if err := repo.UpsertDomain(ctx, "dachui", h5, "storefront_alias"); err != nil {
|
||||
return err
|
||||
}
|
||||
registered[h5] = struct{}{}
|
||||
}
|
||||
if jieyou != "" && jieyou != primary {
|
||||
if err := repo.UpsertDomain(ctx, "jieyou", jieyou, "storefront"); err != nil {
|
||||
return err
|
||||
}
|
||||
registered[jieyou] = struct{}{}
|
||||
}
|
||||
// 选号网域名单独登记为 dachui 的 showcase;与已登记域名重合时保留原类型,
|
||||
// 避免把主站 storefront 或解忧域名改写成 showcase。
|
||||
if show := sitecode.ExtractHost(cfg.ShowDomain); show != "" {
|
||||
if _, ok := registered[show]; !ok {
|
||||
if err := repo.UpsertDomain(ctx, "dachui", show, "showcase"); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -75,6 +75,60 @@ func TestBootstrapDomainsIsIdempotentAndRejectsHostConflict(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapDomainsRegistersShowDomainAsDachuiShowcase(t *testing.T) {
|
||||
repo, _, _ := setupTenantRepoTest(t)
|
||||
cfg := BootstrapConfig{
|
||||
PrimaryDomain: "hao858.com",
|
||||
JieyouDomain: "zuhaox.com",
|
||||
// CADDY_SHOW_DOMAIN 按约定只填域名(不带协议);重复启动必须幂等。
|
||||
ShowDomain: "xh.hao858.com",
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
||||
t.Fatalf("bootstrap domains run %d: %v", i+1, err)
|
||||
}
|
||||
}
|
||||
|
||||
repo.Invalidate()
|
||||
scope, err := repo.ResolveHost(t.Context(), "xh.hao858.com")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve show host: %v", err)
|
||||
}
|
||||
if scope == nil || scope.TenantCode != "dachui" || scope.DomainType != "showcase" {
|
||||
t.Fatalf("show scope = %#v, want dachui/showcase", scope)
|
||||
}
|
||||
|
||||
var count int64
|
||||
if err := repo.db.Model(&model.TenantDomain{}).Count(&count).Error; err != nil {
|
||||
t.Fatalf("count domains: %v", err)
|
||||
}
|
||||
if count != 4 {
|
||||
t.Fatalf("domain count = %d, want 4", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBootstrapDomainsKeepsRegisteredTypeWhenShowDomainOverlaps(t *testing.T) {
|
||||
repo, _, _ := setupTenantRepoTest(t)
|
||||
cfg := BootstrapConfig{
|
||||
PrimaryDomain: "hao858.com",
|
||||
JieyouDomain: "zuhaox.com",
|
||||
// 误把选号网配成解忧域名时,不能让解忧的 storefront 被改写成 dachui/showcase。
|
||||
ShowDomain: "zuhaox.com",
|
||||
}
|
||||
if err := BootstrapDomains(t.Context(), repo.db, cfg); err != nil {
|
||||
t.Fatalf("bootstrap domains: %v", err)
|
||||
}
|
||||
|
||||
repo.Invalidate()
|
||||
scope, err := repo.ResolveHost(t.Context(), "zuhaox.com")
|
||||
if err != nil {
|
||||
t.Fatalf("resolve jieyou host: %v", err)
|
||||
}
|
||||
if scope == nil || scope.TenantCode != "jieyou" || scope.DomainType != "storefront" {
|
||||
t.Fatalf("jieyou scope = %#v, want jieyou/storefront", scope)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveCode(t *testing.T) {
|
||||
repo, dachui, _ := setupTenantRepoTest(t)
|
||||
scope, err := repo.ResolveCode(t.Context(), " DAC HUI ")
|
||||
|
||||
Reference in New Issue
Block a user