01 Introduction HTTP Router
Introduction
HttpRouter is a popular open source library for HTTP Handler in Golang. This HttpRouter is famous for its speed and is very minimalist or simple because it only has features for routing, it doesn’t have any features other than that. If you want to see more details, you can visit the github here https://github.com/julienschmidt/httprouter
HTTP Method
The router in the library here is the same as ServeMux where we can add routes to the Router. The advantage compared to ServeMux is that in this library we can determine the HTTP methods we want to use, for example GET, POST, PUT, DELETE and so on. The way to add the router to the router is by calling the function which we will use the HTTP method for example router.GET().
Httprouter.Handle
When we use ServeMux, of course we add a route to be able to add http.Handler, so this library is different, namely on the router we don’t use http.Handler anymore, but instead we use the httprouter.Handle type. The difference is that there is a third parameter, namely Params, which we can use for our own needs.
Routers
The HttpRouter library contains the Router struct which is an implementation of http.Handler so we can easily add it to http.Server to create a Router using the httprouter.New() function which will return a Router pointers.
The following is an example of router initialization.
1main package
2
3import (
4"net/http"
5
6"github.com/julienschmidt/httprouter"
7)
8
9func main() {
10router := httprouter.New()
11
12server := http.Server{
13Handlers: routers,
14Addr: "localhost:8080",
15}
16
17server.ListenAndServe()
18}Create a New Project
OK, now we will try to use this HttpRouter with a new project first
Create a new folder or new project with mkdir learn-golang-httprouter. Initialize the Golang module with the command
1go mod init github.com/santekno/learn-golang-httprouterAfter that, a file called go.mod will automatically be created. Or if you want it to be easier, you can just clone or download the Santekno repository here https://github.com/santekno/learn-golang-httprouter
.
Next, we add the Golang library that we discussed above, namely with this command
1go get github.com/julienschmidt/httprouterIf it has been executed, the go.mod file will add its library to the file like this and a go.sum file will automatically be created.
1module github.com/santekno/learn-golang-httprouter
2
3go 1.21.1
4
5require github.com/julienschmidt/httprouter v1.3.0 // indirectDon’t forget that we also need an additional library to create unit tests, namely by using this library.
1go get github.com/stretchr/testifyAs a result, the final contents of the go.mod file will look like this.
1module github.com/santekno/learn-golang-httprouter
2
3go 1.21.1
4
5require (
6github.com/davecgh/go-spew v1.1.1 // indirect
7github.com/julienschmidt/httprouter v1.3.0 // indirect
8github.com/pmezard/go-difflib v1.0.0 // indirect
9github.com/stretchr/objx v0.5.0 // indirect
10github.com/stretchr/testify v1.8.4 // indirect
11gopkg.in/yaml.v3 v3.0.1 // indirect
12)We continue to create the main.go file with the initialization contents of httprouter like this
1main package
2
3import (
4"fmt"
5"net/http"
6
7"github.com/julienschmidt/httprouter"
8)
9
10func main() {
11router := httprouter.New()
12
13router.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
14fmt.Fprint(w, "Hello Get")
15})
16
17router.POST("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
18fmt.Fprint(w, "Hello Post")
19})
20
21server := http.Server{
22Handlers: routers,
23Addr: "localhost:8080",
24}
25
26server.ListenAndServe()
27}If it’s like this then we can try running this project with the command below
1go build && ./learn-golang-httprouterSo if we access it using curl the GET method will look like this
1➜ santekno-hugo git:(main) ✗ curl http://localhost:8080/
2Hello Get%And for endpoints with a POST method like this.
1➜ santekno-hugo git:(main) ✗ curl --location --request POST 'http://localhost:8080'
2Hello Post%