<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 使用使用中間件 Go程序的中間件是一個被廣泛探索的領域。有許多用于處理中間件的包。 本節將從頭開始創建中間件,并實現一個ApplyMiddleware函數將一堆中間件鏈接在一起。此外還會在請求上下文對象中設置值,并使用中間件檢索它們。以演示如何將中間件邏輯與處理程序分離。 ### 實踐 1. 建立 middleware.go: ``` package middleware import ( "log" "net/http" "time" ) // Middleware是所有的中間件函數都會返回的 type Middleware func(http.HandlerFunc) http.HandlerFunc // ApplyMiddleware 將應用所有中間件,最后一個參數將是用于上下文傳遞目的的外部包裝 func ApplyMiddleware(h http.HandlerFunc, middleware ...Middleware) http.HandlerFunc { applied := h for _, m := range middleware { applied = m(applied) } return applied } // Logger 記錄請求日志 這會通過SetID()傳遞id func Logger(l *log.Logger) Middleware { return func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { start := time.Now() l.Printf("started request to %s with id %s", r.URL, GetID(r.Context())) next(w, r) l.Printf("completed request to %s with id %s in %s", r.URL, GetID(r.Context()), time.Since(start)) } } } ``` 2. 建立 context.go: ``` package middleware import ( "context" "net/http" "strconv" ) // ContextID 是自定義類型 用于檢索context type ContextID int // ID是我們定義的唯一ID const ID ContextID = 0 // SetID 使用自增唯一id更新context func SetID(start int64) Middleware { return func(next http.HandlerFunc) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := context.WithValue(r.Context(), ID, strconv.FormatInt(start, 10)) start++ r = r.WithContext(ctx) next(w, r) } } } // GetID 如果設置,則從上下文中獲取ID,否則返回空字符串 func GetID(ctx context.Context) string { if val, ok := ctx.Value(ID).(string); ok { return val } return "" } ``` 3. 建立 handler.go: ``` package middleware import ( "net/http" ) func Handler(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) w.Write([]byte("success")) } ``` 4. 建立 main.go: ``` package main import ( "fmt" "log" "net/http" "os" "github.com/agtorre/go-cookbook/chapter7/middleware" ) func main() { // We apply from bottom up h := middleware.ApplyMiddleware( middleware.Handler, middleware.Logger(log.New(os.Stdout, "", 0)), middleware.SetID(100), ) http.HandleFunc("/", h) fmt.Println("Listening on port :3333") err := http.ListenAndServe(":3333", nil) panic(err) } ``` 5. 運行: ``` $ go run main.go Listening on port :3333 $curl "http://localhost:3333 success $curl "http://localhost:3333 success $curl "http://localhost:3333 success ``` 此外在運行main.go的命令行你還會看到: ``` Listening on port :3333 started request to / with id 100 completed request to / with id 100 in 52.284μs started request to / with id 101 completed request to / with id 101 in 40.273μs started request to / with id 102 ``` ### 說明 中間件可用于執行簡單操作,例如日志記錄,度量標準收集和分析。它還可用于在每個請求上動態填充變量。例如,可以用于從請求中收集X-Header以設置ID或生成ID,就像我們在示例中所做的那樣。另一個ID策略可能是為每個請求生成一個UUID,這樣我們可以輕松地將日志消息關聯在一起,并在構建響應時跟蹤請求。 使用上下文值時,考慮中間件的順序很重要。通常,最好不要讓中間件相互依賴。 例如,最好在日志記錄中間件本身中生成UUID。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看