第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何在 http.TimeoutHandler 中使用 gorilla mux

如何在 http.TimeoutHandler 中使用 gorilla mux

Go
森林海 2021-06-28 15:31:07
在用 go 編寫(xiě)的 HTTP 服務(wù)器中,我使用gorilla/mux進(jìn)行路由,我想使用http.TimeoutHandler(和/或其他“中間件”),但我不明白我可以在哪里安裝它們。要說(shuō)清楚:我創(chuàng)建了一個(gè)新的路由器 gorillaMux := mux.NewRouter()通過(guò)調(diào)用添加我的路線(xiàn) gorillaMux.HandleFunc("/", rootHandler)我通過(guò)server := &http.Server{Addr:":1234"}和創(chuàng)建服務(wù)器server.ListenAndServe()我可以在哪里插入http.TimeoutHandler或任何其他中間件?
查看完整描述

2 回答

?
慕森卡

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

此解決方案不回答使用 TimeoutHandler。我在這里發(fā)布了這個(gè)以防有人想要對(duì)他們的服務(wù)器超時(shí)進(jìn)行細(xì)粒度控制。如果需要,這種替代方法將允許定義 IdleTimeout 和 RequestHeaderTimeout。在這個(gè)例子中,我同時(shí)使用了 gorilla/mux 和 gorilla/handlers。希望能幫助到你。


// ReadTimeout is a timing constraint on the client http request imposed by the server from the moment

// of initial connection up to the time the entire request body has been read.

// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]


// WriteTimeout is a time limit imposed on client connecting to the server via http from the

// time the server has completed reading the request header up to the time it has finished writing the response.

// [Accept] --> [TLS Handshake] --> [Request Headers] --> [Request Body] --> [Response]


func main() {

    mux := router.EpicMux()


    srv := &http.Server{

        Handler:      handlers.LoggingHandler(os.Stdout, mux),

        Addr:         "localhost:8080",

        WriteTimeout: 15 * time.Second,

        ReadTimeout:  15 * time.Second,

    }


    log.Fatal(srv.ListenAndServe())

}


func EpicMux() http.Handler {

    r := mux.NewRouter()

    r.HandleFunc("/", BaseURLRouter).Methods(http.MethodGet)

    // create the subroutes for v1 and v2

    v1 := r.PathPrefix("api/v1").Subrouter()

    // register handlers to appropriate version

    v1.HandleFunc("/person", PersonHandlerV1).Methods(http.MethodPost)


    v2 := r.PathPrefix("api/v2").Subrouter()

    v2.HandleFunc("/person",    PersonHandlerV2).Methods(http.MethodPost)

    return r

}


查看完整回答
反對(duì) 回復(fù) 2021-07-05
  • 2 回答
  • 0 關(guān)注
  • 263 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)