From f9d93ead4ef6e099ba7e00318dce6509b0f1eda4 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 14 May 2019 14:14:05 -0600 Subject: Rename and export some types, other minor changes --- modules/caddyhttp/routes.go | 84 +++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 38 deletions(-) (limited to 'modules/caddyhttp/routes.go') diff --git a/modules/caddyhttp/routes.go b/modules/caddyhttp/routes.go index 48a5000..cd612ac 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 -} -- cgit v1.2.3