Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow base path changes #185

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion internal/networktest/networktest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"os"
"strings"
"time"

Expand Down Expand Up @@ -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()

Expand Down
27 changes: 20 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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{
Expand Down