diff --git a/internal/errors/errors.go b/internal/errors/errors.go deleted file mode 100644 index 15adeab..0000000 --- a/internal/errors/errors.go +++ /dev/null @@ -1,22 +0,0 @@ -package errors - -type ErrorCode int - -const ( - ErrInvalidConfig ErrorCode = iota + 1 - ErrRateLimit - ErrMetricsCollection -) - -type MetricsError struct { - Code ErrorCode - Message string - Err error -} - -func (e *MetricsError) Error() string { - if e.Err != nil { - return e.Message + ": " + e.Err.Error() - } - return e.Message -} diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index 78382d7..f93e334 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -195,7 +195,7 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } // 使用统一的路由选择逻辑 - targetBase := utils.GetTargetURL(h.client, r, pathConfig, decodedPath) + targetBase, usedAltTarget := utils.GetTargetURL(h.client, r, pathConfig, decodedPath) // 重新编码路径,保留 '/' parts := strings.Split(decodedPath, "/") @@ -277,6 +277,12 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Encoding", item.ContentEncoding) } w.Header().Set("Proxy-Go-Cache", "HIT") + + // 如果使用了扩展名映射的备用目标,添加标记响应头 + if usedAltTarget { + w.Header().Set("Proxy-Go-AltTarget", "true") + } + if notModified { w.WriteHeader(http.StatusNotModified) return @@ -305,6 +311,11 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { copyHeader(w.Header(), resp.Header) w.Header().Set("Proxy-Go-Cache", "MISS") + // 如果使用了扩展名映射的备用目标,添加标记响应头 + if usedAltTarget { + w.Header().Set("Proxy-Go-AltTarget", "true") + } + // 设置响应状态码 w.WriteHeader(resp.StatusCode) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index 5725c7a..c4569cb 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -183,9 +183,10 @@ func GetFileSize(client *http.Client, url string) (int64, error) { } // GetTargetURL 根据路径和配置决定目标URL -func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathConfig, path string) string { +func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathConfig, path string) (string, bool) { // 默认使用默认目标 targetBase := pathConfig.DefaultTarget + usedAltTarget := false // 如果配置了扩展名映射 if pathConfig.ExtensionMap != nil { @@ -198,7 +199,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo contentLength, err := GetFileSize(client, targetBase+path) if err != nil { log.Printf("[Route] %s -> %s (error getting size: %v)", path, targetBase, err) - return targetBase + return targetBase, false } // 如果没有设置最小阈值,使用默认值 500KB @@ -244,7 +245,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo log.Printf("[Route] %s -> %s (size: %s > %s and <= %s)", path, altTarget, FormatBytes(contentLength), FormatBytes(minThreshold), FormatBytes(maxThreshold)) - return altTarget + return altTarget, true } log.Printf("[Route] %s -> %s (fallback: alternative target not accessible)", path, targetBase) @@ -269,7 +270,7 @@ func GetTargetURL(client *http.Client, r *http.Request, pathConfig config.PathCo log.Printf("[Route] %s -> %s (no extension map)", path, targetBase) } - return targetBase + return targetBase, usedAltTarget } // isTargetAccessible 检查目标URL是否可访问