猿问

`alice` 没有调用中间件

为什么这里只调用了 的ServeHTTP方法,H1而H2和 的方法H3似乎被忽略了?


alice似乎是一个不错的中间件链接,在这里我尝试将它与httprouter一起使用,但只有外部/最后一个中间件被调用:


package main


import (

    "fmt"

    "github.com/julienschmidt/httprouter"

    "github.com/justinas/alice"

    "net/http"


    "log"

    "time"

)


func main() {

    fmt.Println("started ", time.Now())


    c := alice.New(S1, S2, S3).Then(nil)


    router := httprouter.New()

    router.Handler("GET", "/app", c)


    http.ListenAndServe(":27007", router)

}


func S1(h http.Handler) http.Handler {

    var x H1

    return &x

}


func S2(h http.Handler) http.Handler {

    var x H2

    return &x

}


func S3(h http.Handler) http.Handler {

    var x H3

    return &x

}


type H1 struct{}


func (h *H1) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

    log.Println("H1", time.Now())

}


type H2 struct{}


func (h *H2) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

    log.Println("H2")

}


type H3 struct{}


func (h *H3) ServeHTTP(rw http.ResponseWriter, req *http.Request) {

    log.Println("H3")

}


肥皂起泡泡
浏览 204回答 2
2回答
随时随地看视频慕课网APP

相关分类

Go
我要回答