Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 11, 01:00 UTC
| 1 | // Copyright 2015 Matthew Holt and The Caddy Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package templates |
| 16 | |
| 17 | import ( |
| 18 | "github.com/caddyserver/caddy/v2" |
| 19 | "github.com/caddyserver/caddy/v2/caddyconfig" |
| 20 | "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 21 | "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" |
| 22 | "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | httpcaddyfile.RegisterHandlerDirective("templates", parseCaddyfile) |
| 27 | } |
| 28 | |
| 29 | // parseCaddyfile sets up the handler from Caddyfile tokens. Syntax: |
| 30 | // |
| 31 | // templates [<matcher>] { |
| 32 | // mime <types...> |
| 33 | // between <open_delim> <close_delim> |
| 34 | // root <path> |
| 35 | // } |
| 36 | func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) { |
| 37 | h.Next() // consume directive name |
| 38 | t := new(Templates) |
| 39 | for h.NextBlock(0) { |
| 40 | switch h.Val() { |
| 41 | case "mime": |
| 42 | t.MIMETypes = h.RemainingArgs() |
| 43 | if len(t.MIMETypes) == 0 { |
| 44 | return nil, h.ArgErr() |
| 45 | } |
| 46 | case "between": |
| 47 | t.Delimiters = h.RemainingArgs() |
| 48 | if len(t.Delimiters) != 2 { |
| 49 | return nil, h.ArgErr() |
| 50 | } |
| 51 | case "root": |
| 52 | if !h.Args(&t.FileRoot) { |
| 53 | return nil, h.ArgErr() |
| 54 | } |
| 55 | case "extensions": |
| 56 | if h.NextArg() { |
| 57 | return nil, h.ArgErr() |
| 58 | } |
| 59 | if t.ExtensionsRaw != nil { |
| 60 | return nil, h.Err("extensions already specified") |
| 61 | } |
| 62 | for nesting := h.Nesting(); h.NextBlock(nesting); { |
| 63 | extensionModuleName := h.Val() |
| 64 | modID := "http.handlers.templates.functions." + extensionModuleName |
| 65 | unm, err := caddyfile.UnmarshalModule(h.Dispenser, modID) |
| 66 | if err != nil { |
| 67 | return nil, err |
| 68 | } |
| 69 | cf, ok := unm.(CustomFunctions) |
| 70 | if !ok { |
| 71 | return nil, h.Errf("module %s (%T) does not provide template functions", modID, unm) |
| 72 | } |
| 73 | if t.ExtensionsRaw == nil { |
| 74 | t.ExtensionsRaw = make(caddy.ModuleMap) |
| 75 | } |
| 76 | t.ExtensionsRaw[extensionModuleName] = caddyconfig.JSON(cf, nil) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return t, nil |
| 81 | } |