我的路线看起来像这样
max := viper.GetInt("channels")
lights_router.Path("/{channel}/{action}").
Methods("OPTIONS","GET").
Handler( util.Adapt(SerialHandler(router), util.EnableCORS()))
频道必须介于1和最大值之间,且操作必须为false或true。
答案 0 :(得分:0)
func ValidetaChannel() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
channel, err := strconv.Atoi(mux.Vars(r)["channel"])
if err != nil {
http.Error(w, http.StatusText(400), 400)
return
}
if channel >= 1 && channel <= viper.GetInt("channels") {
h.ServeHTTP(w, r)
return
}
http.Error(w, http.StatusText(400), 400)
})
}
}
func ValidetaAction() Adapter {
return func(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if mux.Vars(r)["action"] == "true" || mux.Vars(r)["action"] == "false" {
h.ServeHTTP(w, r)
return
}
http.Error(w, http.StatusText(400), 400)
})
}
}