Go语言HttpRouter路由使用方法详解
作者:骏马金龙 发布时间:2024-05-09 14:56:31
HttpRouter是一个轻量级但却非常高效的multiplexer。手册:
https://godoc.org/github.com/julienschmidt/httprouter
https://github.com/julienschmidt/httprouter
用法示例
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
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 main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
log.Fatal(http.ListenAndServe(":8080", router))
}
首先执行:
go get github.com/julienschmidt/httprouter
然后再启动web服务:
go run xxx.go
和http包的ServeMux用法其实很类似。上面定义了两个httprouter中的handle,类似于http包中的http.HandlerFunc类型,具体的对应关系后文会解释,只要认为它是handler,是处理对应请求的就行了。然后使用New()方法创建了实例,并使用GET()方法为两个模式注册了对应的handler。
需要注意的是,第二个模式"/hello/:name",它可以用来做命名匹配,类似于正则表达式的命名捕获分组。后面会详细解释用法。
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。
这些方法还有对应的各自缩写:
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) 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)
例如,Get()等价于route.Handle("GET", path, handle)。
例如上面的示例中,为两个路径注册了各自的httprouter.Handle函数。
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name", Hello)
Handler()方法是直接为指定http方法和路径注册http.Handler;HandlerFunc()方法则是直接为指定http方法和路径注册http.HandlerFunc。
Param相关
type Param struct {
Key string
Value string
}
Param is a single URL parameter, consisting of a key and a value.
type Params []Param
Params is a Param-slice, as returned by the router. The slice is ordered, the first URL parameter is also the first slice value. It is therefore safe to read values by the index.
func (ps Params) ByName(name string) string
ByName returns the value of the first Param which key matches the given name. If no matching Param is found, an empty string is returned.
Param类型是key/value型的结构,每个分组捕获到的值都会保存为此类型。正如前面的示例中:
router.GET("/hello/:name", Hello)
这里的:name
就是key,当请求的URL路径为/hello/abc
,则key对应的value为abc。也就是说保存了一个Param实例:
Param{
Key: "name",
Value: "abc",
}
更多的匹配用法稍后解释。
Params是Param的slice。也就是说,每个分组捕获到的key/value都存放在这个slice中。
ByName(str)方法可以根据Param的Key检索已经保存在slice中的Param的Value。正如示例中:
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
}
router.GET("/hello/:name", Hello)
这里ByName("name")
将检索保存在slice中,Key="name"的Param,且返回这个Param中的Value。
由于Params是slice结构,除了ByName()方法可以检索key/value,通过slice的方法也可以直接检索:
ps[0].Key
ps[0].Value
路径匹配规则
httprouter要为路径注册handler的适合,路径可以进行命名捕获。有两种命名捕获的方式:
Syntax Type
:name named parameter
*name catch-all parameter
其中:name
的捕获方式是匹配内容直到下一个斜线或者路径的结尾。例如要为如下路径注册handler:
Path: /blog/:category/:post
当请求路径为:
/blog/go/request-routers match: category="go", post="request-routers"
/blog/go/request-routers/ no match, but the router would redirect
/blog/go/ no match
/blog/go/request-routers/comments no match
*name
的捕获方式是从指定位置开始(包含前缀"/")匹配到结尾:
Path: /files/*filepath
/files/ match: filepath="/"
/files/LICENSE match: filepath="/LICENSE"
/files/templates/article.html match: filepath="/templates/article.html"
/files no match, but the router would redirect
再解释下什么时候会进行重定向。在Router类型中,第一个字段控制尾随斜线的重定向操作:
type Router struct {
RedirectTrailingSlash bool
...
}
如果请求的URL路径包含或者不包含尾随斜线时,但在注册的路径上包含了或没有包含"/"的目标上定义了handler,但是会进行301重定向。简单地说,不管URL是否带尾随斜线,只要注册路径不存在,但在去掉尾随斜线或加上尾随斜线的路径上定义了handler,就会自动重定向。
例如注册路径为/foo
,请求路径为/foo/
,会重定向。
下面还有几种会重定向的情况:
注册路径:/blog/:category/:post
请求URL路径:/blog/go/request-routers/
注册路径:/blog/:category
请求URL路径:/blog/go
注册路径:/files/*filepath
请求URL路径:/files
Lookup()
func (r *Router) Lookup(method, path string) (Handle, Params, bool)
Lookup根据method+path检索对应的Handle,以及Params,并可以通过第三个返回值判断是否会进行重定向。
来源:https://www.cnblogs.com/f-ck-need-u/p/10020917.html
猜你喜欢
- 使用说明:1. 将PHP.INI文件里面的"extension=php_gd2.dll"一行前面的;号去掉,因为我们要用
- 概述:本文主要分享一下博主在学习wxpy 的过程中开发的一个小程序。博主在最近有一个监控报警的需求需要完成,然后刚好在学习wxpy 这个东西
- 这段时间告诉项目需要,用到了vue。刚开始搭框架的时候用的是vue-resource后面看到官方推荐axios就换过来了 顺便封装了一下 /
- 在python中,普通的列表list和numpy中的数组array是不一样的,最大的不同是:一个列表中可以存放不同类型的数据,包括int、f
- 1、下面就来介绍一下如何简单的显示一个消息提示,好像js可以控制,不过这里用了django.contrib.messages这个库2、首先呢
- np.where共两种用法:第一种np.where(condition, x, y),即condition为条件,当满足条件输出为x,不满足
- 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的编写,刚开始不会体会出SQL语句各种写法的性能优劣,但是如果将应
- 1、前言拖拽排序对于小伙伴们来说应该不陌生,平时工作的时候,可能会选择使用类似Sortable.js这样的开源库来实现需求。但在完成需求后,
- 环境 django 1.10.6缘起今天接到一个任务——解决终端满屏日志中的无用错误。 django 会尽可能给你准确报出错误位置,但是一些
- 当然,5.6的GUID功能的出现也带来了multi-master的无限可能,不过这个已经是题外话了。 本文主要介绍一种非实时的适用于各版本M
- Python是一门面向对象的语言。面向对象都有三大特性:封装、继承、多态。下面分别来说说这三大特性:1、封装隐藏对象的属性和实现细节,仅对外
- 数据库中对数据的操作是一大重要技能,其中,数据的恢复和还原也是常做的事。不知你是否在数据库恢复时遇到诸如“设备激活错误,请使用with mo
- MySQL低调宣布它将不再把MySQL企业版服务器作为一个tar包发布,而这距离这个公司宣布将MySQL分为免费版和付费版的时间还不到一年。
- 本文实例为大家分享了Python读写Excel表格的具体代码,供大家参考,具体内容如下python读取Excel表格:import xlrd
- django1.3新加入了一个静态资源管理的app,django.contrib.staticfiles。在以往的django版本中,静态资
- 有时候,规划师(或需求、交互)把内容呈现的框架草图搭建好后,就直接“丢”给了设计师,让设计师在画好的框架里去美化内容,出来后的效果,往往达不
- 春节前在蓝色理想上发了个“雅虎口碑招聘前端工程师 ”的启事,节后收到很多简历,加之HR通过专业招聘网站得到的简历和朋友同事推荐的简历,数量上
- 本文实例为大家分享了python语音整点报时的具体代码,供大家参考,具体内容如下主要的技术特殊点在于PyS60的定时器最多只能定2147秒。
- 以下的文章主要介绍的是SQL Serve数据库到DB2连接服务器的实现过程,我们大家都知道不同数据库平台的互连,一般对其称之为数据库的异构服
- 由于实际需要,简要写了个小脚本,并打包生成exe,供无网络环境下使用脚本1:显示当前时间与时间戳,以及10分钟后的时间与时间戳# -*- c