http.HandleFunc("/protected", validateToken(func(w http.ResponseWriter, r *http.Request) {w.Write([]byte("Hello, I'm protected" outside the main function as
func protecteduri(w http.ResponseWriter, r *http.Request) { w.Write([]byte("Hello, I'm protected")) }
and apply the middleware function "validateToken" to all my gorilla mux routes so that the token used to request can be validated. The routes
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, },
{ "AccessTokenRequest", strings.ToUpper("Post"), "/oauth2/token", AccessTokenRequest, }, }
Can anyone help?
|
|