summaryrefslogtreecommitdiffhomepage
path: root/modules/caddyhttp/routes.go
diff options
context:
space:
mode:
authorMatthew Holt <[email protected]>2019-05-14 14:14:05 -0600
committerMatthew Holt <[email protected]>2019-05-14 14:14:05 -0600
commitf9d93ead4ef6e099ba7e00318dce6509b0f1eda4 (patch)
treeb14b418475d25c1aa4d13cb53b5e162054570483 /modules/caddyhttp/routes.go
parent8ae0d6a509fd1b871457cf742369af04346933a8 (diff)
downloadcaddy-f9d93ead4ef6e099ba7e00318dce6509b0f1eda4.tar.gz
caddy-f9d93ead4ef6e099ba7e00318dce6509b0f1eda4.zip
Rename and export some types, other minor changes
Diffstat (limited to 'modules/caddyhttp/routes.go')
-rw-r--r--modules/caddyhttp/routes.go84
1 files changed, 46 insertions, 38 deletions
diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go
index 48a5000aa..cd612ac72 100644
--- a/modules/caddyhttp/routes.go
+++ b/modules/caddyhttp/routes.go
@@ -8,7 +8,10 @@ import (
"bitbucket.org/lightcodelabs/caddy2"
)
-type serverRoute struct {
+// ServerRoute represents a set of matching rules,
+// middlewares, and a responder for handling HTTP
+// requests.
+type ServerRoute struct {
Matchers map[string]json.RawMessage `json:"match"`
Apply []json.RawMessage `json:"apply"`
Respond json.RawMessage `json:"respond"`
@@ -21,9 +24,49 @@ type serverRoute struct {
responder Handler
}
-type routeList []serverRoute
+// RouteList is a list of server routes that can
+// create a middleware chain.
+type RouteList []ServerRoute
-func (routes routeList) buildMiddlewareChain(w http.ResponseWriter, r *http.Request) Handler {
+// Provision sets up all the routes by loading the modules.
+func (routes RouteList) Provision() error {
+ for i, route := range routes {
+ // matchers
+ for modName, rawMsg := range route.Matchers {
+ val, err := caddy2.LoadModule("http.matchers."+modName, rawMsg)
+ if err != nil {
+ return fmt.Errorf("loading matcher module '%s': %v", modName, err)
+ }
+ routes[i].matchers = append(routes[i].matchers, val.(RouteMatcher))
+ }
+ routes[i].Matchers = nil // allow GC to deallocate - TODO: Does this help?
+
+ // middleware
+ for j, rawMsg := range route.Apply {
+ mid, err := caddy2.LoadModuleInline("middleware", "http.middleware", rawMsg)
+ if err != nil {
+ return fmt.Errorf("loading middleware module in position %d: %v", j, err)
+ }
+ routes[i].middleware = append(routes[i].middleware, mid.(MiddlewareHandler))
+ }
+ routes[i].Apply = nil // allow GC to deallocate - TODO: Does this help?
+
+ // responder
+ if route.Respond != nil {
+ resp, err := caddy2.LoadModuleInline("responder", "http.responders", route.Respond)
+ if err != nil {
+ return fmt.Errorf("loading responder module: %v", err)
+ }
+ routes[i].responder = resp.(Handler)
+ }
+ routes[i].Respond = nil // allow GC to deallocate - TODO: Does this help?
+ }
+ return nil
+}
+
+// BuildHandlerChain creates a chain of handlers by
+// applying all the matching routes.
+func (routes RouteList) BuildHandlerChain(w http.ResponseWriter, r *http.Request) Handler {
if len(routes) == 0 {
return emptyHandler
}
@@ -68,38 +111,3 @@ routeLoop:
return stack
}
-
-func (routes routeList) setup() error {
- for i, route := range routes {
- // matchers
- for modName, rawMsg := range route.Matchers {
- val, err := caddy2.LoadModule("http.matchers."+modName, rawMsg)
- if err != nil {
- return fmt.Errorf("loading matcher module '%s': %v", modName, err)
- }
- routes[i].matchers = append(routes[i].matchers, val.(RouteMatcher))
- }
- routes[i].Matchers = nil // allow GC to deallocate - TODO: Does this help?
-
- // middleware
- for j, rawMsg := range route.Apply {
- mid, err := caddy2.LoadModuleInline("middleware", "http.middleware", rawMsg)
- if err != nil {
- return fmt.Errorf("loading middleware module in position %d: %v", j, err)
- }
- routes[i].middleware = append(routes[i].middleware, mid.(MiddlewareHandler))
- }
- routes[i].Apply = nil // allow GC to deallocate - TODO: Does this help?
-
- // responder
- if route.Respond != nil {
- resp, err := caddy2.LoadModuleInline("responder", "http.responders", route.Respond)
- if err != nil {
- return fmt.Errorf("loading responder module: %v", err)
- }
- routes[i].responder = resp.(Handler)
- }
- routes[i].Respond = nil // allow GC to deallocate - TODO: Does this help?
- }
- return nil
-}