mux https://github.com/gorilla/mux

gorilla / mux实现了一个请求路由器和分发器,用于将传入的请求与其各自的处理程序进行匹配。
名称mux代表“ HTTP请求多路复用器”。 与标准的http.ServeMux一样,mux.Router将传入请求与已注册路由列表进行匹配,并为与URL或其他条件匹配的路由调用处理程序。 主要特点是:

  • 它实现了http.Handler接口,因此与标准的http.ServeMux兼容。
  • 可以基于URL主机,路径,路径前缀,方案,标头和查询值,HTTP方法或使用自定义匹配器来匹配请求。
  • URL的host,路径和查询值可以具有带有可选正则表达式的变量。
  • 可以构建或“反转”已注册的URL,这有助于维护对资源的引用。
  • 路由可用作子路由:仅在父路由匹配时才测试嵌套路由。 这对于定义具有共同条件(例如主机,路径前缀或其他重复属性)的路由组很有用。 另外,这可以优化请求匹配。

Install:

go get -u github.com/gorilla/mux

Examples:

让我们开始注册几个URL路径和处理程序:

func main() {r := mux.NewRouter()r.HandleFunc("/", HomeHandler)r.HandleFunc("/products", ProductsHandler)r.HandleFunc("/articles", ArticlesHandler)http.Handle("/", r)
}

在这里,我们注册了三个将URL路径映射到处理程序的路由。 这等效于http.HandleFunc()的工作方式:如果传入的请求URL与路径之一匹配,则将相应的处理程序称为传递(http.ResponseWriter,* http.Request)作为参数。
路径可以具有变量。 它们使用{name}或{name:pattern}格式定义。 如果未定义正则表达式模式,则匹配的变量将是下一个斜杠之前的任何内容。 例如:

r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)

这些名称用于创建路由变量的映射,可以调用mux.Vars()来检索它们:

func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {vars := mux.Vars(r)w.WriteHeader(http.StatusOK)fmt.Fprintf(w, "Category: %v\n", vars["category"])
}

这就是您需要了解的基本用法。 下面介绍了更多高级选项。

Matching Routes

路由也可以限制到一个域或子域。 只需定义要匹配的主机模式即可。 它们也可以具有变量:

r := mux.NewRouter()
// Only matches if domain is "www.example.com".
r.Host("www.example.com")
// Matches a dynamic subdomain.
r.Host("{subdomain:[a-z]+}.example.com")

可以添加其他几个匹配器。 要匹配路径前缀:

r.PathPrefix("/products/")

HTTP methods:

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

URL schemes:

r.Schemes("https")

header values:

r.Headers("X-Requested-With", "XMLHttpRequest")

query values:

r.Queries("key", "value")

使用自定义匹配器功能:

r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {return r.ProtoMajor == 0
})

最后,可以在一条路线中组合多个匹配器:

r.HandleFunc("/products", ProductsHandler).Host("www.example.com").Methods("GET").Schemes("http")

按照将其添加到路由器的顺序测试路由。 如果两条路线匹配,则第一个获胜:

r := mux.NewRouter()
r.HandleFunc("/specific", specificHandler)
r.PathPrefix("/").Handler(catchAllHandler)

一次又一次地设置相同的匹配条件可能很无聊,因此我们有一种方法可以将具有相同要求的多条路线分组。 我们称其为“子路由”。

例如,假设我们有几个仅在主机为www.example.com时才匹配的URL。 为该主机创建一条路由,并从中获取一个“子路由器”:

r := mux.NewRouter()
s := r.Host("www.example.com").Subrouter()

然后在子路由器中注册路由:

s.HandleFunc("/products/", ProductsHandler)
s.HandleFunc("/products/{key}", ProductHandler)
s.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)

仅当域为www.example.com时,才会测试我们在上面注册的三个URL路径,因为首先测试了子路由器。 这不仅方便,而且可以优化请求匹配。 您可以创建子路由,以合并路由接受的所有属性匹配器。

子路由器可用于创建域或路径“命名空间”:您可以在中央位置定义子路由器,然后应用的某些部分可以相对于给定子路由器注册其路径。

子路由还有一件事。 当子路由器具有路径前缀时,内部路由将其用作其路径的基础:

r := mux.NewRouter()
s := r.PathPrefix("/products").Subrouter()
// "/products/"
s.HandleFunc("/", ProductsHandler)
// "/products/{key}/"
s.HandleFunc("/{key}/", ProductHandler)
// "/products/{key}/details"
s.HandleFunc("/{key}/details", ProductDetailsHandler)

Static Files:

请注意,提供给PathPrefix()的路径表示一个“通配符”:调用PathPrefix(“ / static /”)。Handler(…)意味着将向处理程序传递与“ / static / *”匹配的所有请求。 这使得使用mux服务静态文件变得容易:

func main() {var dir stringflag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")flag.Parse()r := mux.NewRouter()// This will serve files under http://localhost:8000/static/<filename>r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))srv := &http.Server{Handler:      r,Addr:         "127.0.0.1:8000",// Good practice: enforce timeouts for servers you create!WriteTimeout: 15 * time.Second,ReadTimeout:  15 * time.Second,}log.Fatal(srv.ListenAndServe())
}

服务单页应用程序:

在大多数情况下,将SPA与API放在单独的Web服务器上一起使用是很有意义的,但有时最好从一个位置同时提供这两个服务。 可以编写一个简单的处理程序来服务您的SPA(例如,与React Router的BrowserRouter一起使用),并利用mux强大的路由来实现您的API端点。

package mainimport ("encoding/json""log""net/http""os""path/filepath""time""github.com/gorilla/mux"
)// spaHandler implements the http.Handler interface, so we can use it
// to respond to HTTP requests. The path to the static directory and
// path to the index file within that static directory are used to
// serve the SPA in the given static directory.
type spaHandler struct {staticPath stringindexPath  string
}// ServeHTTP inspects the URL path to locate a file within the static dir
// on the SPA handler. If a file is found, it will be served. If not, the
// file located at the index path on the SPA handler will be served. This
// is suitable behavior for serving an SPA (single page application).
func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {// get the absolute path to prevent directory traversalpath, err := filepath.Abs(r.URL.Path)if err != nil {// if we failed to get the absolute path respond with a 400 bad request// and stophttp.Error(w, err.Error(), http.StatusBadRequest)return}// prepend the path with the path to the static directorypath = filepath.Join(h.staticPath, path)// check whether a file exists at the given path_, err = os.Stat(path)if os.IsNotExist(err) {// file does not exist, serve index.htmlhttp.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))return} else if err != nil {// if we got an error (that wasn't that the file doesn't exist) stating the// file, return a 500 internal server error and stophttp.Error(w, err.Error(), http.StatusInternalServerError)return}// otherwise, use http.FileServer to serve the static dirhttp.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
}func main() {router := mux.NewRouter()router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {// an example API handlerjson.NewEncoder(w).Encode(map[string]bool{"ok": true})})spa := spaHandler{staticPath: "build", indexPath: "index.html"}router.PathPrefix("/").Handler(spa)srv := &http.Server{Handler: router,Addr:    "127.0.0.1:8000",// Good practice: enforce timeouts for servers you create!WriteTimeout: 15 * time.Second,ReadTimeout:  15 * time.Second,}log.Fatal(srv.ListenAndServe())
}

Registered URLs

现在,让我们看看如何构建注册的URL。
可以命名路线。 所有定义名称的路由都可以建立或“反向”其URL。 我们定义一个在路由上调用Name()的名称。 例如:

r := mux.NewRouter()
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).Name("article")

要构建URL,请获取路由并调用URL()方法,并为路由变量传递一系列键/值对。 对于先前的路线,我们将执行以下操作:

url, err := r.Get("article").URL("category", "technology", "id", "42")

gorilla/mux 翻译相关推荐

  1. Gorilla源码分析之gorilla/mux源码分析

    本文公众号文章链接:https://mp.weixin.qq.com/s/LLcPDPtpjNeXAA_ffL3YCg 本文csdn博客链接:http://blog.csdn.net/screscen ...

  2. 【Golang源码分析】Go Web常用程序包gorilla/mux的使用与源码简析

    目录[阅读时间:约10分钟] 一.概述 二.对比: gorilla/mux与net/http DefaultServeMux 三.简单使用 四.源码简析 1.NewRouter函数 2.HandleF ...

  3. gorilla/mux 的学习

    原文链接:gorilla/mux的学习 源代码: package mainimport ("encoding/json""fmt""github.co ...

  4. 路由复用器--gorilla/mux

    简介 gorilla/mux是 gorilla Web 开发工具包中的路由管理库.gorilla Web 开发包是 Go 语言中辅助开发 Web 服务器的工具包.它包括 Web 服务器开发的各个方面, ...

  5. 使用gorilla/mux增强Go HTTP服务器的路由能力

    今天这篇文章我们将会为我们之前编写的 HTTP服务器加上复杂路由的功能以及对路由进行分组管理.在之前的文章<深入学习用 Go 编写HTTP服务器>中详细地讲了使用 net/http进行路由 ...

  6. mysql的请求分发,基于 gorilla/mux 实现路由匹配和请求分发:服务单页面应用

    基于 gorilla/mux 实现路由匹配和请求分发:服务单页面应用 由 学院君 创建于1年前, 最后更新于 1年前 版本号 #1 1279 views 0 likes 0 collects 随着前后 ...

  7. no required module provides package github.com/gorilla/mux

    编写go程序时报错:no required module provides package github.com/gorilla/mux # 命令行执行 go env -w GO111MODULE=a ...

  8. Negroni和Gorilla/mux 解析 Golang

    如有错误欢迎纠正, 有缺漏欢迎补充 参考资料: https://github.com/urfave/negroni/blob/master/translations/README_zh_CN.md h ...

  9. gorilla/mux类库解析

    简介 gorilla/mux实现了一个请求路由和分发的Go框架."mux"的意思是"HTTP request multiplexer",和标准包http.Ser ...

最新文章

  1. mysql被格式化恢复数据_三种常见数据库文件恢复方法介绍
  2. 如何利用python dbus来发送一个信号
  3. adapt和adopt的区别_脸盲了,adopt和adapt要如何区分?
  4. vagrant 配置并启动
  5. java什么时候可能产生内存溢出_哪些场景会产生OOM?怎么解决?
  6. springboot jwt token前后端分离_「转」七个开源的 Spring Boot 前后端分离项目,建议收藏加转载...
  7. Ubuntu装机后的基础应用
  8. Linux上对图片进行压缩
  9. Android,UbuntuCore,ROS;TuringOS,iBotOS,ROOBO
  10. python基础编程语法-Python基础及语法(十三)
  11. 深度学习2.0-普通BP神经网络
  12. 微软正式发布 Azure IoT Central
  13. Android FloatingActionButton(FAB) 悬浮按钮
  14. python pip 快速安装第三方库和下载好whl文件
  15. HTTP Status 500 - /dologin.jsp (line: 27, column: 3) Expecting jsp:param standard action with nam
  16. 等你等了这么久:DTCC2021中国数据库技术大会 Galaxybase万亿大图实践分享——终于来了!
  17. Digispark(ATTINY85) 微型开发板驱动安装与开发环境配置教程
  18. 高中计算机上册知识总结怎么写,高中计算机总结范文
  19. C语言标识符之——“~“
  20. 【VHDL】半减器 and 或门 组成 全减器

热门文章

  1. c语言编程简易计算器代码,可编程简易计算器(代码)
  2. [MATLAB]常微分方程数值求解(ode45 ode15s ode23 solver)
  3. Charting Basics制作图表的基本知识
  4. Unity案例---愤怒的小鸟
  5. ProgressBar.js – 漂亮的响应式 SVG 进度条
  6. MobileNet相关知识整理
  7. QT编写磨砂玻璃效果函数
  8. js实现不同城市空气质量报告显示柱形图
  9. PAT 乙级真题 1032 挖掘机技术哪家强 (附测试点2)
  10. 阅读软件怎么添加书源_继阅读软件后又一款阅读神器,安卓专用,上千书源,无广告,无vip...