2015-04-28 00:56:57 +08:00
|
|
|
// Package templates implements template execution for files to be dynamically rendered for the client.
|
2015-04-18 23:57:51 +08:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2015-04-22 11:36:30 +08:00
|
|
|
"bytes"
|
2015-04-18 23:57:51 +08:00
|
|
|
"net/http"
|
2015-07-07 13:37:27 +08:00
|
|
|
"os"
|
2015-04-18 23:57:51 +08:00
|
|
|
"path"
|
2015-05-06 05:50:42 +08:00
|
|
|
"path/filepath"
|
2015-04-19 02:28:22 +08:00
|
|
|
"text/template"
|
2015-04-18 23:57:51 +08:00
|
|
|
|
|
|
|
"github.com/mholt/caddy/middleware"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ServeHTTP implements the middleware.Handler interface.
|
|
|
|
func (t Templates) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
|
|
|
for _, rule := range t.Rules {
|
|
|
|
if !middleware.Path(r.URL.Path).Matches(rule.Path) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-05-06 05:50:42 +08:00
|
|
|
// Check for index files
|
|
|
|
fpath := r.URL.Path
|
|
|
|
if idx, ok := middleware.IndexFile(t.FileSys, fpath, rule.IndexFiles); ok {
|
|
|
|
fpath = idx
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the extension
|
|
|
|
reqExt := path.Ext(fpath)
|
2015-04-18 23:57:51 +08:00
|
|
|
|
|
|
|
for _, ext := range rule.Extensions {
|
|
|
|
if reqExt == ext {
|
|
|
|
// Create execution context
|
2015-07-18 02:07:24 +08:00
|
|
|
ctx := middleware.Context{Root: t.FileSys, Req: r, URL: r.URL}
|
2015-04-18 23:57:51 +08:00
|
|
|
|
|
|
|
// Build the template
|
2015-05-06 05:50:42 +08:00
|
|
|
tpl, err := template.ParseFiles(filepath.Join(t.Root, fpath))
|
2015-04-18 23:57:51 +08:00
|
|
|
if err != nil {
|
2015-07-07 13:37:27 +08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return http.StatusNotFound, nil
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return http.StatusForbidden, nil
|
|
|
|
}
|
2015-04-18 23:57:51 +08:00
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute it
|
2015-04-22 11:36:30 +08:00
|
|
|
var buf bytes.Buffer
|
2015-04-22 12:30:47 +08:00
|
|
|
err = tpl.Execute(&buf, ctx)
|
2015-04-18 23:57:51 +08:00
|
|
|
if err != nil {
|
|
|
|
return http.StatusInternalServerError, err
|
|
|
|
}
|
2015-04-22 11:36:30 +08:00
|
|
|
buf.WriteTo(w)
|
2015-04-18 23:57:51 +08:00
|
|
|
|
|
|
|
return http.StatusOK, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Next.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Templates is middleware to render templated files as the HTTP response.
|
|
|
|
type Templates struct {
|
2015-05-06 05:50:42 +08:00
|
|
|
Next middleware.Handler
|
|
|
|
Rules []Rule
|
|
|
|
Root string
|
|
|
|
FileSys http.FileSystem
|
2015-04-18 23:57:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rule represents a template rule. A template will only execute
|
|
|
|
// with this rule if the request path matches the Path specified
|
|
|
|
// and requests a resource with one of the extensions specified.
|
|
|
|
type Rule struct {
|
|
|
|
Path string
|
|
|
|
Extensions []string
|
2015-05-06 05:50:42 +08:00
|
|
|
IndexFiles []string
|
2015-04-18 23:57:51 +08:00
|
|
|
}
|