wood chen e70ca4cf52 feat(docker, config, api): update docker-compose for logging, enhance app structure, and add system metrics display
- Updated docker-compose.yml to mount logs directory.
- Changed BASE_URL environment variable to point to the new API endpoint.
- Refactored main.go to implement a structured App type, improving initialization and graceful shutdown.
- Enhanced config management with JSON loading and environment variable support.
- Added monitoring capabilities in api_handler for logging request metrics.
- Introduced new metrics display in index.html with corresponding CSS styles for better visualization.
2024-11-30 23:23:58 +08:00

46 lines
981 B
Go

package middleware
import (
"net/http"
"random-api-go/monitoring"
"random-api-go/utils"
"time"
)
func MetricsMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
start := time.Now()
// 创建自定义的ResponseWriter来捕获状态码
rw := &responseWriter{
ResponseWriter: w,
statusCode: http.StatusOK,
}
// 处理请求
next.ServeHTTP(rw, r)
// 记录请求数据
duration := time.Since(start)
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: rw.statusCode,
Latency: float64(duration.Microseconds()) / 1000,
IP: utils.GetRealIP(r),
Referer: r.Referer(),
})
})
}
type responseWriter struct {
http.ResponseWriter
statusCode int
}
func (rw *responseWriter) WriteHeader(statusCode int) {
rw.statusCode = statusCode
rw.ResponseWriter.WriteHeader(statusCode)
}