cedar-router基于前缀树算法的 golang router

联合创作 · 2023-10-01

cedar,轻量级 Golang 路由器,基于前缀树算法。

cedar.NewRouter().Get(prefix,http.HandlerFunc,http.Handler)

推荐组件

session

更新

现在支持了模糊路由匹配

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 ,静态文件路由需要改写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 获得访问.

软件稳定运行在服务器

浏览 4
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

编辑
举报