From 110f1efe8372d6454177362fe7b0d8294c7f97b8 Mon Sep 17 00:00:00 2001 From: wood chen Date: Wed, 30 Oct 2024 09:23:08 +0800 Subject: [PATCH] chore(workflow): remove date step from docker build and update proxy.go Remove unnecessary date step from docker build workflow and simplify proxy.go code --- .github/workflows/docker-build.yml | 5 +--- internal/handler/proxy.go | 38 ++++++------------------------ 2 files changed, 8 insertions(+), 35 deletions(-) diff --git a/.github/workflows/docker-build.yml b/.github/workflows/docker-build.yml index 7ac7280..d784adb 100644 --- a/.github/workflows/docker-build.yml +++ b/.github/workflows/docker-build.yml @@ -63,9 +63,6 @@ jobs: username: woodchen password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Get current date - id: date - run: echo "::set-output name=date::$(date +'%Y%m%d')" - name: Create Docker build context run: | @@ -82,7 +79,7 @@ jobs: push: true tags: | woodchen/proxy-go:latest - woodchen/proxy-go:${{ steps.date.outputs.date }} + - name: Execute deployment commands uses: appleboy/ssh-action@master with: diff --git a/internal/handler/proxy.go b/internal/handler/proxy.go index d5d2c74..ec4b949 100644 --- a/internal/handler/proxy.go +++ b/internal/handler/proxy.go @@ -97,16 +97,8 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } - // 设置接受压缩内容的header - proxyReq.Header.Set("Accept-Encoding", r.Header.Get("Accept-Encoding")) - // 发送代理请求 - client := &http.Client{ - Transport: &http.Transport{ - // 禁用自动处理压缩,让我们能完整传递压缩内容 - DisableCompression: true, - }, - } + client := &http.Client{} resp, err := client.Do(proxyReq) if err != nil { http.Error(w, "Error forwarding request", http.StatusBadGateway) @@ -116,23 +108,8 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } defer resp.Body.Close() - // 复制所有响应头 - for k, vv := range resp.Header { - for _, v := range vv { - w.Header().Add(k, v) - } - } - - // 确保Content-Type正确设置 - contentType := resp.Header.Get("Content-Type") - if contentType != "" { - w.Header().Set("Content-Type", contentType) - } - - // 保持原始的Content-Encoding - if encoding := resp.Header.Get("Content-Encoding"); encoding != "" { - w.Header().Set("Content-Encoding", encoding) - } + // 复制响应 header + copyHeader(w.Header(), resp.Header) // 设置响应状态码 w.WriteHeader(resp.StatusCode) @@ -161,18 +138,17 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } } else { + // 如果不支持 Flusher,使用普通的 io.Copy bytesCopied, err = io.Copy(w, resp.Body) if err != nil { log.Printf("Error copying response: %v", err) } } - // 记录访问日志,添加内容类型信息 - log.Printf("[%s] %s %s -> %s -> %d (%d bytes) [%v] Content-Type: %s, Encoding: %s", + // 记录访问日志 + log.Printf("[%s] %s %s -> %s -> %d (%d bytes) [%v]", getClientIP(r), r.Method, r.URL.Path, targetURL, - resp.StatusCode, bytesCopied, time.Since(startTime), - resp.Header.Get("Content-Type"), - resp.Header.Get("Content-Encoding")) + resp.StatusCode, bytesCopied, time.Since(startTime)) } func copyHeader(dst, src http.Header) {