Golang Gorilla mux,匹配两个URL签名的最佳方法是什么?

huangapple go评论89阅读模式
英文:

Golang Gorilla mux, best way to match against two url signatures

问题

使用gorilla mux,我目前有许多形式为:

domain.com/org/{subdomain}/{name}/pagename

的URL。代码如下所示:

rtr.HandleFunc("/org/{subdomain}/{name}/promote", promoteView)

我还想匹配:

subdomain.domain.com/{name}/pagename

我知道可以这样做:

rtr.Host("{subdomain:[a-z]+}.domain.com").HandleFunc("/{name}/promote", promoteView)

来匹配子域名。是否可能只有一个HandleFunc()可以同时匹配这两种类型的URL,还是我需要有两个HandleFunc(),一个用于第一种情况,一个用于subdomain.domain.com的情况?

英文:

Using gorilla mux, I currently have many URLs that are of the form:

domain.com/org/{subdomain}/{name}/pagename

such that the code looks like:

rtr.HandleFunc("/org/{subdomain}/{name}/promote", promoteView)

I'd like to also match against:

subdomain.domain.com/{name}/pagename

I know I can do something like

rtr.Host("{subdomain:[a-z]+}.domain.com").HandleFunc("/{name}/promote", promoteView)

to match on the subdomain. Is it possible to only have one HandleFunc() that will match both types of URLs, or do I need to have two HandleFunc()s, one for the first case and one for the subdomain.domain.com case?

答案1

得分: 1

使用这样的调度程序,您只需要为每个路由/处理程序添加一行代码。

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

type key struct {
	subdomain, name string
}

type dispatcher map[key]http.Handler

func (d dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	handler, ok := d[key{vars["subdomain"], vars["name"]}]

	if ok {
		handler.ServeHTTP(w, r)
		return
	}
	http.NotFound(w, r)
}

func handleA(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(rw, "handleA serving")
}

func handleB(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(rw, "handleB serving")
}

var Dispatcher = dispatcher{
	key{"subA", "nameA"}: http.HandlerFunc(handleA),
	key{"subB", "nameB"}: http.HandlerFunc(handleB),
    // 在这里添加您的新路由
}

func main() {
	r := mux.NewRouter()
	r.Handle("/org/{subdomain}/{name}/promote", Dispatcher)
    r.Host("{subdomain:[a-z]+}.domain.com").Path("/{name}/promote").Handler(Dispatcher)
	
	http.ListenAndServe(":8080", r)
}

希望对您有所帮助!

英文:

With a dispatcher like this you only have to add one line per router/handler.

package main

import (
	"fmt"
	"github.com/gorilla/mux"
	"net/http"
)

type key struct {
	subdomain, name string
}

type dispatcher map[key]http.Handler

func (d dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	handler, ok := d[key{vars["subdomain"], vars["name"]}]

	if ok {
		handler.ServeHTTP(w, r)
		return
	}
	http.NotFound(w, r)
}

func handleA(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(rw, "handleA serving")
}

func handleB(rw http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(rw, "handleB serving")
}

var Dispatcher = dispatcher{
	key{"subA", "nameA"}: http.HandlerFunc(handleA),
	key{"subB", "nameB"}: http.HandlerFunc(handleB),
    // add your new routes here
}

func main() {
	r := mux.NewRouter()
	r.Handle("/org/{subdomain}/{name}/promote", Dispatcher)
    r.Host("{subdomain:[a-z]+}.domain.com").Path("/{name}/promote").Handler(Dispatcher)
	
	http.ListenAndServe(":8080", r)
}

huangapple
  • 本文由 发表于 2015年5月1日 00:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/29973639.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定