The router is capable of handle any kind or URI, static, dynamic or catchall and based on the HTTP request Method accept or discard the request.
For example, suppose we have an API that exposes a service that allows to ping any IP address.
To handle only
GET requests for any IPv4 addresss:
http://api.violetear.org/command/ping/127.0.0.1 \______/\___/\________/ | | | static | dynamic
HandlerFunc would be:
router.HandleFunc("/command/ping/:ip", ipHandler, "GET")
For this to work, first the regex matching
:ip should be added:
Now let’s say you also want to be available to ping ipv6 or any host:
http://api.violetear.org/command/ping/* \______/\___/\_/ | | | static | catch-all
A catch-all could be used and also a different handler, for example:
router.HandleFunc("/command/ping/*", anyHandler, "GET, HEAD")
* indicates the router to behave like a catch-all therefore it
will match anything after the
/command/ping/ if no other condition matches
Notice also the
HEAD, that indicates that only does HTTP methods will
be accepted, and any other will not be allowed, router will return a:
405 Method Not Allowed
To customise this reponse, the
can be used, it is a configurable http.Handler
which is called when method not allowed.