mirror of
https://github.com/caddyserver/caddy.git
synced 2024-11-24 05:55:11 +08:00
More template love
This commit is contained in:
parent
55801b48ec
commit
f5d0ed5b1c
|
@ -2,8 +2,12 @@ package templates
|
|||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
)
|
||||
|
||||
// This file contains the context and functions available for
|
||||
|
@ -13,6 +17,7 @@ import (
|
|||
type context struct {
|
||||
root http.FileSystem
|
||||
req *http.Request
|
||||
URL *url.URL
|
||||
}
|
||||
|
||||
// Include returns the contents of filename relative to the site root
|
||||
|
@ -50,3 +55,40 @@ func (c context) Header(name string) string {
|
|||
func (c context) RemoteAddr() string {
|
||||
return c.req.RemoteAddr
|
||||
}
|
||||
|
||||
// URI returns the raw, unprocessed request URI (including query
|
||||
// string and hash) obtained directly from the Request-Line of
|
||||
// the HTTP request.
|
||||
func (c context) URI() string {
|
||||
return c.req.RequestURI
|
||||
}
|
||||
|
||||
// Host returns the hostname portion of the Host header
|
||||
// from the HTTP request.
|
||||
func (c context) Host() (string, error) {
|
||||
host, _, err := net.SplitHostPort(c.req.Host)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return host, nil
|
||||
}
|
||||
|
||||
// Port returns the port portion of the Host header if specified.
|
||||
func (c context) Port() (string, error) {
|
||||
_, port, err := net.SplitHostPort(c.req.Host)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return port, nil
|
||||
}
|
||||
|
||||
// Method returns the method (GET, POST, etc.) of the request.
|
||||
func (c context) Method() string {
|
||||
return c.req.Method
|
||||
}
|
||||
|
||||
// PathMatches returns true if the path portion of the request
|
||||
// URL matches pattern.
|
||||
func (c context) PathMatches(pattern string) bool {
|
||||
return middleware.Path(c.req.URL.Path).Matches(pattern)
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
|
|||
for _, ext := range rule.Extensions {
|
||||
if reqExt == ext {
|
||||
// Create execution context
|
||||
ctx := context{root: http.Dir(t.Root), req: r}
|
||||
ctx := context{root: http.Dir(t.Root), req: r, URL: r.URL}
|
||||
|
||||
// Build the template
|
||||
tpl, err := template.ParseFiles(t.Root + r.URL.Path)
|
||||
|
|
Loading…
Reference in New Issue
Block a user