Golang中HttpRouter路由的使用詳解
httprouter
httprouter 是一個(gè)高性能、可擴(kuò)展的HTTP路由,上面我們列舉的net/http默認(rèn)路由的不足,都被httprouter 實(shí)現(xiàn),我們先用一個(gè)例子,認(rèn)識(shí)下 httprouter 這個(gè)強(qiáng)大的 HTTP 路由。
安裝:
go get -u github.com/julienschmidt/httprouter
在這個(gè)例子中,首先通過httprouter.New()生成了一個(gè)*Router路由指針,然后使用GET方法注冊(cè)一個(gè)適配/路徑的Index函數(shù),最后*Router作為參數(shù)傳給ListenAndServe函數(shù)啟動(dòng)HTTP服務(wù)即可。
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Write([]byte("Index"))
}
func main() {
router := httprouter.New()
router.GET("/", Index)
log.Fatal(http.ListenAndServe(":8080", router))
}
httprouter 為所有的HTTP Method 提供了快捷的使用方式,只需要調(diào)用對(duì)應(yīng)的方法即可。
func (r *Router) GET(path string, handle Handle) {
r.Handle("GET", path, handle)
}
func (r *Router) HEAD(path string, handle Handle) {
r.Handle("HEAD", path, handle)
}
func (r *Router) OPTIONS(path string, handle Handle) {
r.Handle("OPTIONS", path, handle)
}
func (r *Router) POST(path string, handle Handle) {
r.Handle("POST", path, handle)
}
func (r *Router) PUT(path string, handle Handle) {
r.Handle("PUT", path, handle)
}
func (r *Router) PATCH(path string, handle Handle) {
r.Handle("PATCH", path, handle)
}
func (r *Router) DELETE(path string, handle Handle) {
r.Handle("DELETE", path, handle)
}現(xiàn)代的API,基本上都是Restful API,httprouter提供的命名參數(shù)的支持,可以很方便的幫助我們開發(fā)Restful API。比如我們?cè)O(shè)計(jì)的API/user/flysnow,這這樣一個(gè)URL,可以查看flysnow這個(gè)用戶的信息,如果要查看其他用戶的,比如zhangsan,我們只需要訪問API/user/zhangsan即可。
URL包括兩種匹配模式:/user/:name精確匹配、/user/*name匹配所有的模式。
package main
import (
"github.com/julienschmidt/httprouter"
"net/http"
"log"
"fmt"
)
func main() {
router:=httprouter.New()
router.GET("/MainData", func (w http.ResponseWriter,r *http.Request,_ httprouter.Params) {
w.Write([]byte("default get"))
})
router.POST("/MainData",func (w http.ResponseWriter,r *http.Request,_ httprouter.Params) {
w.Write([]byte("default post"))
})
//精確匹配
router.GET("/user/name",func (w http.ResponseWriter,r *http.Request,p httprouter.Params) {
w.Write([]byte("user name:"+p.ByName("name")))
})
//匹配所有
router.GET("/employee/*name",func (w http.ResponseWriter,r *http.Request,p httprouter.Params) {
w.Write([]byte("employee name:"+p.ByName("name")))
})
http.ListenAndServe(":8081", router)
}
Handler處理鏈處理不同二級(jí)域名
package main
import (
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
type HostMap map[string]http.Handler
func (hs HostMap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
fmt.Println("222")
//根據(jù)域名獲取對(duì)應(yīng)的Handler路由,然后調(diào)用處理(分發(fā)機(jī)制)
if handler := hs[r.Host]; handler != nil {
handler.ServeHTTP(w, r)
} else {
http.Error(w, "Forbidden", 403)
}
}
func main() {
userRouter := httprouter.New()
userRouter.GET("/", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
w.Write([]byte("play"))
})
dataRouter := httprouter.New()
dataRouter.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
w.Write([]byte("tool"))
})
//分別用于處理不同的二級(jí)域名
hs := make(HostMap)
hs["user.localhost:12345"] = userRouter
hs["data.localhost:12345"] = dataRouter
log.Fatal(http.ListenAndServe(":12345", hs))
}
httprouter提供了很方便的靜態(tài)文件服務(wù),可以把一個(gè)目錄托管在服務(wù)器上,以供訪問。
router.ServeFiles("/static/*filepath",http.Dir("./"))使用ServeFiles需要注意的是,第一個(gè)參數(shù)路徑,必須要以/*filepath,因?yàn)橐@取我們要訪問的路徑信息。
func (r *Router) ServeFiles(path string, root http.FileSystem) {
if len(path) < 10 || path[len(path)-10:] != "/*filepath" {
panic("path must end with /*filepath in path '" + path + "'")
}
fileServer := http.FileServer(root)
r.GET(path, func(w http.ResponseWriter, req *http.Request, ps Params) {
req.URL.Path = ps.ByName("filepath")
fileServer.ServeHTTP(w, req)
})
}例子:
package main
import (
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func main() {
router := httprouter.New()
//訪問靜態(tài)文件
router.ServeFiles("/static/*filepath", http.Dir("./files"))
log.Fatal(http.ListenAndServe(":8080", router))
}httprouter 異常捕獲,httprouter允許使用者,設(shè)置PanicHandler用于處理HTTP請(qǐng)求中發(fā)生的panic。
package main
import (
"fmt"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
panic("error")
}
func main() {
router := httprouter.New()
router.GET("/", Index)
//捕獲異常
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, v interface{}) {
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "error:%s", v)
}
log.Fatal(http.ListenAndServe(":8080", router))
}httprouter還有不少有用的小功能,比如對(duì)404進(jìn)行處理,我們通過設(shè)置Router.NotFound來實(shí)現(xiàn),我們看看Router這個(gè)結(jié)構(gòu)體的配置,可以發(fā)現(xiàn)更多有用的功能。
type Router struct {
//是否通過重定向,給路徑自定加斜杠
RedirectTrailingSlash bool
//是否通過重定向,自動(dòng)修復(fù)路徑,比如雙斜杠等自動(dòng)修復(fù)為單斜杠
RedirectFixedPath bool
//是否檢測(cè)當(dāng)前請(qǐng)求的方法被允許
HandleMethodNotAllowed bool
//是否自定答復(fù)OPTION請(qǐng)求
HandleOPTIONS bool
//404默認(rèn)處理
NotFound http.Handler
//不被允許的方法默認(rèn)處理
MethodNotAllowed http.Handler
//異常統(tǒng)一處理
PanicHandler func(http.ResponseWriter, *http.Request, interface{})
}到此這篇關(guān)于Golang中HttpRouter路由的使用詳解的文章就介紹到這了,更多相關(guān)Golang HttpRouter路由內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Golang中實(shí)現(xiàn)簡(jiǎn)單的Http Middleware
本文主要針對(duì)Golang的內(nèi)置庫(kù) net/http 做了簡(jiǎn)單的擴(kuò)展,實(shí)現(xiàn)簡(jiǎn)單的Http Middleware,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
使用golang引入外部包的三種方式:go get, go module, ve
這篇文章主要介紹了使用golang引入外部包的三種方式:go get, go module, vendor目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索
這篇文章主要為大家介紹了Go并發(fā)編程中的錯(cuò)誤恢復(fù)機(jī)制與代碼持續(xù)執(zhí)行實(shí)例探索,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
golang協(xié)程關(guān)閉踩坑實(shí)戰(zhàn)記錄
協(xié)程(coroutine)是Go語(yǔ)言中的輕量級(jí)線程實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于golang協(xié)程關(guān)閉踩坑的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
Go語(yǔ)言操作redis數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Go語(yǔ)言操作redis數(shù)據(jù)庫(kù)的方法,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-07-07

