From bbf33f960c24c3a7c1f33042d4c6691718af20fb Mon Sep 17 00:00:00 2001 From: a-wing <1@233.email> Date: Wed, 3 Jan 2024 23:07:00 +0800 Subject: [PATCH] fix(server): reverse proxy error --- server/api/api.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/server/api/api.go b/server/api/api.go index d3c8e31..5087c13 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -15,11 +15,15 @@ import ( "github.com/redis/go-redis/v9" ) -func handler(p http.Handler, token string) func(http.ResponseWriter, *http.Request) { +func handler(p http.Handler, address, token string) func(http.ResponseWriter, *http.Request) { + u, _ := url.Parse(address) return func(w http.ResponseWriter, r *http.Request) { if token != "" { r.Header.Set("Authorization", "Bearer "+token) } + + // Reference: https://liqiang.io/post/implement-reverse-proxy-with-golang + r.Host = u.Host p.ServeHTTP(w, r) } } @@ -52,8 +56,8 @@ func NewApi(rdb *redis.Client, secret string, live777Url string, live777Token st //r.Post("/room/{roomId}/message", handle.CreateMessage) //r.Get("/room/{roomId}/message", handle.ShowMessage) - r.HandleFunc("/whip/{uuid}", handler(proxy, live777Token)) - r.HandleFunc("/whep/{uuid}", handler(proxy, live777Token)) + r.HandleFunc("/whip/{uuid}", handler(proxy, live777Url, live777Token)) + r.HandleFunc("/whep/{uuid}", handler(proxy, live777Url, live777Token)) r.Handle("/*", http.StripPrefix("/", http.FileServer(helper.NewSinglePageApp("index.html", http.FS(static.Dist))))) return r