From a5d18ef24c7d6975f041891b60a0645f1707ce08 Mon Sep 17 00:00:00 2001 From: Nicolas Ledez <247138+nledez@users.noreply.github.com> Date: Fri, 8 Jul 2022 18:44:49 +0200 Subject: [PATCH] Added `url_base` parameter to customize the root url and allow hosting with another application. (#41) * Added `url_base` parameter and code to rewrite URL * Manage redirect on stats login Co-authored-by: Nicolas Ledez --- config/config.go | 2 ++ results/stats.go | 4 ++-- settings.toml | 2 ++ web/web.go | 59 +++++++++++++++++++++++++++--------------------- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/config/config.go b/config/config.go index 384762a..b7deb43 100644 --- a/config/config.go +++ b/config/config.go @@ -8,6 +8,7 @@ import ( type Config struct { BindAddress string `mapstructure:"bind_address"` Port string `mapstructure:"listen_port"` + BaseURL string `mapstructure:"url_base"` ProxyProtocolPort string `mapstructure:"proxyprotocol_port"` ServerLat float64 `mapstructure:"server_lat"` ServerLng float64 `mapstructure:"server_lng"` @@ -39,6 +40,7 @@ var ( func init() { viper.SetDefault("listen_port", "8989") + viper.SetDefault("url_base", "") viper.SetDefault("proxyprotocol_port", "0") viper.SetDefault("download_chunks", 4) viper.SetDefault("distance_unit", "K") diff --git a/results/stats.go b/results/stats.go index 21931f8..8e2b600 100644 --- a/results/stats.go +++ b/results/stats.go @@ -66,7 +66,7 @@ func Stats(w http.ResponseWriter, r *http.Request) { session.Values["authenticated"] = false session.Options.MaxAge = -1 session.Save(r, w) - http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) + http.Redirect(w, r, conf.BaseURL+"/stats", http.StatusTemporaryRedirect) } else { data.LoggedIn = true @@ -98,7 +98,7 @@ func Stats(w http.ResponseWriter, r *http.Request) { if password == conf.StatsPassword { session.Values["authenticated"] = true session.Save(r, w) - http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) + http.Redirect(w, r, conf.BaseURL+"/stats", http.StatusTemporaryRedirect) } else { w.WriteHeader(http.StatusForbidden) } diff --git a/settings.toml b/settings.toml index 83e3256..bdfe9fb 100644 --- a/settings.toml +++ b/settings.toml @@ -2,6 +2,8 @@ bind_address="" # backend listen port listen_port=8989 +# change the base URL +# url_base="/librespeed" # proxy protocol port, use 0 to disable proxyprotocol_port=0 # Server location diff --git a/web/web.go b/web/web.go index f03d47b..b82bc07 100644 --- a/web/web.go +++ b/web/web.go @@ -66,33 +66,33 @@ func ListenAndServe(conf *config.Config) error { assetFS = justFilesFilesystem{fs: http.Dir(conf.AssetsPath), readDirBatchSize: 2} } - r.Get("/*", pages(assetFS)) - r.HandleFunc("/empty", empty) - r.HandleFunc("/backend/empty", empty) - r.Get("/garbage", garbage) - r.Get("/backend/garbage", garbage) - r.Get("/getIP", getIP) - r.Get("/backend/getIP", getIP) - r.Get("/results", results.DrawPNG) - r.Get("/results/", results.DrawPNG) - r.Get("/backend/results", results.DrawPNG) - r.Get("/backend/results/", results.DrawPNG) - r.Post("/results/telemetry", results.Record) - r.Post("/backend/results/telemetry", results.Record) - r.HandleFunc("/stats", results.Stats) - r.HandleFunc("/backend/stats", results.Stats) + r.Get(conf.BaseURL+"/*", pages(assetFS, conf.BaseURL)) + r.HandleFunc(conf.BaseURL+"/empty", empty) + r.HandleFunc(conf.BaseURL+"/backend/empty", empty) + r.Get(conf.BaseURL+"/garbage", garbage) + r.Get(conf.BaseURL+"/backend/garbage", garbage) + r.Get(conf.BaseURL+"/getIP", getIP) + r.Get(conf.BaseURL+"/backend/getIP", getIP) + r.Get(conf.BaseURL+"/results", results.DrawPNG) + r.Get(conf.BaseURL+"/results/", results.DrawPNG) + r.Get(conf.BaseURL+"/backend/results", results.DrawPNG) + r.Get(conf.BaseURL+"/backend/results/", results.DrawPNG) + r.Post(conf.BaseURL+"/results/telemetry", results.Record) + r.Post(conf.BaseURL+"/backend/results/telemetry", results.Record) + r.HandleFunc(conf.BaseURL+"/stats", results.Stats) + r.HandleFunc(conf.BaseURL+"/backend/stats", results.Stats) // PHP frontend default values compatibility - r.HandleFunc("/empty.php", empty) - r.HandleFunc("/backend/empty.php", empty) - r.Get("/garbage.php", garbage) - r.Get("/backend/garbage.php", garbage) - r.Get("/getIP.php", getIP) - r.Get("/backend/getIP.php", getIP) - r.Post("/results/telemetry.php", results.Record) - r.Post("/backend/results/telemetry.php", results.Record) - r.HandleFunc("/stats.php", results.Stats) - r.HandleFunc("/backend/stats.php", results.Stats) + r.HandleFunc(conf.BaseURL+"/empty.php", empty) + r.HandleFunc(conf.BaseURL+"/backend/empty.php", empty) + r.Get(conf.BaseURL+"/garbage.php", garbage) + r.Get(conf.BaseURL+"/backend/garbage.php", garbage) + r.Get(conf.BaseURL+"/getIP.php", getIP) + r.Get(conf.BaseURL+"/backend/getIP.php", getIP) + r.Post(conf.BaseURL+"/results/telemetry.php", results.Record) + r.Post(conf.BaseURL+"/backend/results/telemetry.php", results.Record) + r.HandleFunc(conf.BaseURL+"/stats.php", results.Stats) + r.HandleFunc(conf.BaseURL+"/backend/stats.php", results.Stats) go listenProxyProtocol(conf, r) @@ -157,8 +157,15 @@ func listenProxyProtocol(conf *config.Config, r *chi.Mux) { } } -func pages(fs http.FileSystem) http.HandlerFunc { +func pages(fs http.FileSystem, BaseURL string) http.HandlerFunc { + var removeBaseURL *regexp.Regexp + if BaseURL != "" { + removeBaseURL = regexp.MustCompile("^" + BaseURL + "/") + } fn := func(w http.ResponseWriter, r *http.Request) { + if BaseURL != "" { + r.URL.Path = removeBaseURL.ReplaceAllString(r.URL.Path, "/") + } if r.RequestURI == "/" { r.RequestURI = "/index.html" }