mirror of
https://github.com/woodchen-ink/random-api-go.git
synced 2025-07-18 05:42:01 +08:00
- Simplified MetricsMiddleware by removing unnecessary comments and focusing on essential request logging. - Updated RequestLog structure to exclude the Referer field from JSON serialization, enhancing data privacy. - Removed top referers tracking and associated HTML/CSS elements to declutter the user interface and improve performance. - Cleaned up CSS styles related to referers for a more streamlined design.
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
package middleware
|
|
|
|
import (
|
|
"net/http"
|
|
"random-api-go/monitoring"
|
|
"random-api-go/utils"
|
|
"time"
|
|
|
|
"golang.org/x/time/rate"
|
|
)
|
|
|
|
var limiter = rate.NewLimiter(rate.Limit(1000), 100)
|
|
|
|
func MetricsMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
|
|
rw := &responseWriter{
|
|
ResponseWriter: w,
|
|
statusCode: http.StatusOK,
|
|
}
|
|
|
|
next.ServeHTTP(rw, r)
|
|
|
|
duration := time.Since(start)
|
|
monitoring.LogRequest(monitoring.RequestLog{
|
|
Time: time.Now().Unix(),
|
|
Path: r.URL.Path,
|
|
Method: r.Method,
|
|
StatusCode: rw.statusCode,
|
|
Latency: float64(duration.Microseconds()) / 1000,
|
|
IP: utils.GetRealIP(r),
|
|
})
|
|
})
|
|
}
|
|
|
|
type responseWriter struct {
|
|
http.ResponseWriter
|
|
statusCode int
|
|
}
|
|
|
|
func (rw *responseWriter) WriteHeader(statusCode int) {
|
|
rw.statusCode = statusCode
|
|
rw.ResponseWriter.WriteHeader(statusCode)
|
|
}
|
|
|
|
func RateLimiter(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if !limiter.Allow() {
|
|
http.Error(w, "Too many requests", http.StatusTooManyRequests)
|
|
return
|
|
}
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|