From 7f3bccfd77ded2b75ec6adbec5aa683570604df1 Mon Sep 17 00:00:00 2001 From: micsthepick <11528421+micsthepick@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:31:14 +1000 Subject: [PATCH] Allow base path changes --- internal/networktest/networktest.go | 10 +++++++++- main.go | 27 ++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/internal/networktest/networktest.go b/internal/networktest/networktest.go index a8f12bf..ba3f415 100644 --- a/internal/networktest/networktest.go +++ b/internal/networktest/networktest.go @@ -8,6 +8,7 @@ import ( "net" "net/http" "net/http/httptest" + "os" "strings" "time" @@ -57,7 +58,14 @@ func Run(whepHandler func(res http.ResponseWriter, req *http.Request)) error { } }) - req := httptest.NewRequest("POST", "/api/whip", strings.NewReader(offer.SDP)) + + // Retrieve the base path from the environment variable + basePath := os.Getenv("BASE_PATH") + if basePath == "" { + basePath = "/" // Default to root if not specified + } + + req := httptest.NewRequest("POST", basePath+"/api/whip", strings.NewReader(offer.SDP)) req.Header["Authorization"] = []string{"Bearer networktest"} recorder := httptest.NewRecorder() diff --git a/main.go b/main.go index f13cb45..4d782b1 100644 --- a/main.go +++ b/main.go @@ -116,7 +116,13 @@ func whepHandler(res http.ResponseWriter, req *http.Request) { return } - apiPath := req.Host + strings.TrimSuffix(req.URL.RequestURI(), "whep") + // Prefer X-Forwarded-For if available + host := req.Header.Get("X-Forwarded-Host") + if host == "" { + host = req.Host + } + + apiPath := host + strings.TrimSuffix(req.URL.RequestURI(), "whep") res.Header().Add("Link", `<`+apiPath+"sse/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:server-sent-events"; events="layers"`) res.Header().Add("Link", `<`+apiPath+"layer/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:layer"`) res.Header().Add("Location", "/api/whep") @@ -268,17 +274,24 @@ func main() { } + // Retrieve the base path from the environment variable + basePath := os.Getenv("BASE_PATH") + if basePath == "" { + basePath = "/" // Default to root if not specified + } + mux := http.NewServeMux() if os.Getenv("DISABLE_FRONTEND") == "" { - mux.Handle("/", indexHTMLWhenNotFound(http.Dir("./web/build"))) + // Strip the basePath before serving files from ./web/build + mux.Handle(basePath+"/", http.StripPrefix(basePath+"/", indexHTMLWhenNotFound(http.Dir("./web/build")))) } - mux.HandleFunc("/api/whip", corsHandler(whipHandler)) - mux.HandleFunc("/api/whep", corsHandler(whepHandler)) - mux.HandleFunc("/api/sse/", corsHandler(whepServerSentEventsHandler)) - mux.HandleFunc("/api/layer/", corsHandler(whepLayerHandler)) + mux.HandleFunc(basePath+"/api/whip", corsHandler(whipHandler)) + mux.HandleFunc(basePath+"/api/whep", corsHandler(whepHandler)) + mux.HandleFunc(basePath+"/api/sse/", corsHandler(whepServerSentEventsHandler)) + mux.HandleFunc(basePath+"/api/layer/", corsHandler(whepLayerHandler)) if os.Getenv("DISABLE_STATUS") == "" { - mux.HandleFunc("/api/status", corsHandler(statusHandler)) + mux.HandleFunc(basePath+"/api/status", corsHandler(statusHandler)) } server := &http.Server{