简体   繁体   English

如何在 golang 中使用 gin 服务两个静态站点?

[英]How to serve two static sites with gin in golang?

I want to create an application that will call a boolean function and depending on the result provide 1 of 2 compiled react apps as static sites.我想创建一个将调用布尔函数的应用程序,并根据结果提供 2 个已编译的 React 应用程序中的 1 个作为静态站点。

I'm using the LoadHTMLGlob function recommended by gin and it works fine with.tmpl files like the example in thier docs.我正在使用 gin 推荐的 LoadHTMLGlob 函数,它可以很好地处理 .tmpl 文件,就像他们文档中的示例一样。 However when doing just static html with a static directory for each site nothing seems to go well.但是,当只为每个站点使用静态目录执行静态 html 时,似乎一切都不顺利。

File Structure:文件结构:

├── main.go
└── sites
    ├── new
    │   ├── index.html
    │   └── static
    └── old
        ├── index.html
        └── static

Go Code:去代码:

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())
    }
}

I expect that when isBetaUser comes back as true it should load the static content under sites/new otherwise load sites/old.我希望当 isBetaUser 返回为真时,它应该加载 sites/new 下的静态内容,否则加载 sites/old。

However loading globs produces: panic: read sites/new/static: is a directory when starting panics.然而,加载 globs 会产生: panic: read sites/new/static: is a directory when starting panics。

Loading the html files individually (commented out above) Runs fine, but when a request comes it panics with:单独加载 html 文件(上面已注释掉)运行正常,但是当请求出现时它会出现恐慌:

html/template: "new/index.html" is undefined

I've also string using sites/[old||new]/index.html in c.HTML我还在 c.HTML 中使用 sites/[old||new]/index.html 进行了字符串化

Try sites/**/*.html to fix the panic.尝试sites/**/*.html来修复恐慌。

And note that Go uses the template files' base name as the template name, so to execute a template you don't use "path/to/template.html" but "template.html" .请注意,Go 使用模板文件的基本名称作为模板名称,因此要执行模板,您不使用"path/to/template.html"而是使用"template.html" This, of course, causes a problem in your case since as explained in the documentation :当然,这会导致您的情况出现问题,因为如文档中所述:

When parsing multiple files with the same name in different directories, the last one mentioned will be the one that results.当解析不同目录中具有相同名称的多个文件时,最后提到的文件将是结果。

To fix this you need to explicitly name your templates which you can do by using the {{ define "template_name" }} action.要解决此问题,您需要明确命名您的模板,您可以使用{{ define "template_name" }}操作来完成。

  1. Open sites/new/index.html打开sites/new/index.html
  2. Add {{ define "new/index.html" }} as the first line添加{{ define "new/index.html" }}作为第一行
  3. Add {{ end }} as the last line添加{{ end }}作为最后一行
  4. Repeat for sites/old/index.html with "old/index.html" as the name.对名称为"old/index.html"sites/old/index.html重复上述操作。

You need to define the template in your template files first, whether it's html/tmpl file.您需要先在模板文件中定义模板,无论是 html/tmpl 文件。 Something like this,像这样的东西,

{{ define "new/index.tmpl" }}... {{ end }}

or if you want to stick with html file then it would be或者如果你想坚持使用 html 文件,那么它将是

{{ define "new/index.html" }}... {{ end }} . {{ define "new/index.html" }}... {{ end }}

So your template file (from your example: sites/new/index.html ) should look like this,所以你的模板文件(来自你的例子: sites/new/index.html )应该是这样的,

{{ define "new/index.html" }}
  <html>
     <h1>
         {{ .title }}
     </h1>
     <p>New site</p>
   </html>
{{ end }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM