4

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.

I'm using the LoadHTMLGlob function recommended by gin and it works fine with .tmpl files like the example in thier docs. However when doing just static html with a static directory for each site nothing seems to go well.

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.

However loading globs produces: 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/template: "new/index.html" is undefined

I've also string using sites/[old||new]/index.html in c.HTML

jizhihaoSAMA
  • 10,685
  • 9
  • 18
  • 36
Jbar
  • 43
  • 4

2 Answers2

2

Try sites/**/*.html to fix the panic.

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". 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.

  1. Open sites/new/index.html
  2. Add {{ define "new/index.html" }} as the first line
  3. Add {{ end }} as the last line
  4. Repeat for sites/old/index.html with "old/index.html" as the name.
mkopriva
  • 18,908
  • 3
  • 28
  • 40
  • Perfect, that's solved the html not returning, however I'm now hitting the issue of getting a 404 when static/mystylesheet.app.css And both sites have their own static directories to serve. – Jbar Apr 25 '19 at 15:34
  • @Jbar just to clarify, static files like css, js, images etc are not being handled by `c.HTML` nor `r.LoadHTMLGlob`. To serve static files you need to properly setup the gin router by using one of those [`StaticXxx`](https://godoc.org/github.com/gin-gonic/gin#RouterGroup.Static) methods provided by the router. – mkopriva Apr 25 '19 at 15:40
  • That's what I was hoping to avoid as I need to change which static folder is served as well as the html. r.StaticX's dir can't be changed depending on the result of isBetaUser – Jbar Apr 25 '19 at 15:59
  • @Jbar use [`c.HTML`](https://godoc.org/github.com/gin-gonic/gin#Context.HTML)'s `obj` argument to pass any dynamic data like `gin.H{"dir": dir}` where the `dir` variable holds `"new"` or `"old"`. Then update your templates to link to the static files using the dir. e.g. ``. – mkopriva Apr 25 '19 at 16:06
0

You need to define the template in your template files first, whether it's html/tmpl file. Something like this,

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

or if you want to stick with html file then it would be

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

So your template file (from your example: sites/new/index.html) should look like this,

{{ define "new/index.html" }}
  <html>
     <h1>
         {{ .title }}
     </h1>
     <p>New site</p>
   </html>
{{ end }}
monirz
  • 474
  • 5
  • 14