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:
parent
f09f2df2e3
commit
a5d18ef24c
@ -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")
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
|
59
web/web.go
59
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"
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user