Phriction Knowledge Center - Hexavara Technology Crema Framework Router and Endpoints History Version 1 vs 4
Version 1 vs 4
Version 1 vs 4
Content Changes
Content Changes
routing
The router component provides us a way to define routes that are mapped to handlers that process the request. Crema uses [[ https://github.com/gorilla/mux | 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 [[ http://phabricator.hexavara.com/w/crema/handler/ | here ]] to see how Crema handles requests
routingThe router component provides us a way to define routes that are mapped to handlers that process the request. Crema uses [[ https://github.com/gorilla/mux | 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 [[ http://phabricator.hexavara.com/w/crema/handler/ | here ]] to see how Crema handles requests