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:
yml
2026-11-24 12:58:04 +08:00
parent 44f883866c
commit b63f5d771b
6 changed files with 83 additions and 2 deletions
+6 -2
View File
@@ -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,
+16
View File
@@ -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 ")