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
This commit is contained in:
wood chen 2024-10-30 09:23:08 +08:00
parent 62b82bfb5a
commit 110f1efe83
2 changed files with 8 additions and 35 deletions

View File

@ -63,9 +63,6 @@ jobs:
username: woodchen username: woodchen
password: ${{ secrets.DOCKERHUB_TOKEN }} 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 - name: Create Docker build context
run: | run: |
@ -82,7 +79,7 @@ jobs:
push: true push: true
tags: | tags: |
woodchen/proxy-go:latest woodchen/proxy-go:latest
woodchen/proxy-go:${{ steps.date.outputs.date }}
- name: Execute deployment commands - name: Execute deployment commands
uses: appleboy/ssh-action@master uses: appleboy/ssh-action@master
with: with:

View File

@ -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{ client := &http.Client{}
Transport: &http.Transport{
// 禁用自动处理压缩,让我们能完整传递压缩内容
DisableCompression: true,
},
}
resp, err := client.Do(proxyReq) resp, err := client.Do(proxyReq)
if err != nil { if err != nil {
http.Error(w, "Error forwarding request", http.StatusBadGateway) 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() defer resp.Body.Close()
// 复制所有响应头 // 复制响应 header
for k, vv := range resp.Header { copyHeader(w.Header(), 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)
}
// 设置响应状态码 // 设置响应状态码
w.WriteHeader(resp.StatusCode) w.WriteHeader(resp.StatusCode)
@ -161,18 +138,17 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
} }
} else { } else {
// 如果不支持 Flusher使用普通的 io.Copy
bytesCopied, err = io.Copy(w, resp.Body) bytesCopied, err = io.Copy(w, resp.Body)
if err != nil { if err != nil {
log.Printf("Error copying response: %v", err) 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, getClientIP(r), r.Method, r.URL.Path, targetURL,
resp.StatusCode, bytesCopied, time.Since(startTime), resp.StatusCode, bytesCopied, time.Since(startTime))
resp.Header.Get("Content-Type"),
resp.Header.Get("Content-Encoding"))
} }
func copyHeader(dst, src http.Header) { func copyHeader(dst, src http.Header) {