登录
首页 >  Golang >  Go问答

使用 Gorilla Mux 实现统一路由处理

来源:stackoverflow

时间:2024-03-21 09:51:30 238浏览 收藏

本文探讨了使用 Gorilla Mux 库为每个端点应用错误处理程序的方法。传统上,每个路由函数都必须用 roothandler 包装,但本文提供了替代方案:将 roothandler 设置为中间件。此外,文章介绍了使用上下文将错误传递回中间件或使用错误记录器包装每个处理程序等其他方法。最后,文章强调了使用上下文传回值时设置占位符的重要性。

问题内容

我正在使用一个有价值的 roothandler 来处理生成的错误。我想知道如何为我的 api 可能服务的每个端点使用此处理程序。

r := mux.newrouter()

r.handle("/api/endpoint0", roothandler(function0)).methods("post")
r.handlefunc("/api/endpoint1", function1).methods("post")
r.handlefunc("/api/endpoint2", function2).methods("post")

    s := &http.server{
        handler:      r,
        addr:         "127.0.0.1:8080",
        writetimeout: 15 * time.second,
        readtimeout:  15 * time.second,
    }

我的错误处理程序工作如下:

type roothandler func(http.responsewriter, *http.request) error

func (fn roothandler) servehttp(w http.responsewriter, r *http.request) {
    err := fn(w, r) // call handler function
    if err == nil {
        return
    }
    //log error or w/e
}

func newhttperror(err error, status int, detail string) error {
    return &httperror{
        cause:  err,
        detail: detail,
        status: status,
    }
}

我使用的示例函数之一:

func function0(w http.ResponseWriter, r *http.Request) (err error) {
    if err = notLogged(); err != nil {
        return NewHTTPError(err, 400, "not authorized")
    }

}

我是否必须用 roothanlder(functionname) 包装每个路由函数,或者有没有办法将其应用于每个路由?


解决方案


根据 roothandler 的具体结构,您也许可以将其设置为中间件:

func getroothandlermw(authuser sometype) func(http.handler) http.handler {
  return func(next http.handler) http.handler {
    return http.handlerfunc(func(w http.responsewriter, r *http.request) {
        // do what needs to be done?
        next.servehttp(w, r)
    })
  }
}

...
r.use(getroothandlermw(authuser))

一个简单的日志记录中间件如下所示:

func logmw(next http.handler) http.handler {
    return http.handlerfunc(func(w http.responsewriter, r *http.request) {
        logrequest(...)
        next.servehttp(w, r)
    })
}

...
r.use(logmw)

从您添加的代码来看,您似乎想要更改 http 处理程序函数以返回错误。这会更改函数的签名,并且您不能将其用作处理程序。您可以:

  • 使用上下文将错误传递回中间件。也就是说,在处理程序的请求上下文中设置一些错误值,以便记录器中间件可以在处理程序返回后记录它,或者
  • 用错误记录器包装每个处理程序,就像您现在所做的那样。

向上下文添加值将创建一个新的上下文,因此如果您使用上下文从处理程序传回值,则必须首先设置一个占位符。像这样的事情:

type RequestError struct {
   Err error
}

type requestErrorKeyType int
const requestErrorKey requestErrorKeyType=iota

func SetRequestError(req *http.Request, err error) {
   if r:=req.Context().Value(requestErrorKey); r!=nil {
       r.(*RequestError).Err=err
   }
}

// In the handler...
requestError:=RequestError{}
newReq:=request.WithContext(request.Context().WithValue(requestErrorKey,&requestError))
next.ServeHTTP(w,newReq)
if requestError.Err!=nil {
  ...
}

今天关于《使用 Gorilla Mux 实现统一路由处理》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>