我正在訪問我前段時(shí)間編寫的簡單網(wǎng)絡(luò)服務(wù)器的一些舊代碼,我的請求模式不再有效。我有一個(gè)初始化我的路由的函數(shù),如下所示: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 處理程序只返回一個(gè) {"error": "Not Found"} 的 json從這里開始,我在我的應(yīng)用程序的初始化函數(shù)(底部 3 行)結(jié)束時(shí)初始化這些路由: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()}我的應(yīng)用程序在以下功能上運(yùn)行:func (a *App) Run() { port := fmt.Sprintf(":%s", os.Getenv("SERVER_PORT")) log.Fatal(http.ListenAndServe(port, a.Logger))}我的服務(wù)器運(yùn)行在:func main() { a := App{} a.Initialize() a.Run()}當(dāng)我嘗試向我所知的有效 URL 模式發(fā)出請求時(shí),我得到以下信息:>>> import requests>>> r = requests.get("http://localhost:8000/api/forecast/detailed?city=Chicago&state=IL")>>> r<Response [404]>>>> r.json(){'error': 'Not Found'}
1 回答

至尊寶的傳說
TA貢獻(xiàn)1789條經(jīng)驗(yàn) 獲得超10個(gè)贊
從自述文件...要匹配您需要使用的查詢
r := mux.NewRouter()
...
r.Queries("key", "value")
匹配 PathPrefix
r.PathPrefix("/products/")
我認(rèn)為您需要將這兩者結(jié)合起來才能實(shí)現(xiàn)您想要的。
- 1 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報(bào)
0/150
提交
取消