dongshan4316 2016-10-22 03:02
浏览 34

大猩猩Mux正则表达式用于范围和预定义选项之间的数字

My route looks like this

max := viper.GetInt("channels")

lights_router.Path("/{channel}/{action}").
    Methods("OPTIONS","GET").
    Handler( util.Adapt(SerialHandler(router), util.EnableCORS()))

Channels have to be between 1 and max and action has to be either false or true.

  • 写回答

1条回答 默认 最新

  • dongxiaoxing3058 2016-11-06 13:20
    关注
    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)
            })
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。