我需要将GetAssetsCompute函数包装在中间件中
r.Handle("/api/v1/assets/ComputeBlade", GetAssetsCompute(assetService)).Methods("GET")
func GetAssetsCompute(assetService ServiceType) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// stuff here
})
}
但是因为中间件将 HTTP 处理程序作为参数,而我的函数不是处理程序,所以我不能。
我正在考虑做这样的事情。
func GetAssetsCompute(assetService ServiceType) http.Handler {
return MyMiddleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// stuff here
}))
}
func MyMiddleware(next http.Handler) http.Handler {
}
这个对吗?或者有没有更好的方法来做到这一点。同样在中间件内部,我需要访问 URL 端点,进行一些处理并存储这个处理后的值,然后再次在主处理程序中访问它。我怎样才能做到这一点?
编辑:我想将此中间件仅应用于我拥有的端点的子集(> 1)。不是全部
我还需要处理程序中函数中assetService使用的变量。GetAssetsCompute(assetService ServiceType)所以,我也需要这个闭包。
鸿蒙传说
相关分类