为了账号安全,请及时绑定邮箱和手机立即绑定

如何在 golang 中间件中获取 Response statusCode?

如何在 golang 中间件中获取 Response statusCode?

Go
呼如林 2023-05-08 15:54:51
如何在 golang 中间件中获取 Response statusCode?ResponseWriter 只有WriteHeader 接口,找不到get 接口。
查看完整描述

4 回答

?
小怪兽爱吃肉

TA贡献1852条经验 获得超1个赞

这种方法是可行的。


    type loggingResponseWriter struct {

        http.ResponseWriter

        statusCode int

    }


    func NewLoggingResponseWriter(w http.ResponseWriter) *loggingResponseWriter {

        return &loggingResponseWriter{w, http.StatusOK}

    }


    func (lrw *loggingResponseWriter) WriteHeader(code int) {

        lrw.statusCode = code

        lrw.ResponseWriter.WriteHeader(code)

    }


    func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {

        return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

            log.Printf("--> %s %s", req.Method, req.URL.Path)


            lrw := NewLoggingResponseWriter(w)

            wrappedHandler.ServeHTTP(lrw, req)


            statusCode := lrw.statusCode

            log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))

        })

    }


查看完整回答
反对 回复 2023-05-08
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞

使用内格罗尼。它的工作原理与@huangapple 的答案相同,但处理程序实际上实现了所有接口。


import (

    "github.com/urfave/negroni"

)


func wrapHandlerWithLogging(wrappedHandler http.Handler) http.Handler {

    return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {

        log.Printf("--> %s %s", req.Method, req.URL.Path)


        lrw := negroni.NewResponseWriter(w)

        wrappedHandler.ServeHTTP(lrw, req)


        statusCode := lrw.Status()

        log.Printf("<-- %d %s", statusCode, http.StatusText(statusCode))

    })

}


查看完整回答
反对 回复 2023-05-08
?
慕桂英546537

TA贡献1848条经验 获得超10个赞

长话短说,您应该自己包装 http.ResponseWriter 或使用库。如果你想自己实现它,你可以从Negroni源代码中找到一些提示



查看完整回答
反对 回复 2023-05-08
?
精慕HU

TA贡献1845条经验 获得超8个赞

在我的例子中,我不使用外部库,我不想包装http.ResponseWriter. 我必须在请求的上下文中添加响应的状态,以便以后在日志记录中使用。所以我创建了一个小帮手来同时在 ResponseWriter 和请求的上下文中写入状态。


type AppContext string

var StatusCode = AppContext("statuCode")


func WriteHeaderAndContext(w http.ResponseWriter, statusCode int, r *http.Request) {

    ctx := context.WithValue(r.Context(), StatusCode, statusCode)

    *r = *(r.WithContext(ctx))


    w.WriteHeader(statusCode)

}

在日志记录中,我将值检索为


r.Context().Value(StatusCode)

缺点发生在被叫方,看起来有点不寻常。例如


WriteHeaderAndContext(w, http.StatusCreated, r)

我们通常做的地方


w.WriteHeader(http.StatusCreated)


查看完整回答
反对 回复 2023-05-08
  • 4 回答
  • 0 关注
  • 208 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信