Go Web:HttpRouter路由(一)

HttpRouter是一個輕量級但卻很是高效的multiplexer。git

手冊:github

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

安裝httprouter


go get github.com/julienschmidt/httprouter

用法示例


代碼以下:web

main.go 文件正則表達式

package main

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

func RegisterHandlers() *httprouter.Router {
    //New()方法建立了實例,啓動web服務
    router := httprouter.New()

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

    //註冊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)
}

這種模式"/hello/:name",它能夠用來作命名匹配,相似於正則表達式的命名捕獲分組。瀏覽器

Handlers.go 文件函數

定義httprouter中的handler,處理對應請求測試

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)
}

測試

瀏覽器上輸入路由便可code

如:router

http://localhost:8080/hello/xxxx

瀏覽器上輸出:hello, xxxx!接口

httprouter用法說明

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

httprouter中的Handle相似於http.HandlerFunc,只不過它支持第三個參數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).

例如前面示例中的Index()和Hello()都是Handle類型的實例。

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"))
}

註冊handler httprouter.Router類型相似於http包中的ServeMux,它實現了http.Handler接口,因此它是一個http.Handler。它能夠將請求分配給註冊好的handler。

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

除此以外,Router提供了很多方法,用來指示如何爲路徑註冊handler。

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()用於爲路徑註冊指定的Handle,而httprouter.Handle對應於http.HandlerFunc,因此是直接將Handle類型的函數綁定到指定路徑上。同時,它還能夠指定http方法:GET, POST, HEAD, PUT, PATCH, DELETE, OPTIONS。

相關文章
相關標籤/搜索