@marcel91200 / caddy Mirror · read-only / commits / 2663dd17
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 14, 05:15 UTC

Refactor templates execution; add sprig functions

Matthew Holt committed Jun 18, 2019 at 15:17 UTC 2663dd176d38dfb929bcc478cac3913827f1246e
4 files changed +44 -27
go.mod
+6
@@ -4,10 +4,16 @@ go 1.12
4
5 require (
6 github.com/DataDog/zstd v1.4.0 // indirect
7 + github.com/Masterminds/goutils v1.1.0 // indirect
8 + github.com/Masterminds/semver v1.4.2 // indirect
9 + github.com/Masterminds/sprig v2.20.0+incompatible // indirect
10 github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb
11 github.com/dustin/go-humanize v1.0.0
12 github.com/go-acme/lego v2.6.0+incompatible
13 github.com/google/go-cmp v0.3.0 // indirect
14 + github.com/google/uuid v1.1.1 // indirect
15 + github.com/huandu/xstrings v1.2.0 // indirect
16 + github.com/imdario/mergo v0.3.7 // indirect
17 github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b
18 github.com/klauspost/cpuid v1.2.1
19 github.com/mholt/certmagic v0.6.0
go.sum
+12
@@ -1,5 +1,11 @@
1 github.com/DataDog/zstd v1.4.0 h1:vhoV+DUHnRZdKW1i5UMjAk2G4JY8wN4ayRfYDNdEhwo=
2 github.com/DataDog/zstd v1.4.0/go.mod h1:1jcaCB/ufaK+sKp1NBhlGmpz41jOoPQ35bpF36t7BBo=
3 +github.com/Masterminds/goutils v1.1.0 h1:zukEsf/1JZwCMgHiK3GZftabmxiCw4apj3a28RPBiVg=
4 +github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
5 +github.com/Masterminds/semver v1.4.2 h1:WBLTQ37jOCzSLtXNdoo8bNM8876KhNqOKvrlGITgsTc=
6 +github.com/Masterminds/semver v1.4.2/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
7 +github.com/Masterminds/sprig v2.20.0+incompatible h1:dJTKKuUkYW3RMFdQFXPU/s6hg10RgctmTjRcbZ98Ap8=
8 +github.com/Masterminds/sprig v2.20.0+incompatible/go.mod h1:y6hNFY5UBTIWBxnzTeuNhlNS5hqE0NB0E6fgfo2Br3o=
9 github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb h1:Qs8/an94NFS1x2nn7rSI+skKFuw/EGfEYAGS3w/p+jc=
10 github.com/andybalholm/brotli v0.0.0-20190430215306-5c318f9037cb/go.mod h1:+lx6/Aqd1kLJ1GQfkvOnaZ1WGmLpMpbprPuIOOZX30U=
11 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
21 github.com/golang/gddo v0.0.0-20190419222130-af0f2af80721/go.mod h1:xEhNfoBDX1hzLm2Nf80qUvZ2sVwoMZ8d6IE2SrsQfh4=
22 github.com/google/go-cmp v0.3.0 h1:crn/baboCvb5fXaQ0IJ1SGTsTVrWpDsCWC8EGETZijY=
23 github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
24 +github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
25 +github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
26 +github.com/huandu/xstrings v1.2.0 h1:yPeWdRnmynF7p+lLYz0H2tthW9lqhMJrQV/U7yy4wX0=
27 +github.com/huandu/xstrings v1.2.0/go.mod h1:DvyZB1rfVYsBIigL8HwpZgxHwXozlTgGqn63UyNX5k4=
28 +github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
29 +github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
30 github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b h1:LHpBANNM/cw1PAXJtKV9dgfp6ztOKfdGXcltGmqU9aE=
31 github.com/klauspost/compress v1.7.1-0.20190613161414-0b31f265a57b/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
32 github.com/klauspost/cpuid v1.2.0/go.mod h1:Pj4uuM528wm8OyEC2QMXAi2YiTZ96dNQPGgoMS4s3ek=
modules/caddyhttp/templates/templates.go
+3 -14
@@ -6,7 +6,6 @@ import (
6 "io"
7 "net/http"
8 "strconv"
9 - "text/template"
9
10 "github.com/caddyserver/caddy"
11 "github.com/caddyserver/caddy/modules/caddyhttp"
@@ -67,29 +66,19 @@ func (t *Templates) ServeHTTP(w http.ResponseWriter, r *http.Request, next caddy
66 // executeTemplate executes the template contianed
67 // in wb.buf and replaces it with the results.
68 func (t *Templates) executeTemplate(wb *responseBuffer, r *http.Request) error {
70 - tpl := template.New(r.URL.Path)
71 -
72 - if len(t.Delimiters) == 2 {
73 - tpl.Delims(t.Delimiters[0], t.Delimiters[1])
74 - }
75 -
76 - parsedTpl, err := tpl.Parse(wb.buf.String())
77 - if err != nil {
78 - return caddyhttp.Error(http.StatusInternalServerError, err)
79 - }
80 -
69 var fs http.FileSystem
70 if t.FileRoot != "" {
71 fs = http.Dir(t.FileRoot)
72 }
73 +
74 ctx := &templateContext{
75 Root: fs,
76 Req: r,
77 RespHeader: tplWrappedHeader{wb.Header()},
78 + config: t,
79 }
80
91 - wb.buf.Reset() // reuse buffer for output
92 - err = parsedTpl.Execute(wb.buf, ctx)
81 + err := ctx.executeTemplateInBuffer(r.URL.Path, wb.buf)
82 if err != nil {
83 return caddyhttp.Error(http.StatusInternalServerError, err)
84 }
modules/caddyhttp/templates/tplcontext.go
+23 -13
@@ -17,6 +17,7 @@ import (
17
18 "os"
19
20 + "github.com/Masterminds/sprig"
21 "github.com/caddyserver/caddy/modules/caddyhttp"
22 "gopkg.in/russross/blackfriday.v2"
23 )
@@ -27,7 +28,8 @@ type templateContext struct {
28 Req *http.Request
29 Args []interface{} // defined by arguments to .Include
30 RespHeader tplWrappedHeader
30 - server http.Handler
31 +
32 + config *Templates
33 }
34
35 // Include returns the contents of filename relative to the site root.
@@ -53,7 +55,12 @@ func (c templateContext) Include(filename string, args ...interface{}) (string,
55
56 c.Args = args
57
56 - return c.executeTemplate(filename, bodyBuf.Bytes())
58 + err = c.executeTemplateInBuffer(filename, bodyBuf)
59 + if err != nil {
60 + return "", err
61 + }
62 +
63 + return bodyBuf.String(), nil
64 }
65
66 // HTTPInclude returns the body of a virtual (lightweight) request
@@ -81,25 +88,28 @@ func (c templateContext) HTTPInclude(uri string) (string, error) {
88 return "", fmt.Errorf("http %d", vrw.status)
89 }
90
84 - return c.executeTemplate(uri, buf.Bytes())
85 -}
86 -
87 -func (c templateContext) executeTemplate(tplName string, body []byte) (string, error) {
88 - tpl, err := template.New(tplName).Parse(string(body))
91 + err = c.executeTemplateInBuffer(uri, buf)
92 if err != nil {
93 return "", err
94 }
95
93 - buf := bufPool.Get().(*bytes.Buffer)
94 - buf.Reset()
95 - defer bufPool.Put(buf)
96 + return buf.String(), nil
97 +}
98 +
99 +func (c templateContext) executeTemplateInBuffer(tplName string, buf *bytes.Buffer) error {
100 + tpl := template.New(tplName).Funcs(sprig.TxtFuncMap())
101 + if len(c.config.Delimiters) == 2 {
102 + tpl.Delims(c.config.Delimiters[0], c.config.Delimiters[1])
103 + }
104
97 - err = tpl.Execute(buf, c)
105 + parsedTpl, err := tpl.Parse(buf.String())
106 if err != nil {
99 - return "", err
107 + return err
108 }
109
102 - return buf.String(), nil
110 + buf.Reset() // reuse buffer for output
111 +
112 + return parsedTpl.Execute(buf, c)
113 }
114
115 // Now returns the current timestamp.