refactor(handlers): simplify referer handling in API request logging

- Removed custom referer parsing logic and replaced it with direct usage of r.Referer() for cleaner code.
- Updated logging to consistently use the referer from the request object across all relevant handlers.
- Enhanced readability and maintainability by eliminating redundant code related to referer extraction.
This commit is contained in:
wood chen 2024-12-02 05:45:39 +08:00
parent d3933374af
commit b252b8313e
3 changed files with 15 additions and 39 deletions

View File

@ -5,7 +5,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url"
"random-api-go/monitoring" "random-api-go/monitoring"
"random-api-go/services" "random-api-go/services"
"random-api-go/stats" "random-api-go/stats"
@ -26,18 +25,6 @@ func InitializeHandlers(sm *stats.StatsManager) error {
func HandleAPIRequest(w http.ResponseWriter, r *http.Request) { func HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
realIP := utils.GetRealIP(r) realIP := utils.GetRealIP(r)
referer := r.Referer()
// 修改这部分,获取完整的referer信息
sourceInfo := "direct"
if 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, "/") path := strings.TrimPrefix(r.URL.Path, "/")
pathSegments := strings.Split(path, "/") pathSegments := strings.Split(path, "/")
@ -50,7 +37,7 @@ func HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusNotFound, StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000, Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
http.NotFound(w, r) http.NotFound(w, r)
return return
@ -96,16 +83,16 @@ func HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusFound, StatusCode: http.StatusFound,
Latency: float64(duration.Microseconds()) / 1000, // 转换为毫秒 Latency: float64(duration.Microseconds()) / 1000, // 转换为毫秒
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
log.Printf(" %-12s | %-15s | %-6s | %-20s | %-20s | %-50s", log.Printf(" %-12s | %-15s | %-6s | %-20s | %-20s | %-50s",
duration, // 持续时间 duration, // 持续时间
realIP, // 真实IP realIP, // 真实IP
r.Method, // HTTP方法 r.Method, // HTTP方法
r.URL.Path, // 请求路径 r.URL.Path, // 请求路径
sourceInfo, // 来源信息 r.Referer(), // 来源信息
randomURL, // 重定向URL randomURL, // 重定向URL
) )
http.Redirect(w, r, randomURL, http.StatusFound) http.Redirect(w, r, randomURL, http.StatusFound)

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"log" "log"
"net/http" "net/http"
"net/url"
"random-api-go/monitoring" "random-api-go/monitoring"
"random-api-go/router" "random-api-go/router"
"random-api-go/services" "random-api-go/services"
@ -39,17 +38,6 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
start := time.Now() start := time.Now()
realIP := utils.GetRealIP(r) 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, "/") path := strings.TrimPrefix(r.URL.Path, "/")
pathSegments := strings.Split(path, "/") pathSegments := strings.Split(path, "/")
@ -61,7 +49,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusNotFound, StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000, Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
resultChan <- result{err: fmt.Errorf("not found")} resultChan <- result{err: fmt.Errorf("not found")}
return return
@ -82,7 +70,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusNotFound, StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000, Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
resultChan <- result{err: fmt.Errorf("not found")} resultChan <- result{err: fmt.Errorf("not found")}
return return
@ -98,7 +86,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusInternalServerError, StatusCode: http.StatusInternalServerError,
Latency: float64(time.Since(start).Microseconds()) / 1000, Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
resultChan <- result{err: err} resultChan <- result{err: err}
return return
@ -112,7 +100,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusNotFound, StatusCode: http.StatusNotFound,
Latency: float64(time.Since(start).Microseconds()) / 1000, Latency: float64(time.Since(start).Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
resultChan <- result{err: fmt.Errorf("no content available")} resultChan <- result{err: fmt.Errorf("no content available")}
return return
@ -130,7 +118,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
StatusCode: http.StatusFound, StatusCode: http.StatusFound,
Latency: float64(duration.Microseconds()) / 1000, Latency: float64(duration.Microseconds()) / 1000,
IP: realIP, IP: realIP,
Referer: sourceInfo, Referer: r.Referer(),
}) })
log.Printf(" %-12s | %-15s | %-6s | %-20s | %-20s | %-50s", log.Printf(" %-12s | %-15s | %-6s | %-20s | %-20s | %-50s",
@ -138,7 +126,7 @@ func (h *Handlers) HandleAPIRequest(w http.ResponseWriter, r *http.Request) {
realIP, realIP,
r.Method, r.Method,
r.URL.Path, r.URL.Path,
sourceInfo, r.Referer(),
randomURL, randomURL,
) )

View File

@ -30,6 +30,7 @@ func MetricsMiddleware(next http.Handler) http.Handler {
StatusCode: rw.statusCode, StatusCode: rw.statusCode,
Latency: float64(duration.Microseconds()) / 1000, Latency: float64(duration.Microseconds()) / 1000,
IP: utils.GetRealIP(r), IP: utils.GetRealIP(r),
Referer: r.Referer(),
}) })
}) })
} }