gorilla/mux 请求不匹配 URL 模式

gorilla/mux requests not matching URL pattern

我正在访问一些我以前编写的简单网络服务器的旧代码,但我的请求模式不再有效。我有一个函数可以按如下方式初始化我的路线:

func (a *App) InitializeRoutes() {
    a.Router.HandleFunc("/api/forecast/detailed?city={city}&state={state}&period={period}", a.DetailedForecast).Methods("GET")
    a.Router.HandleFunc("/api/forecast/detailed?city={city}&state={state}", a.DetailedForecast).Methods("GET")
    a.Router.HandleFunc("/api/forecast/detailed?random", a.RandomDetailedForecast).Methods("GET")
    a.Router.HandleFunc("/api/forecast/hourly?city={city}&state={state}&hours={hours}", a.HourlyForecast).Methods("GET")
    a.Router.HandleFunc("/api/forecast/hourly?city={city}&state={state}", a.HourlyForecast).Methods("GET")
    a.Router.NotFoundHandler = http.HandlerFunc(a.Custom404Handler)
}

我的自定义 404 处理程序只是 returns 一个 json of {"error": "Not Found"}

从这里开始,我在我的应用程序的初始化函数(底部 3 行)末尾初始化这些路由:

func (a *App) Initialize() {
    var err error
    conf.configure()
    sqlDataSource := fmt.Sprintf(
        "postgres://%s:%s@%s:%s/%s?sslmode=disable",
        conf.sqlUsername,
        conf.sqlPassword,
        conf.sqlHost,
        conf.sqlPort,
        conf.sqlDbName)
    a.DB, err = sql.Open("postgres", sqlDataSource)
    if err != nil {
        log.Fatal(err)
    }
    a.Redis = redis.NewClient(&redis.Options{
        Addr:     fmt.Sprintf("%s:%s", conf.redisHost, conf.redisPort),
        Password: conf.redisPassword,
        DB:       conf.redisDb,
    })
    a.Router = mux.NewRouter()
    a.Logger = handlers.CombinedLoggingHandler(os.Stdout, a.Router)
    a.InitializeRoutes()
}

我的应用在以下函数上运行:

func (a *App) Run() {
    port := fmt.Sprintf(":%s", os.Getenv("SERVER_PORT"))
    log.Fatal(http.ListenAndServe(port, a.Logger))
}

我的服务器运行在:

func main() {
    a := App{}
    a.Initialize()
    a.Run()
}

当我尝试向据我所知是有效的 URL 模式发出请求时,我得到以下信息:

>>> import requests
>>> r = requests.get("http://localhost:8000/api/forecast/detailed?city=Chicago&state=IL")
>>> r
<Response [404]>
>>> r.json()
{'error': 'Not Found'}

编辑

我进行了更改以将查询移出 URL。我的路线现在看起来像这样:

a.Router.HandleFunc("/api/forecast/detailed", a.DetailedForecast).Queries("city", "{city:[a-zA-Z+]+}", "state", "{state:[a-zA-Z+]+}", "period", "{period:[a-zA-Z+]+}").Schemes("http").Methods("GET")
a.Router.HandleFunc("/api/forecast/detailed", a.DetailedForecast).Queries("city", "{city:[a-zA-Z+]+}", "state", "{state:[a-zA-Z+]+}").Schemes("http").Methods("GET")
a.Router.HandleFunc("/api/forecast/detailed/random", a.RandomDetailedForecast).Schemes("http").Methods("GET")
a.Router.HandleFunc("/api/forecast/hourly", a.HourlyForecast).Queries("city", "{city:[a-zA-Z+]+}", "state", "{state:[a-zA-Z+]+}", "hours", "{hours:[0-9]+}").Schemes("http").Methods("GET")
a.Router.HandleFunc("/api/forecast/hourly", a.HourlyForecast).Queries("city", "{city:[a-zA-Z+]+}", "state", "{state:[a-zA-Z+]+}").Schemes("http").Methods("GET")

当我发出同样的请求时,我现在看到:

Get : unsupported protocol scheme ""

我检查了 URL 以查看正在发送的内容:

2020/02/20 18:19:41 Scheme is:
2020/02/20 18:19:41 Host is:
2020/02/20 18:19:41 Path is: /api/forecast/detailed
2020/02/20 18:19:41 Query is: city=Chicago&state=IL

路径和查询看起来是正确的,但我不明白为什么方案无效。我的要求还是r = requests.get("http://localhost:8000/api/forecast/detailed?city=Chicago&state=IL")

来自 README... 要匹配您需要使用的查询

r := mux.NewRouter()
...
r.Queries("key", "value")

匹配PathPrefix

r.PathPrefix("/products/")

我认为你需要结合这两者来实现你想要的。