24 lines
619 B
Go
24 lines
619 B
Go
|
package middleware
|
|||
|
|
|||
|
import (
|
|||
|
"log"
|
|||
|
"net/http"
|
|||
|
"time"
|
|||
|
)
|
|||
|
|
|||
|
// LoginMiddleware 登录 // 添加日志中间件到路由器 使用:r.Handle("/", LoginMiddleware(http.HandlerFunc(handler)))
|
|||
|
func LoginMiddleware(next http.Handler) http.Handler {
|
|||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|||
|
// 记录请求时间和路径
|
|||
|
start := time.Now()
|
|||
|
log.Printf("Started %s %s", r.Method, r.URL.Path)
|
|||
|
|
|||
|
// 调用下一个处理程序
|
|||
|
next.ServeHTTP(w, r)
|
|||
|
|
|||
|
// 记录请求处理时间
|
|||
|
duration := time.Since(start)
|
|||
|
log.Printf("Completed %s %s in %v", r.Method, r.URL.Path, duration)
|
|||
|
})
|
|||
|
}
|