mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-27 06:03:48 +03:00
Refactor templates execution; add sprig functions
This commit is contained in:
parent
6706c9225a
commit
2663dd176d
4 changed files with 47 additions and 30 deletions
6
go.mod
6
go.mod
|
@ -4,10 +4,16 @@ go 1.12
|
|||
|
||||
require (
|
||||
github.com/DataDog/zstd v1.4.0 // indirect
|
||||
github.com/Masterminds/goutils v1.1.0 // indirect
|
||||
github.com/Masterminds/semver v1.4.2 // indirect
|
||||
github.com/Masterminds/sprig v2.20.0+incompatible // indirect
|
||||
github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb
|
||||
github.com/dustin/go-humanize v1.0.0
|
||||
github.com/go-acme/lego v2.6.0+incompatible
|
||||
github.com/google/go-cmp v0.3.0 // indirect
|
||||
github.com/google/uuid v1.1.1 // indirect
|
||||
github.com/huandu/xstrings v1.2.0 // indirect
|
||||
github.com/imdario/mergo v0.3.7 // indirect
|
||||
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b
|
||||
github.com/klauspost/cpuid v1.2.1
|
||||
github.com/mholt/certmagic v0.6.0
|
||||
|
|
12
go.sum
12
go.sum
|
@ -1,5 +1,11 @@
|
|||
github.com/DataDog/zstd v1.4.0 h1:vhoV+DUHnRZdKW1i5UMjAk2G4JY8wN4ayRfYDNdEhwo=
|
||||
github.com/DataDog/zstd v1.4.0/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
|
||||
github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
|
||||
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
|
||||
github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
|
||||
github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
|
||||
github.com/Masterminds/sprig v2.20.0+incompatible h1:dJTKKuUkYW3RMFdQFXPU/s6hg10RgctmTjRcbZ98Ap8=
|
||||
github.com/Masterminds/sprig v2.20.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
|
||||
github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb h1:Qs8/an94NFS1x2nn7rSI+skKFuw/EGfEYAGS3w/p+jc=
|
||||
github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb/go.mod h1:+lx6/Aqd1kLJ1GQfkvOnaZ1WGmLpMpbprPuIOOZX30U=
|
||||
github.com/cenkalti/backoff v2.1.1+incompatible h1:tKJnvO2kl0zmb/jA5UKAt4VoEVw1qxKWjE/Bpp46npY=
|
||||
|
@ -15,6 +21,12 @@ github.com/golang/gddo v0.0.0-20190419222130-af0f2af80721 h1:KRMr9A3qfbVM7iV/WcL
|
|||
github.com/golang/gddo v0.0.0-20190419222130-af0f2af80721/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
|
||||
github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
|
||||
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
|
||||
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
|
||||
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
|
||||
github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
|
||||
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
|
||||
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
|
||||
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b h1:LHpBANNM/cw1PAXJtKV9dgfp6ztOKfdGXcltGmqU9aE=
|
||||
github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
|
||||
github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
|
||||
|
|
|
@ -6,7 +6,6 @@ import (
|
|||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
"github.com/caddyserver/caddy"
|
||||
"github.com/caddyserver/caddy/modules/caddyhttp"
|
||||
|
@ -67,29 +66,19 @@ func (t *Templates) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
|
|||
// executeTemplate executes the template contianed
|
||||
// in wb.buf and replaces it with the results.
|
||||
func (t *Templates) executeTemplate(wb *responseBuffer, r *http.Request) error {
|
||||
tpl := template.New(r.URL.Path)
|
||||
|
||||
if len(t.Delimiters) == 2 {
|
||||
tpl.Delims(t.Delimiters[0], t.Delimiters[1])
|
||||
}
|
||||
|
||||
parsedTpl, err := tpl.Parse(wb.buf.String())
|
||||
if err != nil {
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
||||
var fs http.FileSystem
|
||||
if t.FileRoot != "" {
|
||||
fs = http.Dir(t.FileRoot)
|
||||
}
|
||||
|
||||
ctx := &templateContext{
|
||||
Root: fs,
|
||||
Req: r,
|
||||
RespHeader: tplWrappedHeader{wb.Header()},
|
||||
config: t,
|
||||
}
|
||||
|
||||
wb.buf.Reset() // reuse buffer for output
|
||||
err = parsedTpl.Execute(wb.buf, ctx)
|
||||
err := ctx.executeTemplateInBuffer(r.URL.Path, wb.buf)
|
||||
if err != nil {
|
||||
return caddyhttp.Error(http.StatusInternalServerError, err)
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@ import (
|
|||
|
||||
"os"
|
||||
|
||||
"github.com/Masterminds/sprig"
|
||||
"github.com/caddyserver/caddy/modules/caddyhttp"
|
||||
"gopkg.in/russross/blackfriday.v2"
|
||||
)
|
||||
|
@ -27,7 +28,8 @@ type templateContext struct {
|
|||
Req *http.Request
|
||||
Args []interface{} // defined by arguments to .Include
|
||||
RespHeader tplWrappedHeader
|
||||
server http.Handler
|
||||
|
||||
config *Templates
|
||||
}
|
||||
|
||||
// Include returns the contents of filename relative to the site root.
|
||||
|
@ -53,7 +55,12 @@ func (c templateContext) Include(filename string, args ...interface{}) (string,
|
|||
|
||||
c.Args = args
|
||||
|
||||
return c.executeTemplate(filename, bodyBuf.Bytes())
|
||||
err = c.executeTemplateInBuffer(filename, bodyBuf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return bodyBuf.String(), nil
|
||||
}
|
||||
|
||||
// HTTPInclude returns the body of a virtual (lightweight) request
|
||||
|
@ -81,20 +88,7 @@ func (c templateContext) HTTPInclude(uri string) (string, error) {
|
|||
return "", fmt.Errorf("http %d", vrw.status)
|
||||
}
|
||||
|
||||
return c.executeTemplate(uri, buf.Bytes())
|
||||
}
|
||||
|
||||
func (c templateContext) executeTemplate(tplName string, body []byte) (string, error) {
|
||||
tpl, err := template.New(tplName).Parse(string(body))
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
buf := bufPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
defer bufPool.Put(buf)
|
||||
|
||||
err = tpl.Execute(buf, c)
|
||||
err = c.executeTemplateInBuffer(uri, buf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -102,6 +96,22 @@ func (c templateContext) executeTemplate(tplName string, body []byte) (string, e
|
|||
return buf.String(), nil
|
||||
}
|
||||
|
||||
func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buffer) error {
|
||||
tpl := template.New(tplName).Funcs(sprig.TxtFuncMap())
|
||||
if len(c.config.Delimiters) == 2 {
|
||||
tpl.Delims(c.config.Delimiters[0], c.config.Delimiters[1])
|
||||
}
|
||||
|
||||
parsedTpl, err := tpl.Parse(buf.String())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
buf.Reset() // reuse buffer for output
|
||||
|
||||
return parsedTpl.Execute(buf, c)
|
||||
}
|
||||
|
||||
// Now returns the current timestamp.
|
||||
func (c templateContext) Now() time.Time {
|
||||
return time.Now()
|
||||
|
|
Loading…
Reference in a new issue