0
Fork 0
mirror of https://github.com/caddyserver/caddy.git synced 2024-12-23 22:27:38 -05:00
caddy/config/setup/templates.go

62 lines
1.3 KiB
Go
Raw Normal View History

2015-05-04 12:49:49 -05:00
package setup
import (
"net/http"
2015-05-04 12:49:49 -05:00
"github.com/mholt/caddy/middleware"
"github.com/mholt/caddy/middleware/templates"
)
// Templates configures a new Templates middleware instance.
func Templates(c *Controller) (middleware.Middleware, error) {
rules, err := templatesParse(c)
if err != nil {
return nil, err
}
tmpls := templates.Templates{
Rules: rules,
Root: c.Root,
FileSys: http.Dir(c.Root),
2015-05-04 12:49:49 -05:00
}
return func(next middleware.Handler) middleware.Handler {
tmpls.Next = next
return tmpls
}, nil
}
func templatesParse(c *Controller) ([]templates.Rule, error) {
var rules []templates.Rule
for c.Next() {
var rule templates.Rule
if c.NextArg() {
// First argument would be the path
rule.Path = c.Val()
// Any remaining arguments are extensions
rule.Extensions = c.RemainingArgs()
if len(rule.Extensions) == 0 {
2015-06-26 17:19:44 -05:00
rule.Extensions = defaultTemplateExtensions
2015-05-04 12:49:49 -05:00
}
} else {
2015-06-26 17:19:44 -05:00
rule.Path = defaultTemplatePath
rule.Extensions = defaultTemplateExtensions
2015-05-04 12:49:49 -05:00
}
for _, ext := range rule.Extensions {
rule.IndexFiles = append(rule.IndexFiles, "index"+ext)
}
2015-05-04 12:49:49 -05:00
rules = append(rules, rule)
}
return rules, nil
}
2015-06-26 17:19:44 -05:00
const defaultTemplatePath = "/"
2015-05-04 12:49:49 -05:00
2015-06-26 17:19:44 -05:00
var defaultTemplateExtensions = []string{".html", ".htm", ".tmpl", ".tpl", ".txt"}