亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 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 關注
  • 329 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號