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 caddyhttp |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "net/http" |
| 20 | |
| 21 | "github.com/caddyserver/caddy/v2" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | caddy.RegisterModule(Subroute{}) |
| 26 | } |
| 27 | |
| 28 | // Subroute implements a handler that compiles and executes routes. |
| 29 | // This is useful for a batch of routes that all inherit the same |
| 30 | // matchers, or for multiple routes that should be treated as a |
| 31 | // single route. |
| 32 | // |
| 33 | // You can also use subroutes to handle errors from its handlers. |
| 34 | // First the primary routes will be executed, and if they return an |
| 35 | // error, the errors routes will be executed; in that case, an error |
| 36 | // is only returned to the entry point at the server if there is an |
| 37 | // additional error returned from the errors routes. |
| 38 | type Subroute struct { |
| 39 | // The primary list of routes to compile and execute. |
| 40 | Routes RouteList `json:"routes,omitempty"` |
| 41 | |
| 42 | // If the primary routes return an error, error handling |
| 43 | // can be promoted to this configuration instead. |
| 44 | Errors *HTTPErrorConfig `json:"errors,omitempty"` |
| 45 | } |
| 46 | |
| 47 | // CaddyModule returns the Caddy module information. |
| 48 | func (Subroute) CaddyModule() caddy.ModuleInfo { |
| 49 | return caddy.ModuleInfo{ |
| 50 | ID: "http.handlers.subroute", |
| 51 | New: func() caddy.Module { return new(Subroute) }, |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // Provision sets up subrouting. |
| 56 | func (sr *Subroute) Provision(ctx caddy.Context) error { |
| 57 | if sr.Routes != nil { |
| 58 | err := sr.Routes.Provision(ctx) |
| 59 | if err != nil { |
| 60 | return fmt.Errorf("setting up subroutes: %v", err) |
| 61 | } |
| 62 | if sr.Errors != nil { |
| 63 | err := sr.Errors.Routes.Provision(ctx) |
| 64 | if err != nil { |
| 65 | return fmt.Errorf("setting up error subroutes: %v", err) |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | return nil |
| 70 | } |
| 71 | |
| 72 | func (sr *Subroute) ServeHTTP(w http.ResponseWriter, r *http.Request, next Handler) error { |
| 73 | subroute := sr.Routes.Compile(next) |
| 74 | err := subroute.ServeHTTP(w, r) |
| 75 | if err != nil && sr.Errors != nil { |
| 76 | r = sr.Errors.WithError(r, err) |
| 77 | errRoute := sr.Errors.Routes.Compile(next) |
| 78 | return errRoute.ServeHTTP(w, r) |
| 79 | } |
| 80 | return err |
| 81 | } |
| 82 | |
| 83 | // Interface guards |
| 84 | var ( |
| 85 | _ caddy.Provisioner = (*Subroute)(nil) |
| 86 | _ MiddlewareHandler = (*Subroute)(nil) |
| 87 | ) |