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 <github.public@ledez.net>
This commit is contained in:
Nicolas Ledez 2022-07-08 18:44:49 +02:00 committed by GitHub
parent f09f2df2e3
commit a5d18ef24c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 28 deletions

View File

@ -8,6 +8,7 @@ import (
type Config struct { type Config struct {
BindAddress string `mapstructure:"bind_address"` BindAddress string `mapstructure:"bind_address"`
Port string `mapstructure:"listen_port"` Port string `mapstructure:"listen_port"`
BaseURL string `mapstructure:"url_base"`
ProxyProtocolPort string `mapstructure:"proxyprotocol_port"` ProxyProtocolPort string `mapstructure:"proxyprotocol_port"`
ServerLat float64 `mapstructure:"server_lat"` ServerLat float64 `mapstructure:"server_lat"`
ServerLng float64 `mapstructure:"server_lng"` ServerLng float64 `mapstructure:"server_lng"`
@ -39,6 +40,7 @@ var (
func init() { func init() {
viper.SetDefault("listen_port", "8989") viper.SetDefault("listen_port", "8989")
viper.SetDefault("url_base", "")
viper.SetDefault("proxyprotocol_port", "0") viper.SetDefault("proxyprotocol_port", "0")
viper.SetDefault("download_chunks", 4) viper.SetDefault("download_chunks", 4)
viper.SetDefault("distance_unit", "K") viper.SetDefault("distance_unit", "K")

View File

@ -66,7 +66,7 @@ func Stats(w http.ResponseWriter, r *http.Request) {
session.Values["authenticated"] = false session.Values["authenticated"] = false
session.Options.MaxAge = -1 session.Options.MaxAge = -1
session.Save(r, w) session.Save(r, w)
http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) http.Redirect(w, r, conf.BaseURL+"/stats", http.StatusTemporaryRedirect)
} else { } else {
data.LoggedIn = true data.LoggedIn = true
@ -98,7 +98,7 @@ func Stats(w http.ResponseWriter, r *http.Request) {
if password == conf.StatsPassword { if password == conf.StatsPassword {
session.Values["authenticated"] = true session.Values["authenticated"] = true
session.Save(r, w) session.Save(r, w)
http.Redirect(w, r, "/stats", http.StatusTemporaryRedirect) http.Redirect(w, r, conf.BaseURL+"/stats", http.StatusTemporaryRedirect)
} else { } else {
w.WriteHeader(http.StatusForbidden) w.WriteHeader(http.StatusForbidden)
} }

View File

@ -2,6 +2,8 @@
bind_address="" bind_address=""
# backend listen port # backend listen port
listen_port=8989 listen_port=8989
# change the base URL
# url_base="/librespeed"
# proxy protocol port, use 0 to disable # proxy protocol port, use 0 to disable
proxyprotocol_port=0 proxyprotocol_port=0
# Server location # Server location

View File

@ -66,33 +66,33 @@ func ListenAndServe(conf *config.Config) error {
assetFS = justFilesFilesystem{fs: http.Dir(conf.AssetsPath), readDirBatchSize: 2} assetFS = justFilesFilesystem{fs: http.Dir(conf.AssetsPath), readDirBatchSize: 2}
} }
r.Get("/*", pages(assetFS)) r.Get(conf.BaseURL+"/*", pages(assetFS, conf.BaseURL))
r.HandleFunc("/empty", empty) r.HandleFunc(conf.BaseURL+"/empty", empty)
r.HandleFunc("/backend/empty", empty) r.HandleFunc(conf.BaseURL+"/backend/empty", empty)
r.Get("/garbage", garbage) r.Get(conf.BaseURL+"/garbage", garbage)
r.Get("/backend/garbage", garbage) r.Get(conf.BaseURL+"/backend/garbage", garbage)
r.Get("/getIP", getIP) r.Get(conf.BaseURL+"/getIP", getIP)
r.Get("/backend/getIP", getIP) r.Get(conf.BaseURL+"/backend/getIP", getIP)
r.Get("/results", results.DrawPNG) r.Get(conf.BaseURL+"/results", results.DrawPNG)
r.Get("/results/", results.DrawPNG) r.Get(conf.BaseURL+"/results/", results.DrawPNG)
r.Get("/backend/results", results.DrawPNG) r.Get(conf.BaseURL+"/backend/results", results.DrawPNG)
r.Get("/backend/results/", results.DrawPNG) r.Get(conf.BaseURL+"/backend/results/", results.DrawPNG)
r.Post("/results/telemetry", results.Record) r.Post(conf.BaseURL+"/results/telemetry", results.Record)
r.Post("/backend/results/telemetry", results.Record) r.Post(conf.BaseURL+"/backend/results/telemetry", results.Record)
r.HandleFunc("/stats", results.Stats) r.HandleFunc(conf.BaseURL+"/stats", results.Stats)
r.HandleFunc("/backend/stats", results.Stats) r.HandleFunc(conf.BaseURL+"/backend/stats", results.Stats)
// PHP frontend default values compatibility // PHP frontend default values compatibility
r.HandleFunc("/empty.php", empty) r.HandleFunc(conf.BaseURL+"/empty.php", empty)
r.HandleFunc("/backend/empty.php", empty) r.HandleFunc(conf.BaseURL+"/backend/empty.php", empty)
r.Get("/garbage.php", garbage) r.Get(conf.BaseURL+"/garbage.php", garbage)
r.Get("/backend/garbage.php", garbage) r.Get(conf.BaseURL+"/backend/garbage.php", garbage)
r.Get("/getIP.php", getIP) r.Get(conf.BaseURL+"/getIP.php", getIP)
r.Get("/backend/getIP.php", getIP) r.Get(conf.BaseURL+"/backend/getIP.php", getIP)
r.Post("/results/telemetry.php", results.Record) r.Post(conf.BaseURL+"/results/telemetry.php", results.Record)
r.Post("/backend/results/telemetry.php", results.Record) r.Post(conf.BaseURL+"/backend/results/telemetry.php", results.Record)
r.HandleFunc("/stats.php", results.Stats) r.HandleFunc(conf.BaseURL+"/stats.php", results.Stats)
r.HandleFunc("/backend/stats.php", results.Stats) r.HandleFunc(conf.BaseURL+"/backend/stats.php", results.Stats)
go listenProxyProtocol(conf, r) 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) { fn := func(w http.ResponseWriter, r *http.Request) {
if BaseURL != "" {
r.URL.Path = removeBaseURL.ReplaceAllString(r.URL.Path, "/")
}
if r.RequestURI == "/" { if r.RequestURI == "/" {
r.RequestURI = "/index.html" r.RequestURI = "/index.html"
} }