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

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

每次有人進入我的網(wǎng)站時如何運行特定功能?

每次有人進入我的網(wǎng)站時如何運行特定功能?

Go
函數(shù)式編程 2023-02-06 19:31:36
我正在使用以下代碼:func main() {    http.Handle("/", http.FileServer(http.Dir("./web-files")))    http.HandleFunc("/auth/verify", verify)    http.HandleFunc("/auth/login", login)    http.HandleFunc("/auth/signup", signup)    http.ListenAndServe(":8080", nil)}我是新手,我想做的是每次有人進入我的網(wǎng)頁時,都會運行一個名為updateCookiesruns 的函數(shù)。我嘗試使用http.HandleFunc("/", updateCookies)但沒有用,因為我已經(jīng)使用過http.Handle("/", http.FileServer(http.Dir("./web-files")))謝謝
查看完整描述

2 回答

?
鴻蒙傳說

TA貢獻1865條經(jīng)驗 獲得超7個贊

您的應(yīng)用程序的處理程序是http.DefaultServeMux。用另一個執(zhí)行代碼的處理程序包裝該處理程序:


// wrap returns an http.Handler that wraps another http.Handler

func wrap(h http.Handler) http.HandlerFunc {

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

        // Before hook.

        updateCookies(w, r)


        h.ServeHTTP(w, r)  // call the wrapped handler


        // After hook.

        // Nothing for now.

    }

}



func main() {

    http.Handle("/", http.FileServer(http.Dir("./web-files")))

    http.HandleFunc("/auth/verify", verify)

    http.HandleFunc("/auth/login", login)

        http.HandleFunc("/auth/signup", signup)

    http.ListenAndServe(":8080", wrap(http.DefaultServeMux))

}

我編寫代碼來包裝任意處理程序,因為可以輕松過渡到使用您自己的http.ServeMux:


func main() {

    mux := http.NewServeMux()

    mux.Handle("/", http.FileServer(http.Dir("./web-files")))

    mux.HandleFunc("/auth/verify", verify)

    mux.HandleFunc("/auth/login", login)

    mux.HandleFunc("/auth/signup", signup)

    http.ListenAndServe(":8080", wrap(mux))

}

任何包都可以在 http.DefaultServeMux 中注冊一個處理程序。創(chuàng)建自己的多路復(fù)用器可確保您完全控制應(yīng)用程序中運行的處理程序。


查看完整回答
反對 回復(fù) 2023-02-06
?
冉冉說

TA貢獻1877條經(jīng)驗 獲得超1個贊

http.Handle, http.HandleFunc, 和http.ListenAndServe(nil作為第二個參數(shù)),http.DefaultServeMux用于將請求路由到它們各自的處理程序。


http.ListenAndServe僅當(dāng)傳遞給它的第二個參數(shù)是時才使用默認 mux nil。如果提供了非 nil 參數(shù),那么它將使用該參數(shù)而不是默認的 mux 來處理傳入的請求。


鑒于http.ListenAndServe第二個參數(shù)的類型是http.Handler接口,您可以簡單地將updateCookiestohttp.ListenAndServe作為第二個參數(shù)傳遞(盡管您必須將其顯式轉(zhuǎn)換為http.HandlerFunc),然后修改updateCookies為,最后將 thew和 the傳遞r給默認多路復(fù)用器。


func updateCookies(w http.ResponseWriter, r *http.Request) {

    // ... [your original cookie code] ...

    http.DefaultServeMux.ServeHTTP(w, r)

}


// ...


http.ListenAndServe(":8080", http.HandlerFunc(updateCookies))


查看完整回答
反對 回復(fù) 2023-02-06
  • 2 回答
  • 0 關(guān)注
  • 95 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

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

公眾號

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