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