1 Star 0 Fork 6

xyhp915 / router

forked from zakzou / router 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

router

router是一个go语言的路由分发包

特性

  • 支持自定义正则匹配路由
  • 支持restful
  • 支持Middleware
  • 支持Hook

安装

go get -u github.com/zakzou/router

快速开始

1. 实例化路由

import (
    "github.com/zakzou/router"
)

r := router.NewRouter()

1.1 默认不会严格匹配斜杠,如果需要匹配,则需要设置

r.StrictSlash(true)

1.2 也可以使用连缀方式

r := route.NewRouter().StrictSlash(true)

2. 注册路由

home := r.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "home")
})

2.1 默认路只支持GET访问,如果需要支持POST,GET同时访问,可以通过Methods方法

home.Methods("POST", "GET")

2.2 也可以通过连缀方式,也可也支持单个路由不严格匹配斜杠

r.HandleFunc("/home", func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "home")
}).Methods("GET").StrictSlash(false)

2.3 注册命名路由

home.Name("home")

2.4 路由也支持模式匹配

  • 支持三种默认模式int, any, string
  • 也可以自定义则正<[pattern:]paramName>
r.HandleFunc("/<param1>/<string:params2>/<int:user_id>/", func(w http.ResponseWriter, r *http.Request) {
    query := r.URL.Query()
    param1 := query.Get("param1")
    param2 := query.Get("param2")
    param3 := query.Get("param3")
    fmt.Fprintln(w, param1, param2, param3)
})

3. 注册中间件(Middleware)

中间件会在路由被调用之前调用,一个路由可以注册多个中间件

home.MiddlewareFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln("Middleware 1")
})

3.1 也可以为所有路由注册中间件

r := router.NewRouter().MiddlewareFunc(func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln("Middleware 0")
})

4. 注册钩子

钩子会在执行路由的前后调用,目前支持2对钩子

  • HookBeforeRouter 每次请求前,无论路由是否存在,都会执行,在HookBeforeDispatch之前
  • HookAfterRouter 每次请求后,无论路由是否存在,都会执行,在HookAfterDispatch之后
  • HookBeforeDispatch 路由匹配成功后,路由分发之前执行
  • HookAfterDispatch 路由匹配成功后,路由分发之后执行
r.HookFunc(HookAfterDispatch, func(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "hook")
})

5. 生成地址

UrlFor 可以为命名路由自动生成地址

r.HandleFunc("/user/profile/query/<int:user_id>/", func(w http.ResponseWriter, r *http.Request) {
}).Name("profile")

if urls, ok := r.UrlFor("profile", map[string]interface{}{"user_id": 100001}); ok {
    println(urls)
}

6. 运行

if err := http.ListenAndServe(":9999", r); err != nil {
    println(err.Error())
}
The MIT License (MIT) Copyright (c) 2013 zakzou Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

golang 路由 展开 收起
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/Charlie-V/router.git
git@gitee.com:Charlie-V/router.git
Charlie-V
router
router
master

搜索帮助