route name
route
name
To be available to know the name of the route within the handler, the .Name
method can be used, example:
router := violetear.New()
router.Handle("*", catchAll, "ALL").Name("*")
router.HandleFunc("/post", methodPOST, "POST").Name("post")
router.HandleFunc("/get", methodALL, "GET").Name("get")
router.HandleFunc("/:any", methodRX).Name("rx")
To retrieve the name within the handler this could be used:
func theHandler(w http.ResponseWriter, r *http.Request) {
endpoint := violetear.GetRouteName(r)
fmt.Fprintf(w, "my name is: %s!", endpoint)
}
This is useful when instrumenting your code with tools like prometheus.
GetError()
Handle
and HandleFunc
return a
*Trie this is needed
to be available to set a Name
per route, therefore to know if an error occurred
when creating the route the method GetError()
can be used, for example:
router.Handle("*", catchAll, "ALL").Name("*")
if err := router.GetError(); err != nil {
log.Fatal(err)
}