On Wed, Apr 3, 2019 at 8:35 AM <afriyie.abra...@gmail.com> wrote:

> type Route struct {
>     Name        string
>     Method      string
>     Pattern     string
>     HandlerFunc http.HandlerFunc
> }
>
> type Routes []Route
>
> func NewRouter() *mux.Router {
>     router := mux.NewRouter().StrictSlash(true)
>     for _, route := range routes {
>         var handler http.Handler
>         handler = route.HandlerFunc
>         handler = Logger(handler, route.Name)
>
>         router.
>             Methods(route.Method).
>             Path(route.Pattern).
>             Name(route.Name).
>             Handler(handler)
>     }
>
>     return router
> }
>
> func Index(w http.ResponseWriter, r *http.Request) {
>     fmt.Fprintf(w, "Hello World!")
> }
>
> var routes = Routes{
>     {
>         "Index",
>         "GET",
>         "/",
>         Index,
>     },
>
>     {
>         "protecteduri",
>         strings.ToUpper("Get"),
>         "/protected",
>         protecteduri,
>     },
> }
>
> My question is how do i apply the "validateToken" function (middleware) to 
> the routes in the router.go?
> The function is to validate the access token in the request message before 
> calling the handler functions.
>
Have you looked at the gorilla/mux documentation about middlewares?
There are examples there: https://godoc.org/github.com/gorilla/mux

You need to change the validateToken func:

func getTokenMW(srv *server.Server) func(http.Handler) http.Handler {
   return func(next http.Handler) http.Handler {
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
      }
   }
}

Then:
router.Use(getTokenMW(server))

Or, you can define a struct, put the server pointer in it, and use a
member function of that struct as the middleware.

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

Reply via email to