Go Web: HttpRouter Routing

Keywords: Programming github

HttpRouter is a lightweight but highly efficient multiplexer.

Manual:

https://godoc.org/github.com/julienschmidt/httprouter
https://github.com/julienschmidt/httprouter

Install httprouter

go get github.com/julienschmidt/httprouter

Usage example

The code is as follows:

main.go file



package main

import (
    "github.com/julienschmidt/httprouter"
    "net/http"
   //"log"
)

func RegisterHandlers() *httprouter.Router {
    //The New() method creates an instance to start a web service
    router := httprouter.New()

    //Register handler
    router.GET("/", Index)
    router.GET("/hello/:name", Hello)

    //Register handler
    router.POST("/user", CreateUser)
    router.POST("/user/:user_name", Login)

    return router
}


func main() {
    registerHandler := RegisterHandlers()
    //log.Fatal(http.ListenAndServe(":8080", router))
    http.ListenAndServe(":8080", router)
}

This pattern "/ hello/:name" can be used for naming matches, similar to the naming capture grouping of regular expressions.

Handlers.go file

Define handler in httprouter to process corresponding requests

package main

import (
    "github.com/julienschmidt/httprouter"
    "net/http"
    "io"
)

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}


func CreateUser(w http.ResponseWriter, r *http.Request, p httprouter.Params)  {
    io.WriteString(w, "create User Handler")
}

func Login(w http.ResponseWriter, r *http.Request, p httprouter.Params)  {
    userName := p.ByName("user_name")

    io.WriteString(w, userName)
}

test

Enter routing on Browser

Such as:

http://localhost:8080/hello/xxxx

Output on browser: hello, xxx!

Description of httprouter usage


Variables
func CleanPath(p string) string
type Handle
type Param
type Params
    func ParamsFromContext(ctx context.Context) Params
    func (ps Params) ByName(name string) string
type Router
    func New() *Router
    func (r *Router) DELETE(path string, handle Handle)
    func (r *Router) GET(path string, handle Handle)
    func (r *Router) HEAD(path string, handle Handle)
    func (r *Router) Handle(method, path string, handle Handle)
    func (r *Router) Handler(method, path string, handler http.Handler)
    func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)
    func (r *Router) Lookup(method, path string) (Handle, Params, bool)
    func (r *Router) OPTIONS(path string, handle Handle)
    func (r *Router) PATCH(path string, handle Handle)
    func (r *Router) POST(path string, handle Handle)
    func (r *Router) PUT(path string, handle Handle)
    func (r *Router) ServeFiles(path string, root http.FileSystem)
    func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)
type Handle

Handle in httprouter is similar to http.HandlerFunc, except that it supports the third parameter, Params.

type Handle func(http.ResponseWriter, *http.Request, Params)
    Handle is a function that can be registered to a route to handle HTTP
    requests. Like http.HandlerFunc, but has a third parameter for the values of
    wildcards (variables).

For example, both Index() and Hello() in the previous example are instances of Handle type.

func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    fmt.Fprint(w, "Welcome!\n")
}

func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
    fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}

Register handler The httprouter.Router type is similar to ServeMux in the HTTP package. It implements the http.Handler interface, so it is an http.Handler. It can assign requests to registered handlers.

type Router struct {}
func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

In addition, Router provides a number of ways to indicate how to register handler s for paths.

func (r *Router) Handle(method, path string, handle Handle)
func (r *Router) Handler(method, path string, handler http.Handler)
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc)

httprouter.Handle() is used to register the specified handle for the path, and httprouter.Handle corresponds to http.HandlerFunc, so it binds the function of Handle type directly to the specified path. It can also specify http methods: GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS.

Posted by darkjedig on Thu, 18 Apr 2019 16:39:32 -0700