cedar-router基于前綴樹算法的 golang router
cedar,輕量級 Golang 路由器,基于前綴樹算法。
cedar.NewRouter().Get(prefix,http.HandlerFunc,http.Handler)
推薦組件
更新
現(xiàn)在支持了模糊路由匹配
r.Get("/index/:id",func(w http.ResponseWriter, r *http.Request){
fmt.Println(r.URL.Fragment) // <- use this ,get the `id`
},nil)
全局方法
r.GlobalFunc("test", func(r *http.Request) error {
fmt.Println("123213")
return nil
})
支持常見的method
r := cedar.NewRouter()
r.Get("/",http.HandlerFunc(),nil)
r.Post("/",http.HandlerFunc(),nil)
r.Put("/",http.HandlerFunc(),nil)
r.Delete("/",http.HandlerFunc(),nil)
handFunc和Handler在同一路徑下我建議只填寫一個 . 測試通過websocket ,靜態(tài)文件路由需要改寫ServerHTTP方法.我默認在當前路徑下 static/
同一路由下 不同Method ,會覆蓋前面的的HandlerFunc和Handler
新版本修復了該問題
群組路由
r := cedar.NewRouter()
r.Group("/a",func (group *cedar.Groups){
group.Get("/b",http.HandlerFunc(),nil)
group.Group("/c",func(groups *cedar.Groups) {
group.Get("/d",http.HandlerFunc(),nil)
})
})
同時也支持RestFul風格
r := cedar.NewRestRouter(cedar.RestConfig{
EntryPath: "blog",
ApiName: "api",
Pattern: ".",
})
r.Get("user", func(writer http.ResponseWriter, request *http.Request) {
r.Template(writer, "/index")
}, nil)
//localhost/blog?api=user
通過 localhost/blog?api=user 獲得訪問.
軟件穩(wěn)定運行在服務器
評論
圖片
表情
