我想创建一个将调用布尔函数的应用程序,并根据结果提供 2 个已编译的反应应用程序中的 1 个作为静态站点。
我正在使用 gin 推荐的 LoadHTMLGlob 函数,它适用于 .tmpl 文件,如他们文档中的示例。然而,当只为每个站点使用静态目录做静态 html 时,似乎没有任何进展。
文件结构:
├── main.go
└── sites
├── new
│ ├── index.html
│ └── static
└── old
├── index.html
└── static
去代码:
func main() {
r := gin.Default()
//r.LoadHTMLFiles("sites/old/index.html", "sites/new/index.html") //doesn't complain, but can't load html
r.LoadHTMLGlob("sites/**/*") // complains about /static being a dir on boot
r.GET("/sites/lib", func(c *gin.Context) {
id := c.Query("id")
useNewSite, err := isBetaUser(id)
if err != nil {
c.AbortWithStatusJSON(500, err.Error())
return
}
if useNewSite {
c.HTML(http.StatusOK, "new/index.html", nil)
} else {
c.HTML(http.StatusOK, "old/index.html", nil)
}
})
routerErr := r.Run(":8080")
if routerErr != nil {
panic(routerErr.Error())
}
}
我希望当 isBetaUser 返回为真时,它应该在站点/新加载静态内容,否则加载站点/旧。
但是加载 glob 会产生:
panic: read sites/new/static: is a directory
开始恐慌时。
单独加载 html 文件(上面注释掉)运行良好,但是当请求到来时它会出现以下情况:
html/template: "new/index.html" is undefined
我还在 c.HTML 中使用了 sites/[old||new]/index.html 字符串