gorilla/mux 翻译

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 string

    flag.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 main

import (
	"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 string
	indexPath  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 traversal
	path, err := filepath.Abs(r.URL.Path)
	if err != nil {
        // if we failed to get the absolute path respond with a 400 bad request
        // and stop
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

    // prepend the path with the path to the static directory
	path = 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.html
		http.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 stop
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

    // otherwise, use http.FileServer to serve the static dir
	http.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 handler
		json.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")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值