From 58752b5750cf6cc86571448fb2261eb353a6d858 Mon Sep 17 00:00:00 2001 From: math-nao Date: Thu, 22 Mar 2018 22:14:21 +0100 Subject: [PATCH 1/6] Add new method LoadHTMLFilesRecursively for recursively load template files in a folder --- README.md | 6 +++++- gin.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 72ac99bd..03327110 100644 --- a/README.md +++ b/README.md @@ -829,13 +829,17 @@ func main() { ### HTML rendering -Using LoadHTMLGlob() or LoadHTMLFiles() +Using LoadHTMLGlob(), LoadHTMLFiles() or LoadHTMLFilesRecursively ```go func main() { router := gin.Default() router.LoadHTMLGlob("templates/*") //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") + + // load recursively all .html in "public" folder + //router.LoadHTMLFilesRecursively("public", []string{".html"}) + router.GET("/index", func(c *gin.Context) { c.HTML(http.StatusOK, "index.tmpl", gin.H{ "title": "Main website", diff --git a/gin.go b/gin.go index 4205eff0..32277285 100644 --- a/gin.go +++ b/gin.go @@ -10,6 +10,9 @@ import ( "net/http" "os" "sync" + "strings" + "sort" + "path/filepath" "github.com/gin-gonic/gin/render" ) @@ -194,6 +197,26 @@ func (engine *Engine) LoadHTMLFiles(files ...string) { engine.SetHTMLTemplate(templ) } +// LoadHTMLFilesRecursively loads recursivly a slice of HTML files mathing an extension +// and associates the result with HTML renderer. +func (engine *Engine) LoadHTMLFilesRecursively(folder string, extensionsAllowed []string) { + + files := []string{} + sort.Strings(extensionsAllowed) + + filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { + extension := strings.ToLower(filepath.Ext(path)) + indexFound := sort.SearchStrings(extensionsAllowed, extension) + if indexFound < len(extensionsAllowed) && extensionsAllowed[indexFound] == extension { + files = append(files, path); + } + + return nil + }) + + engine.LoadHTMLFiles(files...) +} + // SetHTMLTemplate associate a template with HTML renderer. func (engine *Engine) SetHTMLTemplate(templ *template.Template) { if len(engine.trees) > 0 { From d6bef2ee09a3c59ae4c44ab34336e250fb3b4200 Mon Sep 17 00:00:00 2001 From: math-nao Date: Thu, 22 Mar 2018 23:51:42 +0100 Subject: [PATCH 2/6] Add test for LoadHTMLFilesRecursively method --- gin_test.go | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/gin_test.go b/gin_test.go index 3ac60577..1132aa03 100644 --- a/gin_test.go +++ b/gin_test.go @@ -80,6 +80,35 @@ func setupHTMLGlob(t *testing.T, mode string, tls bool) func() { return func() {} } +func setupHTMLFilesRecursively(t *testing.T, mode string, tls bool) func() { + go func() { + SetMode(mode) + router := New() + router.Delims("{[{", "}]}") + router.SetFuncMap(template.FuncMap{ + "formatAsDate": formatAsDate, + }) + router.LoadHTMLFilesRecursively("./fixtures/basic/", []string{".tmpl"}) + router.GET("/test", func(c *Context) { + c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"}) + }) + router.GET("/raw", func(c *Context) { + c.HTML(http.StatusOK, "raw.tmpl", map[string]interface{}{ + "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), + }) + }) + if tls { + // these files generated by `go run $GOROOT/src/crypto/tls/generate_cert.go --host 127.0.0.1` + router.RunTLS(":9999", "./fixtures/testdata/cert.pem", "./fixtures/testdata/key.pem") + } else { + router.Run(":8888") + } + }() + t.Log("waiting 1 second for server startup") + time.Sleep(1 * time.Second) + return func() {} +} + func TestLoadHTMLGlob(t *testing.T) { td := setupHTMLGlob(t, DebugMode, false) res, err := http.Get("http://127.0.0.1:8888/test") @@ -248,6 +277,80 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) { td() } + +func TestLoadHTMLFilesRecursively(t *testing.T) { + td := setupHTMLFilesRecursively(t, DebugMode, false) + res, err := http.Get("http://127.0.0.1:8888/test") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "

Hello world

", string(resp[:])) + + td() +} + +func TestLoadHTMLFilesRecursively2(t *testing.T) { + td := setupHTMLFilesRecursively(t, TestMode, false) + res, err := http.Get("http://127.0.0.1:8888/test") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "

Hello world

", string(resp[:])) + + td() +} + +func TestLoadHTMLFilesRecursively3(t *testing.T) { + td := setupHTMLFilesRecursively(t, ReleaseMode, false) + res, err := http.Get("http://127.0.0.1:8888/test") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "

Hello world

", string(resp[:])) + + td() +} + +func TestLoadHTMLFilesRecursivelyUsingTLS(t *testing.T) { + td := setupHTMLFilesRecursively(t, DebugMode, true) + // Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error + tr := &http.Transport{ + TLSClientConfig: &tls.Config{ + InsecureSkipVerify: true, + }, + } + client := &http.Client{Transport: tr} + res, err := client.Get("https://127.0.0.1:9999/test") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "

Hello world

", string(resp[:])) + + td() +} + +func TestLoadHTMLFilesRecursivelyFromFuncMap(t *testing.T) { + time.Now() + td := setupHTMLFilesRecursively(t, DebugMode, false) + res, err := http.Get("http://127.0.0.1:8888/raw") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "Date: 2017/07/01\n", string(resp[:])) + + td() +} + func TestAddRoute(t *testing.T) { router := New() router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}}) From 4f3ceafa84272cd70c9fbb92bc468d47c85ae2a7 Mon Sep 17 00:00:00 2001 From: math-nao Date: Fri, 23 Mar 2018 00:10:18 +0100 Subject: [PATCH 3/6] Fix typo in comment --- gin.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gin.go b/gin.go index 32277285..f4ecdb9c 100644 --- a/gin.go +++ b/gin.go @@ -197,7 +197,7 @@ func (engine *Engine) LoadHTMLFiles(files ...string) { engine.SetHTMLTemplate(templ) } -// LoadHTMLFilesRecursively loads recursivly a slice of HTML files mathing an extension +// LoadHTMLFilesRecursively loads recursively a slice of HTML files mathing an extension // and associates the result with HTML renderer. func (engine *Engine) LoadHTMLFilesRecursively(folder string, extensionsAllowed []string) { From ee02565f7c847e9db05f4227c8e679c227706437 Mon Sep 17 00:00:00 2001 From: math-nao Date: Fri, 23 Mar 2018 01:17:51 +0100 Subject: [PATCH 4/6] Update files according go fmt --- gin.go | 8 ++++---- gin_test.go | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/gin.go b/gin.go index f4ecdb9c..00d610e2 100644 --- a/gin.go +++ b/gin.go @@ -9,10 +9,10 @@ import ( "net" "net/http" "os" - "sync" - "strings" - "sort" "path/filepath" + "sort" + "strings" + "sync" "github.com/gin-gonic/gin/render" ) @@ -208,7 +208,7 @@ func (engine *Engine) LoadHTMLFilesRecursively(folder string, extensionsAllowed extension := strings.ToLower(filepath.Ext(path)) indexFound := sort.SearchStrings(extensionsAllowed, extension) if indexFound < len(extensionsAllowed) && extensionsAllowed[indexFound] == extension { - files = append(files, path); + files = append(files, path) } return nil diff --git a/gin_test.go b/gin_test.go index 1132aa03..2acfbd9c 100644 --- a/gin_test.go +++ b/gin_test.go @@ -277,7 +277,6 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) { td() } - func TestLoadHTMLFilesRecursively(t *testing.T) { td := setupHTMLFilesRecursively(t, DebugMode, false) res, err := http.Get("http://127.0.0.1:8888/test") From f4515cbbe7d7842e20ca5d86696b64506899f1a2 Mon Sep 17 00:00:00 2001 From: math-nao Date: Fri, 23 Mar 2018 19:14:34 +0100 Subject: [PATCH 5/6] Fix issue with failing TestMode --- gin_test.go | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/gin_test.go b/gin_test.go index 2acfbd9c..32c44243 100644 --- a/gin_test.go +++ b/gin_test.go @@ -278,19 +278,6 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) { } func TestLoadHTMLFilesRecursively(t *testing.T) { - td := setupHTMLFilesRecursively(t, DebugMode, false) - res, err := http.Get("http://127.0.0.1:8888/test") - if err != nil { - fmt.Println(err) - } - - resp, _ := ioutil.ReadAll(res.Body) - assert.Equal(t, "

Hello world

", string(resp[:])) - - td() -} - -func TestLoadHTMLFilesRecursively2(t *testing.T) { td := setupHTMLFilesRecursively(t, TestMode, false) res, err := http.Get("http://127.0.0.1:8888/test") if err != nil { @@ -299,7 +286,18 @@ func TestLoadHTMLFilesRecursively2(t *testing.T) { resp, _ := ioutil.ReadAll(res.Body) assert.Equal(t, "

Hello world

", string(resp[:])) + td() +} +func TestLoadHTMLFilesRecursively2(t *testing.T) { + td := setupHTMLFilesRecursively(t, DebugMode, false) + res, err := http.Get("http://127.0.0.1:8888/test") + if err != nil { + fmt.Println(err) + } + + resp, _ := ioutil.ReadAll(res.Body) + assert.Equal(t, "

Hello world

", string(resp[:])) td() } @@ -312,12 +310,11 @@ func TestLoadHTMLFilesRecursively3(t *testing.T) { resp, _ := ioutil.ReadAll(res.Body) assert.Equal(t, "

Hello world

", string(resp[:])) - td() } func TestLoadHTMLFilesRecursivelyUsingTLS(t *testing.T) { - td := setupHTMLFilesRecursively(t, DebugMode, true) + td := setupHTMLFilesRecursively(t, TestMode, true) // Use InsecureSkipVerify for avoiding `x509: certificate signed by unknown authority` error tr := &http.Transport{ TLSClientConfig: &tls.Config{ @@ -332,13 +329,12 @@ func TestLoadHTMLFilesRecursivelyUsingTLS(t *testing.T) { resp, _ := ioutil.ReadAll(res.Body) assert.Equal(t, "

Hello world

", string(resp[:])) - td() } -func TestLoadHTMLFilesRecursivelyFromFuncMap(t *testing.T) { +func TestLoadHTMLFilesRecursivelyFuncMap(t *testing.T) { time.Now() - td := setupHTMLFilesRecursively(t, DebugMode, false) + td := setupHTMLFilesRecursively(t, TestMode, false) res, err := http.Get("http://127.0.0.1:8888/raw") if err != nil { fmt.Println(err) From 4e757db0fc5994ecff150cdf27b1600654d6230b Mon Sep 17 00:00:00 2001 From: math-nao Date: Fri, 23 Mar 2018 19:27:15 +0100 Subject: [PATCH 6/6] Update comment for using LoadHTMLFilesRecursively method --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 03327110..4720c72a 100644 --- a/README.md +++ b/README.md @@ -837,7 +837,7 @@ func main() { router.LoadHTMLGlob("templates/*") //router.LoadHTMLFiles("templates/template1.html", "templates/template2.html") - // load recursively all .html in "public" folder + // load recursively all .html files in "public" folder //router.LoadHTMLFilesRecursively("public", []string{".html"}) router.GET("/index", func(c *gin.Context) {