how to custom data in route

30 views
Skip to first unread message

gap...@gmail.com

unread,
Jan 25, 2019, 12:02:22 AM1/25/19
to Gorilla web toolkit
I want to use the value of key isAuthority in the adapter to make the authorization judgment . how can i do.
for example:

var routeList = routes{
   {
      pattern:     "/article",
      method:      GET,
      HandlerFunc: ctrls.GetAll,
      isAuthority: false,
   },
   {
      pattern: "/article/:id",
      method:  GET,
      isAuthority: false,
   },


router := mux.NewRouter().StrictSlash(true)

for _, item := range routeList {
   router.Path(item.pattern).
      Methods(item.method).
      Handler(item.HandlerFunc).
      ???(item.isAuthority)
}

func adapter(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
             //how to get the value of currentRoute'isAuthority
})
}

Matt S

unread,
Jan 25, 2019, 8:48:08 AM1/25/19
to goril...@googlegroups.com
How do you determine “isAuthority” to begin with? Can you explain what the request flow looks like?

You can’t arbitrarily extend a struct, but authenticating admin routes can be done by breaking them out into a subrouter & using middleware to validate a cookie/token.

--
You received this message because you are subscribed to the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gorilla-web...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

gap...@gmail.com

unread,
Jan 26, 2019, 12:57:01 AM1/26/19
to Gorilla web toolkit
I want make a rest api server . My idea is to determine if the interface requires authorization verification when defining the routing structure. Use a keyword like "isAuthority" to identify in the routing structure. So in the middleware, I only need to get the value of keyword like "isAuthority" to know if I need to authenticate. now  I use the filed Name to save the value used to judge . 
Reply all
Reply to author
Forward
0 new messages