Skip to content

Commit

Permalink
Add disable auth flag to route
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyphu committed Mar 15, 2024
1 parent 9542553 commit b7ce015
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pkg/service/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ type Route interface {
GetMethod() string
GetHandler() http.Handler
GetOverrideAuthMiddlewareFunc() AuthMiddlewareFunc
GetDisableAuth() bool
}

type WarrantRoute struct {
Pattern string
Method string
Handler http.Handler
OverrideAuthMiddlewareFunc AuthMiddlewareFunc
DisableAuth bool
}

func (route WarrantRoute) GetPattern() string {
Expand All @@ -45,3 +47,7 @@ func (route WarrantRoute) GetHandler() http.Handler {
func (route WarrantRoute) GetOverrideAuthMiddlewareFunc() AuthMiddlewareFunc {
return route.OverrideAuthMiddlewareFunc
}

func (route WarrantRoute) GetDisableAuth() bool {
return route.DisableAuth
}
17 changes: 10 additions & 7 deletions pkg/service/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,16 @@ func NewRouter(config config.Config, pathPrefix string, routes []Route, authMidd
middlewareWrappedHandler := ChainMiddleware(route.GetHandler(), requestMiddlewares...)

var err error
if route.GetOverrideAuthMiddlewareFunc() != nil {
middlewareWrappedHandler, err = route.GetOverrideAuthMiddlewareFunc()(config, middlewareWrappedHandler)
} else {
middlewareWrappedHandler, err = authMiddleware(config, middlewareWrappedHandler)
}
if err != nil {
return nil, err
if !route.GetDisableAuth() {
if route.GetOverrideAuthMiddlewareFunc() != nil {
middlewareWrappedHandler, err = route.GetOverrideAuthMiddlewareFunc()(config, middlewareWrappedHandler)
} else {
middlewareWrappedHandler, err = authMiddleware(config, middlewareWrappedHandler)
}

if err != nil {
return nil, err
}
}

router.Handle(routePattern, middlewareWrappedHandler).Methods(route.GetMethod())
Expand Down

0 comments on commit b7ce015

Please sign in to comment.