📝 Add logging statements for better debugging in proxy handling logic

This commit is contained in:
2026-04-19 11:23:13 +08:00
parent 9d8bfefea9
commit 70b90216d6
+10 -1
View File
@@ -6,6 +6,7 @@ import (
"crypto/tls"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
@@ -70,6 +71,7 @@ func main() {
func handleProxy(cfg Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println("Handle proxy: ", r.URL)
if r.URL.Path == "/health" {
w.WriteHeader(http.StatusOK)
return
@@ -88,6 +90,7 @@ func handleProxy(cfg Config) http.HandlerFunc {
resp, err := client.Do(proxyReq)
if err != nil {
log.Println("Upstream error: ", err)
http.Error(w, fmt.Sprintf("upstream error: %v", err), http.StatusBadGateway)
return
}
@@ -98,7 +101,10 @@ func handleProxy(cfg Config) http.HandlerFunc {
}
w.WriteHeader(resp.StatusCode)
if !isStreaming(resp) {
isStreamingRequest := isStreaming(resp)
log.Println("Request streaming: ", isStreamingRequest)
if !isStreamingRequest {
io.Copy(w, resp.Body)
return
}
@@ -119,12 +125,14 @@ func cloneRequest(r *http.Request, upstreamURL string) *http.Request {
}
proxyReq.Host = upstream.Host
log.Println("Upstream proxy: ", proxyReq.URL)
if val := r.Header.Get("Content-Type"); val != "" {
proxyReq.Header.Set("Content-Type", val)
}
proxyReq.Header.Del("Host")
proxyReq.Header.Del("Authorization")
proxyReq.RequestURI = ""
return proxyReq
}
@@ -158,6 +166,7 @@ func handleStream(w io.Writer, body io.Reader, cfg Config) {
}
func processChunk(w io.Writer, line string, cfg Config) {
log.Println("Process chunk: ", line)
if strings.HasPrefix(line, "data: ") {
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {