http HandleFunc argument in golang

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

http HandleFunc argument in golang

问题

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

以下是我的当前代码:

package main

import (
	"fmt"
	"github.com/didip/tollbooth"
	"net/http"
)

func viewHandler(
	w http.ResponseWriter,
	r *http.Request,
	uniqueId string,
) {
	//data := getData(uniqueId)
	fmt.Println("Id:", uniqueId)
	p := &objects.ModelApp{LoggedUser: "Ryan Hardy", ViewData: "data"}
	renderTemplate(w, "view", p)
}

//URL validation for basic web services
var validPath = regexp.MustCompile("^/$|/(home|about|view)/(|[a-zA-Z0-9]+)$")

func makeHandler(
	fn func(
		http.ResponseWriter,
		*http.Request, string,
	)) http.HandlerFunc {
	return func(
		w http.ResponseWriter,
		r *http.Request,
	) {
		m := validPath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}
		fn(w, r, m[2])
	}
}

func main() {
	http.Handle("/view/", makeHandler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, time.Second), viewHandler)))
	http.ListenAndServe(":8080", nil)
}

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

英文:

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:

package main

import (
	"fmt"
    "github.com/didip/tollbooth"
    "net/http"
    )

func viewHandler(
	w http.ResponseWriter, 
	r *http.Request, 
	uniqueId string,
	) {
	//data := getData(uniqueId)
	fmt.Println("Id:", uniqueId)
	p := &objects.ModelApp{LoggedUser: "Ryan Hardy", ViewData: "data"}
	renderTemplate(w, "view", p)
}

//URL validation for basic web services
var validPath = regexp.MustCompile("^/$|/(home|about|view)/(|[a-zA-Z0-9]+)$")

func makeHandler(
	fn func(
		http.ResponseWriter, 
		*http.Request, string,
		)) http.HandlerFunc {
	return func(
		w http.ResponseWriter, 
		r *http.Request,
		) {
		m := validPath.FindStringSubmatch(r.URL.Path)
		if m == nil {
			http.NotFound(w, r)
			return
		}
		fn(w, r, m[2])
	}
}

func main() {
    http.Handle("/view/", makeHandler(tollbooth.LimitFuncHandler(tollbooth.NewLimiter(1, time.Second), viewHandler)))
    http.ListenAndServe(":8080", nil)
}

Could anyone help me with this?

答案1

得分: 1

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

type makeHandler struct {
    YourVariable string
}

func (m *makeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    yourVariableYouNeed := m.YourVariable
    // 做你需要的任何操作
    w.Write()
}

// 做你需要的操作以获取你的变量
blah := &makeHandler{yourThing}

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

type makeHandler struct {
    YourVariable string
}

func (m *makeHandler) ServeHTTP (w http.ResponseWriter, r *http.Request) {
    yourVariableYouNeed := m.YourVariable
    // do whatever 
    w.Write()
}

// do whatever you need to get your variable
blah := &makeHandler{ yourThing }

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:

确定