相关疑难解决方法(0)

Gorilla mux定制中间件

我正在使用gorilla mux进行管理路由.我缺少的是在每个请求之间集成中间件.

例如

package main

import (
    "fmt"
    "github.com/gorilla/mux"
    "log"
    "net/http"
    "strconv"
)

func HomeHandler(response http.ResponseWriter, request *http.Request) {

    fmt.Fprintf(response, "Hello home")
}

func main() {

    port := 3000
    portstring := strconv.Itoa(port)

    r := mux.NewRouter()
    r.HandleFunc("/", HomeHandler)
    http.Handle("/", r)

    log.Print("Listening on port " + portstring + " ... ")
    err := http.ListenAndServe(":"+portstring, nil)
    if err != nil {
        log.Fatal("ListenAndServe error: ", err)
    }
}
Run Code Online (Sandbox Code Playgroud)

每个传入的请求都应该通过中间件.我怎样才能在这里整合中间件?

更新

我将它与大猩猩/会话结合使用,他们说:

重要说明:如果您不使用gorilla/mux,则需要使用context.ClearHandler包装处理程序,否则您将泄漏内存!一个简单的方法是在调用http.ListenAndServe时包装顶级多路复用器:

我该如何防止这种情况?

go gorilla

27
推荐指数
4
解决办法
2万
查看次数

标签 统计

go ×1

gorilla ×1