英文:
Conditional Chaining of http handlers in golang
问题
我想根据特定条件有条件地添加HTTP处理程序。
func ConditionalCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
check, ok := ctx.Value("specific").(bool)
if check {
SpecificCheck(arg).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}
}
func SpecificCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// something
next.ServeHTTP(w, r)
})
}
}
chain := alice.New(ConditionalCheck, .........)
当我测试时,SpecificCheck的HandlerFunc
没有被调用。如何根据条件进行链式调用?
英文:
I want to conditionally add http handler based on certain condition
func ConditionalCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
check, ok := ctx.Value("specific").(bool);
if check {
SpecificCheck(arg)
} else {
next.ServeHTTP(w, r)
}
})
}
}
func SpecificCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// something
next.ServeHTTP(w, r)
})
}
}
chain := alice.New(ConditionalCheck, .........)
When I test, the SpecificCheck HandlerFunc
is not getting invoked.
How do I chain this based on condition?
答案1
得分: 3
SepecificCheck
只返回一个接受一个处理程序并返回另一个处理程序的函数,它不执行任何返回的对象。如果你想执行这些对象,你必须明确地这样做,例如:
SepecificCheck(arg)(next).ServeHTTP(w, r)
上面的代码立即调用SepecificCheck
返回的中间件函数,然后在该中间件函数返回的处理程序上调用ServeHTTP
方法。
然后,更新后的ConditionalCheck
如下所示:
func ConditionalCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
check, ok := ctx.Value("specific").(bool)
if check {
SpecificCheck(arg)(next).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}
}
英文:
SepecificCheck
just returns a function that takes one handler and returns another, it does not execute any of those returned objects. If you want to execute those objects you must do so explicitly, e.g.
SepecificCheck(arg)(next).ServeHTTP(w, r)
The above immediately calls the middleware function returned by SepecificCheck
and then invokes the ServeHTTP
method on the handler returned by that middleware function.
Then the updated ConditionalCheck
would like the following:
func ConditionalCheck(arg string) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
check, ok := ctx.Value("specific").(bool)
if check {
SpecificCheck(arg)(next).ServeHTTP(w, r)
} else {
next.ServeHTTP(w, r)
}
})
}
}
答案2
得分: 1
你的函数SpecificCheck只返回了一个匿名函数,但没有调用它。你需要添加对返回函数的调用,像这样SpecificCheck(arg)()
例如:
package main
import "fmt"
func main() {
test("Eugene")
}
func test(name string) func() {
return func() {
fmt.Println("Hello! " + name)
}
}
如你所见,输出为空。
但是这段代码将会正常工作:
package main
import "fmt"
func main() {
test("Eugene")()
}
func test(name string) func() {
return func() {
fmt.Println("Hello! " + name)
}
}
英文:
Your function SpecificCheck is returning anonymus function only, but not calling it. You have to add call of returned function, like this SpecificCheck(arg)()
For example
<pre>
package main
import "fmt"
func main() {
test("Eugene")
}
func test(name string) func() {
return func() {
fmt.Println("Hello! " + name)
}
}
</pre>
As you can see, output empty
But this code will work right
<pre>
package main
import "fmt"
func main() {
test("Eugene")()
}
func test(name string) func() {
return func() {
fmt.Println("Hello! " + name)
}
}
</pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论