【问题标题】:How to match subdomain with gorilla mux如何将子域与 gorilla mux 匹配
【发布时间】:2016-10-01 11:06:25
【问题描述】:

我需要使用 gorilla mux 路由器构建一个匹配两个子域(prefix.api.example.com 和 prefix.api.sandbox.example.com)的路由。到目前为止,我有下面的正则表达式,但路由器在请求时返回 404。知道这是为什么吗?

router := mux.NewRouter()
route :=  router.Host(`prefix.api{_:(^$|^\.sandbox$)}.example.com`)

更多代码

package main

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

type handler struct{}

func (_ handler)ServeHTTP(w http.ResponseWriter, r *http.Request){
    w.Write([]byte("hello world"))
    w.WriteHeader(200)

}
func main() {
    router := mux.NewRouter().StrictSlash(true)
    route :=  router.Host(`prefix.api{_:(^$|^\.sandbox$)}.example.com`)
    route.Handler(handler{})
    http.Handle("/", router)
      panic(http.ListenAndServe(":80", nil))
}

请求:

$ curl prefix.api.sandbox.example.com/any -v
*   Trying 127.0.0.1...
* Connected to prefix.api.sandbox.example.com (127.0.0.1) port 80 (#0)
> GET /some HTTP/1.1
> Host: prefix.api.sandbox.example.com
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 404 Not Found
< Content-Type: text/plain; charset=utf-8
< X-Content-Type-Options: nosniff
< Date: Wed, 01 Jun 2016 22:08:21 GMT
< Content-Length: 19
< 
404 page not found
* Connection #0 to host prefix.api.sandbox.example.com left intact

【问题讨论】:

    标签: regex go gorilla


    【解决方案1】:

    应该删除用于匹配行首和行尾的^ 和$ 元字符,括号也可以。

    route := router.Host(`prefix.api{_:|\.sandbox}.example.com`)`
    

    我的主机文件:

    ○ grep prefix /etc/hosts
    127.0.0.1   prefix.api.example.com
    127.0.0.1   prefix.api.sandbox.example.com
    127.0.0.1   prefix.api.xsandbox.example.com
    

    给我以下内容:

    ○ curl prefix.api.example.com:8000
    hello world%                                                                                                                                                                                                                                                                    
    ○ curl prefix.api.sandbox.example.com:8000
    hello world%                                                                                                                                                                                                                                                                    
    ○ curl prefix.api.xsandbox.example.com:8000
    404 page not found
    

    更新:

    这是由两个不同的.Host() 生成的正则表达式:

    route :=  router.Host(`prefix.api{_:(^$|^\.sandbox$)}.example.com`)
    

    正则表达式:^prefix\.api(?P&lt;v0&gt;(^$|^\.sandbox$))\.example\.com$

    route := router.Host(`prefix.api{_:|\.sandbox}.example.com`)
    

    正则表达式:^prefix\.api(?P&lt;v0&gt;|\.sandbox)\.example\.com$

    • 可以使用两个正则表达式的示例测试 here at play.golang

    【讨论】:

    • 我认为这种方式匹配的路线比它应该的多,不是吗? play.golang.org/p/uvxjxrfDrE
    • 如果是这样,prefix.api.xsandbox.example.com不应该返回hello world吗?
    • @Theuserwithnohat 更新了生成的主机正则表达式。
    • 我不太明白...实际/最终的正则表达式与您在路线中输入的不同吗? gorilla mux 是否在 std 正则表达式之上添加了自己的风味?问题不仅在于需要什么正则表达式,还在于它是如何工作的(即为什么我的正则表达式不起作用)
    • 关于前面的评论 prefix.api.xsandbox.example.com 不应该匹配任何路线,因为我只定义了 prefix.api.sandbox.example.com 和 prefix.api.example.com。为了匹配prefix.api.xsandbox.example.com,我认为“沙盒”之前应该有一个通配符,但事实并非如此。
    猜你喜欢
    • 2014-12-22
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    • 2020-08-19
    • 2013-11-08
    • 2017-09-11
    • 1970-01-01
    • 2015-12-01
    相关资源
    最近更新 更多