dsdt66064367 2016-06-01 22:02
浏览 32
已采纳

如何将子域与大猩猩Mux匹配

I need to build a route which matches two subdomains(prefix.api.example.com and prefix.api.sandbox.example.com) using gorilla mux router. So far I have the regex below but the router returns 404 on requests. Any idea why is that?

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

More code

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))
}

Request:

$ 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
  • 写回答

1条回答 默认 最新

  • dongpengyu1363 2016-06-02 00:16
    关注

    The ^ and $ metacharacters for matching beginning and end of lines should be removed, the parens can be as well.

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

    My hosts file:

    ○ 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
    

    Gives me the following:

    ○ 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
    

    Updated:

    Here are the regexes generated by the two different .Host()'s:

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

    regexp: ^prefix\.api(?P<v0>(^$|^\.sandbox$))\.example\.com$

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

    regexp: ^prefix\.api(?P<v0>|\.sandbox)\.example\.com$

    • Example tests for both regexes can be played with here at play.golang
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 Spring MVC项目,访问不到相应的控制器方法
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?
  • ¥15 关于#genesiscsheel#的问题,如何解决?
  • ¥15 Android aidl for hal
  • ¥15 STM32CubeIDE下载程序报错
  • ¥15 微信好友如何转变为会员系统?(相关搜索:小程序)
  • ¥15 c# 直接使用c++ 类库文件
  • ¥15 一个主机电脑有两个显示器,当前有两个软件主体是网页html打包的exe程序,如何通过cmd命令bat同时打开软件一个在主屏幕显示,另外一个软件在2副屏幕上显示