Router and Endpoints
Router and Endpoints
The router component provides us a way to define routes that are mapped to handlers that process the request. Crema uses Gorilla Mux-based HTTP router and it works almost the same way.
Crema uses method server.AddRoutes() to register new endpoints. The method requires three parameters:
- HTTP Method
- URL
- Handler Function
Let's start writing a sample endpoint:
server.AddRoutes(http.MethodGet, "/hello", helloHandler)
From the example above, we register a route mapping an URL path “/hello” to a handler “helloHandler” which is requested using HTTP GET.
Like Gorilla Mux, paths can have variables too. The variables are defined using the format {var}. For example:
server.AddRoutes(http.MethodGet, “/users/{id}”, userHandler) server.AddRoutes(http.MethodGet, “/users/{name}”, userHandler)
That's it! See here to see how Crema handles requests
Tags
None
Subscribers
None
- Last Author
- • galang
- Last Edited
- Oct 15 2019, 3:30 PM