Page MenuHomeHexavara Tech

Router and Endpoints
Updated 1,864 Days AgoPublic

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

Last Author
galang
Last Edited
Oct 15 2019, 3:30 PM

Event Timeline

galang created this object.Oct 15 2019, 2:48 PM
galang changed the title from Routing to Routes and Endpoints.Oct 15 2019, 2:51 PM
galang edited the content of this document. (Show Details)
galang changed the title from Routes and Endpoints to Router and Endpoints.Oct 15 2019, 3:18 PM
galang edited the content of this document. (Show Details)
galang edited the content of this document. (Show Details)Oct 15 2019, 3:30 PM