Skip to content

Commit

Permalink
fix: reduce configuration parameters for mounting service at non-stan…
Browse files Browse the repository at this point in the history
…dard location
  • Loading branch information
tobbee committed Aug 22, 2023
1 parent 823217c commit 560902f
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- moved list URL parameters to [livesim2 wiki](https://github.com/Dash-Industry-Forum/livesim2/wiki/URL-Parameters)
- removed `scheme` and `urlprefix` configuration. Now replaced with `host` which overrides `scheme://host` in all generated URLs

### Added

Expand Down
9 changes: 2 additions & 7 deletions cmd/livesim2/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,8 @@ type ServerConfig struct {
CertPath string `json:"certpath"`
// KeyPath is a path to a valid private TLS key
KeyPath string `json:"keypath"`
// Scheme should be http or https if set. Otherwise the scheme is auto-detected as far as possible.
Scheme string `json:"scheme"`
// If Host is set, it will be used instead of autodetected value.
// If Host is set, it will be used instead of autodetected value scheme://host.
Host string `json:"host"`
// URLPrefix is used for paths generated by livesim2, such as links in welcome page
URLPrefix string `json:"urlprefix"`
}

var DefaultConfig = ServerConfig{
Expand Down Expand Up @@ -113,8 +109,7 @@ func LoadConfig(args []string, cwd string) (*ServerConfig, error) {
f.String("certpath", k.String("certpath"), "path to TLS certificate file (for HTTPS). Use domains instead if possible")
f.String("keypath", k.String("keypath"), "path to TLS private key file (for HTTPS). Use domains instead if possible.")
f.String("scheme", k.String("scheme"), "scheme used in Location and BaseURL elements. If empty, it is attempted to be auto-detected")
f.String("host", k.String("host"), "host used in Location and BaseURL elements. If empty, it is attempted to be auto-detected")
f.String("urlprefix", k.String("urlprefix"), "prefix when paths are not mounted at root")
f.String("host", k.String("host"), "host (and possible prefix) used in MPD elements. Overrides auto-detected full scheme://host")
if err := f.Parse(args[1:]); err != nil {
return nil, fmt.Errorf("command line parse: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/livesim2/app/handler_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
// indexHandlerFunc handles access to /.
func (s *Server) indexHandlerFunc(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
err := s.htmlTemplates.ExecuteTemplate(w, "welcome.html", s.Cfg.URLPrefix)
fullHost := getSchemeAndHost(r, s.Cfg)
err := s.htmlTemplates.ExecuteTemplate(w, "welcome.html", fullHost)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
Expand Down
11 changes: 6 additions & 5 deletions cmd/livesim2/app/handler_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package app

import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"strings"
Expand All @@ -15,12 +16,12 @@ import (
"github.com/stretchr/testify/require"
)

func TestIndexPageWithPrefix(t *testing.T) {
func TestIndexPageWithHost(t *testing.T) {
cfg := ServerConfig{
VodRoot: "testdata/assets",
TimeoutS: 0,
LogFormat: logging.LogDiscard,
URLPrefix: "/livesim2",
Host: "https://example.com/subfolder",
}
_, err := logging.InitZerolog(cfg.LogLevel, cfg.LogFormat)
require.NoError(t, err)
Expand All @@ -31,10 +32,10 @@ func TestIndexPageWithPrefix(t *testing.T) {

resp, body := testFullRequest(t, ts, "GET", "/", nil)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Greater(t, strings.Index(string(body), `href="/livesim2/assets"`), 0)
require.Greater(t, strings.Index(string(body), `href="https://example.com/subfolder/assets"`), 0)
}

func TestIndexPageWithoutPrefix(t *testing.T) {
func TestIndexPageWithoutHost(t *testing.T) {
cfg := ServerConfig{
VodRoot: "testdata/assets",
TimeoutS: 0,
Expand All @@ -49,5 +50,5 @@ func TestIndexPageWithoutPrefix(t *testing.T) {

resp, body := testFullRequest(t, ts, "GET", "/", nil)
require.Equal(t, http.StatusOK, resp.StatusCode)
require.Greater(t, strings.Index(string(body), `href="/assets"`), 0)
require.Greater(t, strings.Index(string(body), fmt.Sprintf(`href="%s/assets"`, ts.URL)), 0)
}
8 changes: 5 additions & 3 deletions cmd/livesim2/app/handler_livesim.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (s *Server) livesimHandlerFunc(w http.ResponseWriter, r *http.Request) {
http.Error(w, msg, http.StatusInternalServerError)
return
}
fullHost := getSchemeAndHost(r)
fullHost := getSchemeAndHost(r, s.Cfg)

var nowMS int // Set from query string or from wall-clock
q := r.URL.Query()
Expand Down Expand Up @@ -79,7 +79,6 @@ func (s *Server) livesimHandlerFunc(w http.ResponseWriter, r *http.Request) {
switch ext {
case ".mpd":
_, mpdName := path.Split(contentPart)
cfg.SetScheme(s.Cfg.Scheme, r)
cfg.SetHost(s.Cfg.Host, r)
err := writeLiveMPD(log, w, cfg, a, mpdName, fullHost, nowMS)
if err != nil {
Expand Down Expand Up @@ -113,7 +112,10 @@ func (s *Server) livesimHandlerFunc(w http.ResponseWriter, r *http.Request) {
}
}

func getSchemeAndHost(r *http.Request) string {
func getSchemeAndHost(r *http.Request, cfg *ServerConfig) string {
if cfg.Host != "" {
return cfg.Host
}
scheme := "http"
if r.TLS != nil {
scheme = "https"
Expand Down

0 comments on commit 560902f

Please sign in to comment.