From 4546f3b8fb07bc724add364fdad8d74fe798587d Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Tue, 13 Oct 2020 17:12:19 +0200 Subject: [PATCH] Map domain to streamid instead of considering that the domain is the streamid: no need to pass a YAML key that contains dots --- docs/ghostream.example.yml | 10 ++++++---- internal/config/config.go | 2 +- web/handler.go | 40 +++++++++++++++++++------------------- web/template/_base.html | 2 +- web/web.go | 2 +- 5 files changed, 29 insertions(+), 27 deletions(-) diff --git a/docs/ghostream.example.yml b/docs/ghostream.example.yml index 98b8554..4856b96 100644 --- a/docs/ghostream.example.yml +++ b/docs/ghostream.example.yml @@ -73,7 +73,7 @@ srt: telnet: # By default, this easter egg is disabled. # You must enable it to use it. - #enable: false + #enabled: false #listenAddress: :8023 @@ -115,11 +115,13 @@ web: # #name: Ghostream - # Use the domain name as the stream name - # e.g., on http://example.com:8080/ the stream served will be "example.com" + # Use the domain name as the stream name for some hosts + # e.g., on http://stream.example.com:8080/, if the domain stream.example.com is mapped to "example", + # the stream served will be "example". # This implies that your domain will be able to serve only one stream. # - #oneStreamPerDomain: false + #mapDomainToStream: + # stream.example.com: example # Stream player poster # Shown when stream is loading or inactive. diff --git a/internal/config/config.go b/internal/config/config.go index a6bd1ce..1ff252e 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -71,7 +71,7 @@ func New() *Config { Hostname: "localhost", ListenAddress: ":8080", Name: "Ghostream", - OneStreamPerDomain: false, + MapDomainToStream: make(map[string]string), PlayerPoster: "/static/img/no_stream.svg", ViewersCounterRefreshPeriod: 20000, }, diff --git a/web/handler.go b/web/handler.go index b2e50ff..9549032 100644 --- a/web/handler.go +++ b/web/handler.go @@ -24,17 +24,17 @@ func viewerPostHandler(w http.ResponseWriter, r *http.Request) { // Get stream ID from URL, or from domain name path := r.URL.Path[1:] - if cfg.OneStreamPerDomain { - host := r.Host - if strings.Contains(host, ":") { - realHost, _, err := net.SplitHostPort(r.Host) - if err != nil { - log.Printf("Failed to split host and port from %s", r.Host) - return - } - host = realHost + host := r.Host + if strings.Contains(host, ":") { + realHost, _, err := net.SplitHostPort(r.Host) + if err != nil { + log.Printf("Failed to split host and port from %s", r.Host) + return } - path = host + host = realHost + } + if streamID, ok := cfg.MapDomainToStream[host]; ok { + path = streamID } // Decode client description @@ -73,20 +73,20 @@ func viewerPostHandler(w http.ResponseWriter, r *http.Request) { func viewerGetHandler(w http.ResponseWriter, r *http.Request) { // Get stream ID from URL, or from domain name path := r.URL.Path[1:] - if cfg.OneStreamPerDomain { - host := r.Host - if strings.Contains(host, ":") { - realHost, _, err := net.SplitHostPort(r.Host) - if err != nil { - log.Printf("Failed to split host and port from %s", r.Host) - return - } - host = realHost + host := r.Host + if strings.Contains(host, ":") { + realHost, _, err := net.SplitHostPort(r.Host) + if err != nil { + log.Printf("Failed to split host and port from %s", r.Host) + return } + host = realHost + } + if streamID, ok := cfg.MapDomainToStream[host]; ok { if path == "about" { path = "" } else { - path = host + path = streamID } } diff --git a/web/template/_base.html b/web/template/_base.html index 78fc1be..35e4de6 100644 --- a/web/template/_base.html +++ b/web/template/_base.html @@ -4,7 +4,7 @@ - {{if .Path}}{{if not .Cfg.OneStreamPerDomain}}{{.Path}} - {{end}}{{end}}{{.Cfg.Name}} + {{if .Path}}{{.Path}} - {{end}}{{.Cfg.Name}} {{if .Cfg.CustomCSS}}{{end}} diff --git a/web/web.go b/web/web.go index ef25844..5f9161c 100644 --- a/web/web.go +++ b/web/web.go @@ -22,7 +22,7 @@ type Options struct { Hostname string ListenAddress string Name string - OneStreamPerDomain bool + MapDomainToStream map[string]string PlayerPoster string SRTServerPort string STUNServers []string