简体   繁体   English

Golang negroni和http.NewServeMux()问题

[英]Golang negroni and http.NewServeMux() issue

I am running a server with below code: 我正在使用以下代码运行服务器:

// Assuming there is no import error
  mux := http.NewServeMux()
  mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
  http.Error(w, "File not found", http.StatusNotFound)
  })

  n := negroni.Classic()
  n.Use(negroni.HandlerFunc(bodmas.sum(4,5)))
  n.UseHandler(mux)
  n.Run(":4000" )

It works perfectly fine. 它工作得很好。

But when I wrap bodmas.sum with another http handler I always get "File not found." 但是,当我用另一个http handler包装bodmas.sum ,总是得到“找不到文件”。 The flow does not go to this route. 流程不去这条路线。

  mux := http.NewServeMux()
  mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
    http.Error(w, "File not found", http.StatusNotFound)
  })

  n := negroni.Classic()
  n.Use(negroni.HandlerFunc(wrapper.RateLimit(bodmas.sum(4,5),10)))
  n.UseHandler(mux)
  n.Run(":" + cfg.Server.Port)
}

wrapper.RateLimit is define as below. wrapper.RateLimit定义如下。 This works as expected when tested separately: 当单独测试时,这可以按预期工作:

func RateLimit(h resized.HandlerFunc, rate int) (resized.HandlerFunc) {
    :
    :
  // logic here

    rl, _ := ratelimit.NewRateLimiter(rate)

    return func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc){
        if rl.Limit(){
            http.Error(w, "Gateway Timeout", http.StatusGatewayTimeout )
        } else {
             next(w, r)
         }
    }
}

There is no error. 没有错误。 Any suggestions about this behavior ? 关于此行为有什么建议吗? How to make it work ? 如何使其工作?

I am not sure what sure what is the problem with this code, but it appears to be not the negorni way. 我不确定这段代码有什么问题,但这似乎不是negorni方法。 negroni might not behave as expected if we wrap its handler with another http.Handlerfunc . 如果我们将其处理程序包装到另一个http.Handlerfuncnegroni可能无法达到预期的http.Handlerfunc So, I modified my code and got the work done by middleware . 因此,我修改了代码,并由middleware完成了工作。

My current code looks something like as below: 我当前的代码如下所示:

         mux := http.NewServeMux()
          mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
            http.Error(w, "File not found", http.StatusNotFound)
          })

          n := negroni.Classic()
          n.Use(wrapper.Ratelimit(10))
          n.Use(negroni.HandlerFunc(bodmas.sum(4,5)))
          n.UseHandler(mux)
          n.Run(":4000")
    }

wrapper.go has : wrapper.go具有:

    type RatelimitStruct struct {
          rate int 
    }

    // A struct that has a ServeHTTP method
    func Ratelimit(rate  int) *RatelimitStruct{
        return &RatelimitStruct{rate}
    }

    func (r *RatelimitStruct) ServeHTTP(w http.ResponseWriter, req *http.Request, next       http.HandlerFunc){
        rl, _ := ratelimit.NewRateLimiter(r.rate)

        if rl.Limit(){
            http.Error(w, "Gateway Timeout", http.StatusGatewayTimeout )
        }

 else {
        next(w, req)
    } 
}

Hope it helps someone. 希望它可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM