Skip to content

Commit

Permalink
feat: after middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
viquitorreis committed Apr 30, 2024
1 parent be035cd commit d766703
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
18 changes: 18 additions & 0 deletions tupa.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@ func (a *APIServer) MakeHTTPHandlerFuncHelper(routeInfo RouteInfo) http.HandlerF
} else {
WriteJSONHelper(w, http.StatusMethodNotAllowed, APIError{Error: "Método HTTP não permitido"})
}

allAfterMiddlewares := MiddlewareChain{}
allAfterMiddlewares = append(allAfterMiddlewares, routeInfo.AfterMiddlewares...)

doneCh = a.executeMiddlewaresAsync(ctx, allAfterMiddlewares)
errorsSlice = <-doneCh

if len(errorsSlice) > 0 {
err := errorsSlice[0]
if apiErr, ok := err.(APIHandlerErr); ok {
slog.Error("API Error", "err:", apiErr, "status:", apiErr.Status)
WriteJSONHelper(w, apiErr.Status, APIError{Error: apiErr.Error()})
} else {
slog.Error("API Error", "err:", apiErr, "status:", apiErr.Status)
WriteJSONHelper(w, http.StatusInternalServerError, APIError{Error: err.Error()})
}
return
}
}
}

Expand Down
9 changes: 5 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ type HandleFunc func(*TupaContext) error
type HTTPMethod string

type RouteInfo struct {
Path string
Method HTTPMethod
Handler APIFunc
Middlewares []MiddlewareFunc
Path string
Method HTTPMethod
Handler APIFunc
Middlewares []MiddlewareFunc
AfterMiddlewares []MiddlewareFunc
}

0 comments on commit d766703

Please sign in to comment.