http HandleFunc argument in golang

huangapple go评论124阅读模式
英文:

http HandleFunc argument in golang

问题

我想使用一个速率限制或节流器库来限制客户端请求的数量。我在我的代码库中使用了一个供应商库。我想传入一个ResponseWriterRequest和从URL中检索到的第三个变量。当我使用节流器库进行节流时,它只返回一个处理程序,该处理程序只处理两个参数。我该如何将我的第三个参数传递给处理程序?

以下是我的当前代码:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/didip/tollbooth"
  5. "net/http"
  6. )
  7. func viewHandler(
  8. w http.ResponseWriter,
  9. r *http.Request,
  10. uniqueId string,
  11. ) {
  12. //data := getData(uniqueId)
  13. fmt.Println("Id:", uniqueId)
  14. p := &objects.ModelApp{LoggedUser: "Ryan Hardy", ViewData: "data"}
  15. renderTemplate(w, "view", p)
  16. }
  17. //URL validation for basic web services
  18. var validPath = regexp.MustCompile("^/$|/(home|about|view)/(|[a-zA-Z0-9]+)$")
  19. func makeHandler(
  20. fn func(
  21. http.ResponseWriter,
  22. *http.Request, string,
  23. )) http.HandlerFunc {
  24. return func(
  25. w http.ResponseWriter,
  26. r *http.Request,
  27. ) {
  28. m := validPath.FindStringSubmatch(r.URL.Path)
  29. if m == nil {
  30. http.NotFound(w, r)
  31. return
  32. }
  33. fn(w, r, m[2])
  34. }
  35. }
  36. func main() {
  37. http.Handle("/view/", makeHandler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, time.Second), viewHandler)))
  38. http.ListenAndServe(":8080", nil)
  39. }

有人可以帮我解决这个问题吗?

英文:

I want to use a rate limiting or throttler library to limit the number of client requests. I use a vendor library in my code base. I want to pass in a ResponseWriter, Request and a third variable retrieved from the URL. When I use the library for throttling, it gives me back a handler that only handles two arguments. How can I pass my third argument into the handler?

Here is my current code:

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/didip/tollbooth"
  5. "net/http"
  6. )
  7. func viewHandler(
  8. w http.ResponseWriter,
  9. r *http.Request,
  10. uniqueId string,
  11. ) {
  12. //data := getData(uniqueId)
  13. fmt.Println("Id:", uniqueId)
  14. p := &objects.ModelApp{LoggedUser: "Ryan Hardy", ViewData: "data"}
  15. renderTemplate(w, "view", p)
  16. }
  17. //URL validation for basic web services
  18. var validPath = regexp.MustCompile("^/$|/(home|about|view)/(|[a-zA-Z0-9]+)$")
  19. func makeHandler(
  20. fn func(
  21. http.ResponseWriter,
  22. *http.Request, string,
  23. )) http.HandlerFunc {
  24. return func(
  25. w http.ResponseWriter,
  26. r *http.Request,
  27. ) {
  28. m := validPath.FindStringSubmatch(r.URL.Path)
  29. if m == nil {
  30. http.NotFound(w, r)
  31. return
  32. }
  33. fn(w, r, m[2])
  34. }
  35. }
  36. func main() {
  37. http.Handle("/view/", makeHandler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, time.Second), viewHandler)))
  38. http.ListenAndServe(":8080", nil)
  39. }

Could anyone help me with this?

答案1

得分: 1

我在手机上,所以可能很难输入,但你可以使用http.Handle函数,该函数接受一个Handler接口的参数,类似于以下代码:

  1. type makeHandler struct {
  2. YourVariable string
  3. }
  4. func (m *makeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  5. yourVariableYouNeed := m.YourVariable
  6. // 做你需要的任何操作
  7. w.Write()
  8. }
  9. // 做你需要的操作以获取你的变量
  10. blah := &makeHandler{yourThing}
  11. http.Handle("/views", blah)

我无法测试,因为我在手机上,但它应该可以工作。如果不能,请告诉我。

英文:

I'm on my phone so this may be difficult to type but you could use the http.Handle function which takes an interface of Handler something like

  1. type makeHandler struct {
  2. YourVariable string
  3. }
  4. func (m *makeHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
  5. yourVariableYouNeed := m.YourVariable
  6. // do whatever
  7. w.Write()
  8. }
  9. // do whatever you need to get your variable
  10. blah := &makeHandler{ yourThing }
  11. http.Handle("/views", blah)

On my phone so can't test but it should work, let me know if it doesn't.

huangapple
  • 本文由 发表于 2015年11月21日 16:45:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/33841459.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定