Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do something everytime, no matter what? #7

Open
undeadindustries opened this issue Mar 31, 2020 · 1 comment
Open

do something everytime, no matter what? #7

undeadindustries opened this issue Mar 31, 2020 · 1 comment

Comments

@undeadindustries
Copy link

undeadindustries commented Mar 31, 2020

This might sound like a dumb question.

With Denco, is it possible to run a function everytime it hit any route?

So if I had something like:

mux := denco.NewMux()
handler, err := mux.Build([]denco.Handler{
mux.GET("/", handleRoot),
mux.GET("/foo", handleFoo),
mux.POST("/bar", handleBar),
})
if err != nil {
panic(err)
}
GetSession()
log.Fatal(http.ListenAndServeTLS(":443","crt", "key", handler))

How would I run GetSession() every time, no matter what route gets triggered?

Thanks!

@gearintellix
Copy link

I think the simple workaround is just wrap the handler with a middleware like function

eg.

func Middleware(handler func(), middlewares ...func()) func() {
   return func() {
      // TODO: invoke middlewares before handler...
   }
}

// usage
handler, err := mux.Build([]denco.Handler{
   mux.GET("/", Middleware(handleRoot, GetSession)),
   mux.GET("/foo", Middleware(handleFoo, GetSession)),
   mux.POST("/bar", Middleware(handleBar, GetSession)),
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants