diff --git a/call.go b/call.go index 40652c6..05370ea 100644 --- a/call.go +++ b/call.go @@ -36,17 +36,18 @@ type raw struct { // values and uses the same method for getting values from the request and setting // values on the response by having optional values. type Call struct { - id string - config *Config - status int - statusWritten bool - w http.ResponseWriter - req *http.Request - pathParams map[string]string - bodyBytes []byte - charset string - session session.Session - Raw raw // Raw contains the raw request and response + id string + config *Config + status int + statusWritten bool + bypassLifecycle bool + w http.ResponseWriter + req *http.Request + pathParams map[string]string + bodyBytes []byte + charset string + session session.Session + Raw raw // Raw contains the raw request and response } func newCallFromRequest(w http.ResponseWriter, req *http.Request, config *Config, pathParams map[string]string) Call { @@ -60,13 +61,14 @@ func newCallFromRequest(w http.ResponseWriter, req *http.Request, config *Config } call := Call{ - id: uniqueID, - config: config, - w: w, - req: req, - status: 0, - pathParams: pathParams, - charset: charsets.UTF8, + id: uniqueID, + config: config, + w: w, + req: req, + status: 0, + bypassLifecycle: false, + pathParams: pathParams, + charset: charsets.UTF8, Raw: raw{ W: &w, Req: req, diff --git a/server.go b/server.go index 30b9894..c17ec38 100644 --- a/server.go +++ b/server.go @@ -332,6 +332,9 @@ func (server *App) getPathHandlerByPath(path string) (*pathHandler, error) { func (server *App) matchBeforeHandlers(call *Call) bool { for _, pathHandler := range server.pathHandlers { + if call.bypassLifecycle { + return false + } if pathHandler.Before != nil && pathHandler.PathMatcher.MatchesURL(call.URL().Path) { call.pathParams = pathHandler.PathMatcher.PathParams(call.URL().Path) @@ -347,6 +350,10 @@ func (server *App) matchBeforeHandlers(call *Call) bool { func (server *App) matchHandlers(call *Call) { for _, pathHandler := range server.pathHandlers { + if call.bypassLifecycle { + return + } + if pathHandler.GetHandlerByMethod(call.Method()) != nil && pathHandler.PathMatcher.MatchesURL(call.URL().Path) { handler := pathHandler.GetHandlerByMethod(call.Method()) call.pathParams = pathHandler.PathMatcher.PathParams(call.URL().Path) @@ -358,6 +365,9 @@ func (server *App) matchHandlers(call *Call) { func (server *App) matchAfterHandlers(call *Call) { for _, pathHandler := range server.pathHandlers { + if call.bypassLifecycle { + return + } if pathHandler.After != nil && pathHandler.PathMatcher.MatchesURL(call.URL().Path) { call.pathParams = pathHandler.PathMatcher.PathParams(call.URL().Path) pathHandler.After(call) @@ -377,17 +387,26 @@ func (server *App) rootHandlerFunc(w http.ResponseWriter, req *http.Request) { ) // Look for before handlers - if !server.matchBeforeHandlers(&call) { + if !server.matchBeforeHandlers(&call) && !call.bypassLifecycle { // Before handler returned false, meaning short circuit, meaning we need to log access log here server.logAccessLog(&call, float64(time.Since(incomingRequestTime))/float64(time.Millisecond)) return } + if call.bypassLifecycle { + return + } // Look for endpoint handler server.matchHandlers(&call) + if call.bypassLifecycle { + return + } // Look for After handlers server.matchAfterHandlers(&call) + if call.bypassLifecycle { + return + } // No status set, meaning no handlers have handled the request properly, // ie 404 / not found