loggingMiddleware 日志中间件
(next http.Handler)
| 112 | |
| 113 | // loggingMiddleware 日志中间件 |
| 114 | func loggingMiddleware(next http.Handler) http.Handler { |
| 115 | return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 116 | start := time.Now() |
| 117 | |
| 118 | // 包装 ResponseWriter 以获取状态码 |
| 119 | wrapped := &responseWriter{ResponseWriter: w, statusCode: http.StatusOK} |
| 120 | |
| 121 | next.ServeHTTP(wrapped, r) |
| 122 | |
| 123 | log.Infof("%s %s %d %s", r.Method, r.URL.Path, wrapped.statusCode, time.Since(start)) |
| 124 | }) |
| 125 | } |
| 126 | |
| 127 | // recoveryMiddleware 恢复中间件 |
| 128 | func recoveryMiddleware(next http.Handler) http.Handler { |