mirror of
https://github.com/gin-gonic/gin.git
synced 2025-04-06 03:57:46 +08:00
Merge eb4a5e18667a522caacfb4a53520dbec98343523 into 8763f33c65f7df8be5b9fe7504ab7fcf20abb41d
This commit is contained in:
commit
4d77c744b2
@ -1393,13 +1393,19 @@ func main() {
|
|||||||
|
|
||||||
### HTML rendering
|
### HTML rendering
|
||||||
|
|
||||||
Using LoadHTMLGlob() or LoadHTMLFiles()
|
Using LoadHTMLGlob() or LoadHTMLFiles() or LoadHTMLFS()
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
//go:embed templates/*
|
||||||
|
var templates embed.FS
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
router.LoadHTMLGlob("templates/*")
|
router.LoadHTMLGlob("templates/*")
|
||||||
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
|
//router.LoadHTMLFiles("templates/template1.html", "templates/template2.html")
|
||||||
|
//router.LoadHTMLFS(http.Dir("templates"), "template1.html", "template2.html")
|
||||||
|
//or
|
||||||
|
//router.LoadHTMLFS(http.FS(templates), "templates/template1.html", "templates/template2.html")
|
||||||
router.GET("/index", func(c *gin.Context) {
|
router.GET("/index", func(c *gin.Context) {
|
||||||
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
||||||
"title": "Main website",
|
"title": "Main website",
|
||||||
|
14
gin.go
14
gin.go
@ -16,6 +16,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin/internal/bytesconv"
|
"github.com/gin-gonic/gin/internal/bytesconv"
|
||||||
|
filesystem "github.com/gin-gonic/gin/internal/fs"
|
||||||
"github.com/gin-gonic/gin/render"
|
"github.com/gin-gonic/gin/render"
|
||||||
|
|
||||||
"github.com/quic-go/quic-go/http3"
|
"github.com/quic-go/quic-go/http3"
|
||||||
@ -285,6 +286,19 @@ func (engine *Engine) LoadHTMLFiles(files ...string) {
|
|||||||
engine.SetHTMLTemplate(templ)
|
engine.SetHTMLTemplate(templ)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadHTMLFS loads an http.FileSystem and a slice of patterns
|
||||||
|
// and associates the result with HTML renderer.
|
||||||
|
func (engine *Engine) LoadHTMLFS(fs http.FileSystem, patterns ...string) {
|
||||||
|
if IsDebugging() {
|
||||||
|
engine.HTMLRender = render.HTMLDebug{FileSystem: fs, Patterns: patterns, FuncMap: engine.FuncMap, Delims: engine.delims}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
templ := template.Must(template.New("").Delims(engine.delims.Left, engine.delims.Right).Funcs(engine.FuncMap).ParseFS(
|
||||||
|
filesystem.FileSystem{FileSystem: fs}, patterns...))
|
||||||
|
engine.SetHTMLTemplate(templ)
|
||||||
|
}
|
||||||
|
|
||||||
// SetHTMLTemplate associate a template with HTML renderer.
|
// SetHTMLTemplate associate a template with HTML renderer.
|
||||||
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
func (engine *Engine) SetHTMLTemplate(templ *template.Template) {
|
||||||
if len(engine.trees) > 0 {
|
if len(engine.trees) > 0 {
|
||||||
|
@ -32,6 +32,11 @@ func LoadHTMLFiles(files ...string) {
|
|||||||
engine().LoadHTMLFiles(files...)
|
engine().LoadHTMLFiles(files...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LoadHTMLFS is a wrapper for Engine.LoadHTMLFS.
|
||||||
|
func LoadHTMLFS(fs http.FileSystem, patterns ...string) {
|
||||||
|
engine().LoadHTMLFS(fs, patterns...)
|
||||||
|
}
|
||||||
|
|
||||||
// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
|
// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.
|
||||||
func SetHTMLTemplate(templ *template.Template) {
|
func SetHTMLTemplate(templ *template.Template) {
|
||||||
engine().SetHTMLTemplate(templ)
|
engine().SetHTMLTemplate(templ)
|
||||||
|
109
gin_test.go
109
gin_test.go
@ -325,6 +325,115 @@ func TestLoadHTMLFilesFuncMap(t *testing.T) {
|
|||||||
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var tmplFS = http.Dir("testdata/template")
|
||||||
|
|
||||||
|
func TestLoadHTMLFSTestMode(t *testing.T) {
|
||||||
|
ts := setupHTMLFiles(
|
||||||
|
t,
|
||||||
|
TestMode,
|
||||||
|
false,
|
||||||
|
func(router *Engine) {
|
||||||
|
router.LoadHTMLFS(tmplFS, "hello.tmpl", "raw.tmpl")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFSDebugMode(t *testing.T) {
|
||||||
|
ts := setupHTMLFiles(
|
||||||
|
t,
|
||||||
|
DebugMode,
|
||||||
|
false,
|
||||||
|
func(router *Engine) {
|
||||||
|
router.LoadHTMLFS(tmplFS, "hello.tmpl", "raw.tmpl")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFSReleaseMode(t *testing.T) {
|
||||||
|
ts := setupHTMLFiles(
|
||||||
|
t,
|
||||||
|
ReleaseMode,
|
||||||
|
false,
|
||||||
|
func(router *Engine) {
|
||||||
|
router.LoadHTMLFS(tmplFS, "hello.tmpl", "raw.tmpl")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(fmt.Sprintf("%s/test", ts.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFSUsingTLS(t *testing.T) {
|
||||||
|
ts := setupHTMLFiles(
|
||||||
|
t,
|
||||||
|
TestMode,
|
||||||
|
true,
|
||||||
|
func(router *Engine) {
|
||||||
|
router.LoadHTMLFS(tmplFS, "hello.tmpl", "raw.tmpl")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
// 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(fmt.Sprintf("%s/test", ts.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestLoadHTMLFSFuncMap(t *testing.T) {
|
||||||
|
ts := setupHTMLFiles(
|
||||||
|
t,
|
||||||
|
TestMode,
|
||||||
|
false,
|
||||||
|
func(router *Engine) {
|
||||||
|
router.LoadHTMLFS(tmplFS, "hello.tmpl", "raw.tmpl")
|
||||||
|
},
|
||||||
|
)
|
||||||
|
defer ts.Close()
|
||||||
|
|
||||||
|
res, err := http.Get(fmt.Sprintf("%s/raw", ts.URL))
|
||||||
|
if err != nil {
|
||||||
|
t.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, _ := io.ReadAll(res.Body)
|
||||||
|
assert.Equal(t, "Date: 2017/07/01", string(resp))
|
||||||
|
}
|
||||||
|
|
||||||
func TestAddRoute(t *testing.T) {
|
func TestAddRoute(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
router.addRoute(http.MethodGet, "/", HandlersChain{func(_ *Context) {}})
|
router.addRoute(http.MethodGet, "/", HandlersChain{func(_ *Context) {}})
|
||||||
|
22
internal/fs/fs.go
Normal file
22
internal/fs/fs.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"io/fs"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FileSystem implements an [fs.FS].
|
||||||
|
type FileSystem struct {
|
||||||
|
http.FileSystem
|
||||||
|
}
|
||||||
|
|
||||||
|
// Open passes `Open` to the upstream implementation and return an [fs.File].
|
||||||
|
func (o FileSystem) Open(name string) (fs.File, error) {
|
||||||
|
f, err := o.FileSystem.Open(name)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return fs.File(f), nil
|
||||||
|
}
|
49
internal/fs/fs_test.go
Normal file
49
internal/fs/fs_test.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package fs
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
type mockFileSystem struct {
|
||||||
|
open func(name string) (http.File, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *mockFileSystem) Open(name string) (http.File, error) {
|
||||||
|
return m.open(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TesFileSystem_Open(t *testing.T) {
|
||||||
|
var testFile *os.File
|
||||||
|
mockFS := &mockFileSystem{
|
||||||
|
open: func(name string) (http.File, error) {
|
||||||
|
return testFile, nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fs := &FileSystem{mockFS}
|
||||||
|
|
||||||
|
file, err := fs.Open("foo")
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, testFile, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFileSystem_Open_err(t *testing.T) {
|
||||||
|
testError := errors.New("mock")
|
||||||
|
mockFS := &mockFileSystem{
|
||||||
|
open: func(_ string) (http.File, error) {
|
||||||
|
return nil, testError
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fs := &FileSystem{mockFS}
|
||||||
|
|
||||||
|
file, err := fs.Open("foo")
|
||||||
|
|
||||||
|
require.ErrorIs(t, err, testError)
|
||||||
|
assert.Nil(t, file)
|
||||||
|
}
|
@ -7,6 +7,8 @@ package render
|
|||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/gin-gonic/gin/internal/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Delims represents a set of Left and Right delimiters for HTML template rendering.
|
// Delims represents a set of Left and Right delimiters for HTML template rendering.
|
||||||
@ -33,6 +35,8 @@ type HTMLProduction struct {
|
|||||||
type HTMLDebug struct {
|
type HTMLDebug struct {
|
||||||
Files []string
|
Files []string
|
||||||
Glob string
|
Glob string
|
||||||
|
FileSystem http.FileSystem
|
||||||
|
Patterns []string
|
||||||
Delims Delims
|
Delims Delims
|
||||||
FuncMap template.FuncMap
|
FuncMap template.FuncMap
|
||||||
}
|
}
|
||||||
@ -73,7 +77,11 @@ func (r HTMLDebug) loadTemplate() *template.Template {
|
|||||||
if r.Glob != "" {
|
if r.Glob != "" {
|
||||||
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseGlob(r.Glob))
|
||||||
}
|
}
|
||||||
panic("the HTML debug render was created without files or glob pattern")
|
if r.FileSystem != nil && len(r.Patterns) > 0 {
|
||||||
|
return template.Must(template.New("").Delims(r.Delims.Left, r.Delims.Right).Funcs(r.FuncMap).ParseFS(
|
||||||
|
fs.FileSystem{FileSystem: r.FileSystem}, r.Patterns...))
|
||||||
|
}
|
||||||
|
panic("the HTML debug render was created without files or glob pattern or file system with patterns")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Render (HTML) executes template and writes its result with custom ContentType for response.
|
// Render (HTML) executes template and writes its result with custom ContentType for response.
|
||||||
|
@ -491,6 +491,8 @@ func TestRenderHTMLDebugFiles(t *testing.T) {
|
|||||||
htmlRender := HTMLDebug{
|
htmlRender := HTMLDebug{
|
||||||
Files: []string{"../testdata/template/hello.tmpl"},
|
Files: []string{"../testdata/template/hello.tmpl"},
|
||||||
Glob: "",
|
Glob: "",
|
||||||
|
FileSystem: nil,
|
||||||
|
Patterns: nil,
|
||||||
Delims: Delims{Left: "{[{", Right: "}]}"},
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
||||||
FuncMap: nil,
|
FuncMap: nil,
|
||||||
}
|
}
|
||||||
@ -510,6 +512,29 @@ func TestRenderHTMLDebugGlob(t *testing.T) {
|
|||||||
htmlRender := HTMLDebug{
|
htmlRender := HTMLDebug{
|
||||||
Files: nil,
|
Files: nil,
|
||||||
Glob: "../testdata/template/hello*",
|
Glob: "../testdata/template/hello*",
|
||||||
|
FileSystem: nil,
|
||||||
|
Patterns: nil,
|
||||||
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
||||||
|
FuncMap: nil,
|
||||||
|
}
|
||||||
|
instance := htmlRender.Instance("hello.tmpl", map[string]any{
|
||||||
|
"name": "thinkerou",
|
||||||
|
})
|
||||||
|
|
||||||
|
err := instance.Render(w)
|
||||||
|
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "<h1>Hello thinkerou</h1>", w.Body.String())
|
||||||
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestRenderHTMLDebugFS(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
htmlRender := HTMLDebug{
|
||||||
|
Files: nil,
|
||||||
|
Glob: "",
|
||||||
|
FileSystem: http.Dir("../testdata/template"),
|
||||||
|
Patterns: []string{"hello.tmpl"},
|
||||||
Delims: Delims{Left: "{[{", Right: "}]}"},
|
Delims: Delims{Left: "{[{", Right: "}]}"},
|
||||||
FuncMap: nil,
|
FuncMap: nil,
|
||||||
}
|
}
|
||||||
@ -528,6 +553,8 @@ func TestRenderHTMLDebugPanics(t *testing.T) {
|
|||||||
htmlRender := HTMLDebug{
|
htmlRender := HTMLDebug{
|
||||||
Files: nil,
|
Files: nil,
|
||||||
Glob: "",
|
Glob: "",
|
||||||
|
FileSystem: nil,
|
||||||
|
Patterns: nil,
|
||||||
Delims: Delims{"{{", "}}"},
|
Delims: Delims{"{{", "}}"},
|
||||||
FuncMap: nil,
|
FuncMap: nil,
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user