random-api-go/handlers/handlers.go
wood chen 1b2fd3ea85 feat(handlers, monitoring, public): enhance API request handling and improve metrics logging
- Implemented detailed logging for API requests, including real IP, referer, and latency metrics.
- Added dynamic routing for handling requests to /pic/ and /video/ endpoints.
- Enhanced error handling for CSV content fetching and improved response messages.
- Updated the HTML and JavaScript to load and display statistics more efficiently, including filtering based on endpoint configuration.
- Introduced new metrics collection logic to only log relevant API requests, optimizing performance and clarity.
2024-11-30 23:52:31 +08:00

171 lines
4.3 KiB
Go

package handlers
import (
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"random-api-go/monitoring"
"random-api-go/router"
"random-api-go/services"
"random-api-go/stats"
"random-api-go/utils"
"strings"
"time"
)
type Router interface {
HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
}
type Handlers struct {
Stats *stats.StatsManager
}
func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
start := time.Now()
realIP := utils.GetRealIP(r)
// 获取并处理 referer
sourceInfo := "direct"
if referer := r.Referer(); referer != "" {
if parsedURL, err := url.Parse(referer); err == nil {
sourceInfo = parsedURL.Host + parsedURL.Path
if parsedURL.RawQuery != "" {
sourceInfo += "?" + parsedURL.RawQuery
}
}
}
path := strings.TrimPrefix(r.URL.Path, "/")
pathSegments := strings.Split(path, "/")
if len(pathSegments) < 2 {
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP,
Referer: sourceInfo,
})
http.NotFound(w, r)
return
}
prefix := pathSegments[0]
suffix := pathSegments[1]
services.Mu.RLock()
csvPath, ok := services.CSVPathsCache[prefix][suffix]
services.Mu.RUnlock()
if !ok {
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP,
Referer: sourceInfo,
})
http.NotFound(w, r)
return
}
selector, err := services.GetCSVContent(csvPath)
if err != nil {
log.Printf("Error fetching CSV content: %v", err)
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: http.StatusInternalServerError,
Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP,
Referer: sourceInfo,
})
http.Error(w, "Failed to fetch content", http.StatusInternalServerError)
return
}
if len(selector.URLs) == 0 {
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP,
Referer: sourceInfo,
})
http.Error(w, "No content available", http.StatusNotFound)
return
}
randomURL := selector.GetRandomURL()
endpoint := fmt.Sprintf("%s/%s", prefix, suffix)
h.Stats.IncrementCalls(endpoint)
duration := time.Since(start)
monitoring.LogRequest(monitoring.RequestLog{
Time: time.Now(),
Path: r.URL.Path,
Method: r.Method,
StatusCode: http.StatusFound,
Latency: float64(duration.Microseconds()) / 1000,
IP: realIP,
Referer: sourceInfo,
})
log.Printf(" %-12s | %-15s | %-6s | %-20s | %-20s | %-50s",
duration,
realIP,
r.Method,
r.URL.Path,
sourceInfo,
randomURL,
)
http.Redirect(w, r, randomURL, http.StatusFound)
}
func (h *Handlers) HandleStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
stats := h.Stats.GetStats()
if err := json.NewEncoder(w).Encode(stats); err != nil {
http.Error(w, "Error encoding stats", http.StatusInternalServerError)
log.Printf("Error encoding stats: %v", err)
}
}
func (h *Handlers) HandleURLStats(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
services.Mu.RLock()
response := map[string]interface{}{
"paths": services.CSVPathsCache,
}
services.Mu.RUnlock()
json.NewEncoder(w).Encode(response)
}
func (h *Handlers) HandleMetrics(w http.ResponseWriter, r *http.Request) {
metrics := monitoring.CollectMetrics()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(metrics)
}
func (h *Handlers) Setup(r *router.Router) {
// 动态路由处理
r.HandleFunc("/pic/", h.HandleAPIRequest)
r.HandleFunc("/video/", h.HandleAPIRequest)
// API 统计和监控
r.HandleFunc("/stats", h.HandleStats)
r.HandleFunc("/urlstats", h.HandleURLStats)
r.HandleFunc("/metrics", h.HandleMetrics)
}