P
P
pcdesign2015-10-13 13:36:10
go
pcdesign, 2015-10-13 13:36:10

How to generate routes and templates?

There is a completely static site.
On bare html.
It needs to be translated to Go.
Here is the code, it works.

package main

import (
        "github.com/gin-gonic/contrib/renders/multitemplate"
        "github.com/gin-gonic/gin"
)

func main() {
        templates := multitemplate.New()
        templates.AddFromFiles("index",
                "templates/Base.html",
                "templates/Index.html")

        templates.AddFromFiles("contact",
                "templates/Base.html",
                "templates/Contact.html")

        router := gin.New()
        router.HTMLRender = templates

        router.GET("/index.html", func(c *gin.Context) {
                c.HTML(200, "index", gin.H{
                        "title": "Home",
                        "stuff": "Interesting home stuff",
                })
        })

        router.GET("/contact.html", func(c *gin.Context) {
                c.HTML(200, "contact", gin.H{
                        "title": "Contact",
                        "stuff": "Interesting contact stuff",
                })
        })
        router.Run(":5000")
}

Is there an option not to describe the routes each time and not to add a template for each case each time?
For example, the template folder contains the about.html file.
How can I make the route "/about.html" automatically created and the required template automatically added?
PS Tried to process it in a cycle.
Here is the code.
It even starts up.
package main

import (
        "github.com/gin-gonic/contrib/renders/multitemplate"
        "github.com/gin-gonic/gin"
        "path/filepath"
)

func main() {
        templates := multitemplate.New()
        files, _ := filepath.Glob("templates/*.html")
        for _, element := range files {
                name := element[10:]
                templates.AddFromFiles(name, "templates/Base_tmpl", element)
        }

But, when you try to access the route, you get the following:
curl  localhost:5000/contact.html
curl: (52) Empty reply from server

And here's the error:
http: panic serving [::1]:39610: runtime error: invalid memory address or nil pointer dereference
goroutine 10 [running]:
net/http.func·011()
        /usr/lib/golang/src/net/http/server.go:1130 +0xbb
html/template.(*Template).escape(0x0, 0x0, 0x0)
        /usr/lib/golang/src/html/template/template.go:56 +0x3a
html/template.(*Template).Execute(0x0, 0x7f7a588e0780, 0xc2080726e0, 0x76e360, 0xc2080305d0, 0x0, 0x0)
        /usr/lib/golang/src/html/template/template.go:75 +0x3d
github.com/gin-gonic/gin/render.HTML.Render(0x0, 0x0, 0x0, 0x76e360, 0xc2080305d0, 0x7f7a588e0748, 0xc2080726e0, 0x0, 0x0)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/render/html.go:63 +0xd6
github.com/gin-gonic/gin/render.(*HTML).Render(0xc208030600, 0x7f7a588e0748, 0xc2080726e0, 0x0, 0x0)
        <autogenerated>:2 +0xc6
github.com/gin-gonic/gin.(*Context).Render(0xc2080726e0, 0xc8, 0x7f7a588e0720, 0xc208030600)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/context.go:326 +0x90
github.com/gin-gonic/gin.(*Context).HTML(0xc2080726e0, 0xc8, 0x804530, 0xc, 0x76e360, 0xc2080305d0)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/context.go:341 +0xad
main.func·002(0xc2080726e0)
        /home/go-user/site/main.go:41 +0x1ad
github.com/gin-gonic/gin.(*Context).Next(0xc2080726e0)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/context.go:95 +0x80
github.com/gin-gonic/gin.(*Engine).handleHTTPRequest(0xc2080cc700, 0xc2080726e0)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/gin.go:294 +0x2b3
github.com/gin-gonic/gin.(*Engine).ServeHTTP(0xc2080cc700, 0x7f7a588e05e8, 0xc2080ecaa0, 0xc208033450)
        /home/go-user/workspace/src/github.com/gin-gonic/gin/gin.go:275 +0x117
net/http.serverHandler.ServeHTTP(0xc20804e300, 0x7f7a588e05e8, 0xc2080ecaa0, 0xc208033450)
        /usr/lib/golang/src/net/http/server.go:1703 +0x19a
net/http.(*conn).serve(0xc2080eca00)
        /usr/lib/golang/src/net/http/server.go:1204 +0xb57
created by net/http.(*Server).Serve
        /usr/lib/golang/src/net/http/server.go:1751 +0x35e

Answer the question

In order to leave comments, you need to log in

3 answer(s)
N
Nikita, 2015-10-13
@pcdesign

Well, loop through all the files in your pages folder and do the same as written in the question, except that you will pull the names dynamically.

A
Artem, 2015-10-13
@artem_kovardin

It's probably possible to use http.FileServer

package main

import (
  "log"
  "net/http"
)

func main() {
  // Simple static webserver:
  log.Fatal(http.ListenAndServe(":8080", http.FileServer(http.Dir("/usr/share/doc"))))
}

Here is the dock for http.FileServer

U
uvelichitel, 2015-10-13
@uvelichitel

Hugo is a great static site generator in Go.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question