大猩猩多路复用中间件:包装处理程序

问题描述 投票:0回答:1

我需要将GetAssetsCompute函数包装在中间件中

r.HandlerFunc("/api/v1/assets/ComputeBlade", GetAssetsCompute(assetService)).Methods("GET")

但是因为中间件将HTTP处理程序作为参数,而我的函数不是我不能使用的处理程序。

func GetAssetsCompute(assetService ServiceType) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // do stuff here
    })
}

我当时正在考虑做这样的事情。

func GetAssetsCompute(assetService ServiceType) http.Handler {
        return MyMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            // do stuff here
        }))
    }


func MyMiddleware(next http.Handler) http.Handler {

}

这样好吗?还是有更好的方法来做到这一点。同样在中间件内部,我需要访问和存储端点,然后在主处理程序中访问它。我怎样才能做到这一点?

go gorilla mux
1个回答
0
投票

解决您的问题的方法(我不确定这是否是唯一的方法)是将中间件应用于整个router对象。

© www.soinside.com 2019 - 2024. All rights reserved.