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 sidecar,生产 Compose 默认值为 http://ocr:8787。
|
||||||
# OCR_SERVICE_URL=http://ocr:8787
|
# OCR_SERVICE_URL=http://ocr:8787
|
||||||
# 选号网(hfb_show)独立域名,与主站同机;不部署选号网可留空。
|
# 选号网(hfb_show)独立域名,与主站同机;不部署选号网可留空。
|
||||||
|
# 后端启动时会自动登记为 dachui 的 showcase 租户域名。
|
||||||
# CADDY_SHOW_DOMAIN=show.example.com
|
# CADDY_SHOW_DOMAIN=show.example.com
|
||||||
# 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show
|
# 选号网源码目录(相对 hfb_sys 根或绝对路径),默认 ../hfb_show
|
||||||
# HFB_SHOW_DIR=../hfb_show
|
# HFB_SHOW_DIR=../hfb_show
|
||||||
|
|||||||
@@ -88,6 +88,7 @@ func main() {
|
|||||||
if err := tenancy.BootstrapDomains(context.Background(), db, tenancy.BootstrapConfig{
|
if err := tenancy.BootstrapDomains(context.Background(), db, tenancy.BootstrapConfig{
|
||||||
PrimaryDomain: cfg.PrimaryDomain,
|
PrimaryDomain: cfg.PrimaryDomain,
|
||||||
JieyouDomain: cfg.TenantJieyouDomain,
|
JieyouDomain: cfg.TenantJieyouDomain,
|
||||||
|
ShowDomain: cfg.ShowDomain,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
logger.Fatal("经营租户域名初始化失败", zap.Error(err))
|
logger.Fatal("经营租户域名初始化失败", zap.Error(err))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,8 +27,11 @@ type Config struct {
|
|||||||
BootstrapAdminNickname string
|
BootstrapAdminNickname string
|
||||||
PrimaryDomain string
|
PrimaryDomain string
|
||||||
TenantJieyouDomain string
|
TenantJieyouDomain string
|
||||||
OpsDomain string
|
// ShowDomain 是选号网(hfb_show)的独立域名。启动时登记为 dachui 的 showcase
|
||||||
DistributionAdminDomain string
|
// 租户域名,否则生产环境的 Unknown Host 保护会以 site_not_found 拒绝其 /api 请求。
|
||||||
|
ShowDomain string
|
||||||
|
OpsDomain string
|
||||||
|
DistributionAdminDomain string
|
||||||
// FrontendShellURL 指向 Caddy 内网只读的构建后 index.html;后端据此按 Host 注入租户标题和 SEO 信息。
|
// FrontendShellURL 指向 Caddy 内网只读的构建后 index.html;后端据此按 Host 注入租户标题和 SEO 信息。
|
||||||
FrontendShellURL string
|
FrontendShellURL string
|
||||||
Storage StorageConfig
|
Storage StorageConfig
|
||||||
@@ -115,6 +118,7 @@ func Load() Config {
|
|||||||
BootstrapAdminNickname: getEnv("ADMIN_BOOTSTRAP_NICKNAME", "超级管理员"),
|
BootstrapAdminNickname: getEnv("ADMIN_BOOTSTRAP_NICKNAME", "超级管理员"),
|
||||||
PrimaryDomain: primaryDomain,
|
PrimaryDomain: primaryDomain,
|
||||||
TenantJieyouDomain: strings.TrimSpace(getEnv("TENANT_JIEYOU_DOMAIN", "")),
|
TenantJieyouDomain: strings.TrimSpace(getEnv("TENANT_JIEYOU_DOMAIN", "")),
|
||||||
|
ShowDomain: strings.TrimSpace(getEnv("CADDY_SHOW_DOMAIN", "")),
|
||||||
OpsDomain: opsDomain(appEnv, getEnv("OPS_DOMAIN", ""), primaryDomain),
|
OpsDomain: opsDomain(appEnv, getEnv("OPS_DOMAIN", ""), primaryDomain),
|
||||||
DistributionAdminDomain: distributionAdminDomain(
|
DistributionAdminDomain: distributionAdminDomain(
|
||||||
appEnv,
|
appEnv,
|
||||||
|
|||||||
@@ -148,6 +148,9 @@ func (r *Repository) cacheHost(host string, scope *Scope) {
|
|||||||
type BootstrapConfig struct {
|
type BootstrapConfig struct {
|
||||||
PrimaryDomain string
|
PrimaryDomain string
|
||||||
JieyouDomain string
|
JieyouDomain string
|
||||||
|
// ShowDomain 是选号网(hfb_show)域名,登记为 dachui 的 showcase 域名。
|
||||||
|
// 选号网与主站共用后端,未登记时 RequireKnownApplicationHost 会拒绝其 /api 请求。
|
||||||
|
ShowDomain string
|
||||||
}
|
}
|
||||||
|
|
||||||
func BootstrapDomains(ctx context.Context, db *gorm.DB, cfg BootstrapConfig) error {
|
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)
|
primary := sitecode.ExtractHost(cfg.PrimaryDomain)
|
||||||
jieyou := sitecode.ExtractHost(cfg.JieyouDomain)
|
jieyou := sitecode.ExtractHost(cfg.JieyouDomain)
|
||||||
|
|
||||||
|
registered := make(map[string]struct{}, 4)
|
||||||
if primary != "" {
|
if primary != "" {
|
||||||
if err := repo.UpsertDomain(ctx, "dachui", primary, "storefront"); err != nil {
|
if err := repo.UpsertDomain(ctx, "dachui", primary, "storefront"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
registered[primary] = struct{}{}
|
||||||
h5 := "h5." + primary
|
h5 := "h5." + primary
|
||||||
if err := repo.UpsertDomain(ctx, "dachui", h5, "storefront_alias"); err != nil {
|
if err := repo.UpsertDomain(ctx, "dachui", h5, "storefront_alias"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
registered[h5] = struct{}{}
|
||||||
}
|
}
|
||||||
if jieyou != "" && jieyou != primary {
|
if jieyou != "" && jieyou != primary {
|
||||||
if err := repo.UpsertDomain(ctx, "jieyou", jieyou, "storefront"); err != nil {
|
if err := repo.UpsertDomain(ctx, "jieyou", jieyou, "storefront"); err != nil {
|
||||||
return err
|
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
|
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) {
|
func TestResolveCode(t *testing.T) {
|
||||||
repo, dachui, _ := setupTenantRepoTest(t)
|
repo, dachui, _ := setupTenantRepoTest(t)
|
||||||
scope, err := repo.ResolveCode(t.Context(), " DAC HUI ")
|
scope, err := repo.ResolveCode(t.Context(), " DAC HUI ")
|
||||||
|
|||||||
@@ -153,6 +153,11 @@ HFB_SHOW_DIR=../hfb_show
|
|||||||
- 构建 Caddy 镜像时打入主站 + 选号网静态资源;
|
- 构建 Caddy 镜像时打入主站 + 选号网静态资源;
|
||||||
- 为选号网域名申请 HTTPS,并把 `/api/*` 反代到 `backend:8080`。
|
- 为选号网域名申请 HTTPS,并把 `/api/*` 反代到 `backend:8080`。
|
||||||
|
|
||||||
|
后端启动时会把 `CADDY_SHOW_DOMAIN` 自动登记为 dachui 的 `showcase` 租户域名
|
||||||
|
(见 `tenancy.BootstrapDomains`),使选号网的同源 `/api/*` 请求能按 dachui 租户解析;
|
||||||
|
该域名与主站域名、解忧域名重合时保留原有类型,不会改写。手工在数据库补过域名后,
|
||||||
|
部署会自动自愈,不再依赖人工 SQL。
|
||||||
|
|
||||||
不配置 `CADDY_SHOW_DOMAIN` 则行为与原来一致,仅部署主站。
|
不配置 `CADDY_SHOW_DOMAIN` 则行为与原来一致,仅部署主站。
|
||||||
|
|
||||||
如果正式对外运营,还需要配置真实短信和实名服务,避免继续使用测试配置。
|
如果正式对外运营,还需要配置真实短信和实名服务,避免继续使用测试配置。
|
||||||
|
|||||||
Reference in New Issue
Block a user