Skip to content

Commit

Permalink
feat(call-buypass): add option to bypass call lifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
pkkummermo committed Sep 28, 2024
1 parent 50f4288 commit d8975c1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 19 deletions.
38 changes: 20 additions & 18 deletions call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
21 changes: 20 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit d8975c1

Please sign in to comment.