Go Gorilla Mux "match anything" path template
Asked Answered
H

2

12

What is the proper syntax to create a simple "match anything" handler?

mux.NewRouter().StrictSlash(true).Path("/")....

The above code seems to strictly match / and /foo won't get matched

Hamer answered 14/4, 2017 at 18:31 Comment(0)
B
11

This should work:

route := mux.NewRouter().PathPrefix("/")
Bowdlerize answered 14/4, 2017 at 18:43 Comment(2)
as per the docs, StrictSlash() has no effect when used with PathPrefix()Bowdlerize
PathPrefix returns a Route, not a Router.Joceline
D
11

You can use mux.Route.HandlerFunc together with mux.Router.PathPrefix:

r := mux.NewRouter()

// route catalog to catalogHandler:
r.HandleFunc("/catalog/{id}", catalogHandler) 

// route everything else to defaultHandler:
r.PathPrefix("/").HandlerFunc(defaultHandler)

Note the difference in names (HandlerFunc vs HandleFunc).

Decent answered 30/11, 2018 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.