<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、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 中間件 ### 怎樣自己寫一個中間件? 比如有以下需求 - 通過該中間件去統計請求數目、狀態和時間。 - 中間件把寫過寫回響應。 #### Server `server.go` ```go package main import ( "net/http" "strconv" "sync" "time" "github.com/labstack/echo" ) type ( Stats struct { Uptime time.Time `json:"uptime"` RequestCount uint64 `json:"requestCount"` Statuses map[string]int `json:"statuses"` mutex sync.RWMutex } ) func NewStats() *Stats { return &Stats{ Uptime: time.Now(), Statuses: make(map[string]int), } } // Process is the middleware function. func (s *Stats) Process(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { if err := next(c); err != nil { c.Error(err) } s.mutex.Lock() defer s.mutex.Unlock() s.RequestCount++ status := strconv.Itoa(c.Response().Status) s.Statuses[status]++ return nil } } // Handle is the endpoint to get stats. func (s *Stats) Handle(c echo.Context) error { s.mutex.RLock() defer s.mutex.RUnlock() return c.JSON(http.StatusOK, s) } // ServerHeader middleware adds a `Server` header to the response. func ServerHeader(next echo.HandlerFunc) echo.HandlerFunc { return func(c echo.Context) error { c.Response().Header().Set(echo.HeaderServer, "Echo/3.0") return next(c) } } func main() { e := echo.New() // Debug mode e.Debug = true //------------------- // Custom middleware //------------------- // Stats s := NewStats() e.Use(s.Process) e.GET("/stats", s.Handle) // Endpoint to get stats // Server header e.Use(ServerHeader) // Handler e.GET("/", func(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!") }) // Start server e.Logger.Fatal(e.Start(":1323")) } ``` #### 響應 *響應頭* ```sh Content-Length:122 Content-Type:application/json; charset=utf-8 Date:Thu, 14 Apr 2016 20:31:46 GMT Server:Echo/2.0 ``` *響應體* ```js { "uptime": "2016-04-14T13:28:48.486548936-07:00", "requestCount": 5, "statuses": { "200": 4, "404": 1 } } ```
                  <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>

                              哎呀哎呀视频在线观看