From d6e9f0295ad222242441be286357a7337e7301c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferdinand=20M=C3=BCtsch?= Date: Sun, 24 May 2020 17:08:44 +0200 Subject: [PATCH] feat: introduce base path config option to better support redirections behind a proxy --- config.ini | 1 + main.go | 6 ++++++ models/config.go | 1 + routes/index.go | 16 ++++++++-------- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/config.ini b/config.ini index 226d7e6..85e0f9b 100644 --- a/config.ini +++ b/config.ini @@ -1,6 +1,7 @@ [server] listen = 127.0.0.1 port = 3000 +base_path = / [app] cleanup = true diff --git a/main.go b/main.go index 8a8b9e3..6d7d1c3 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,11 @@ func readConfig() *models.Config { port = cfg.Section("server").Key("port").MustInt() } + basePath := cfg.Section("server").Key("base_path").MustString("/") + if strings.HasSuffix(basePath, "/") { + basePath = basePath[:len(basePath)-1] + } + cleanUp := cfg.Section("app").Key("cleanup").MustBool(false) // Read custom languages @@ -117,6 +122,7 @@ func readConfig() *models.Config { Env: env, Port: port, Addr: addr, + BasePath: basePath, DbHost: dbHost, DbPort: uint(dbPort), DbUser: dbUser, diff --git a/models/config.go b/models/config.go index 26c58e7..750173b 100644 --- a/models/config.go +++ b/models/config.go @@ -6,6 +6,7 @@ type Config struct { Env string Port int Addr string + BasePath string DbHost string DbPort uint DbUser string diff --git a/routes/index.go b/routes/index.go index a23cc53..1698149 100644 --- a/routes/index.go +++ b/routes/index.go @@ -31,7 +31,7 @@ func (h *IndexHandler) Index(w http.ResponseWriter, r *http.Request) { } if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" { - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) return } @@ -56,7 +56,7 @@ func (h *IndexHandler) Login(w http.ResponseWriter, r *http.Request) { } if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" { - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) return } @@ -95,7 +95,7 @@ func (h *IndexHandler) Login(w http.ResponseWriter, r *http.Request) { HttpOnly: true, } http.SetCookie(w, cookie) - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) } func (h *IndexHandler) Logout(w http.ResponseWriter, r *http.Request) { @@ -104,7 +104,7 @@ func (h *IndexHandler) Logout(w http.ResponseWriter, r *http.Request) { } utils.ClearCookie(w, models.AuthCookieKey) - http.Redirect(w, r, "/", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/", h.config.BasePath), http.StatusFound) } func (h *IndexHandler) Signup(w http.ResponseWriter, r *http.Request) { @@ -113,7 +113,7 @@ func (h *IndexHandler) Signup(w http.ResponseWriter, r *http.Request) { } if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" { - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) return } @@ -133,7 +133,7 @@ func (h *IndexHandler) handleGetSignup(w http.ResponseWriter, r *http.Request) { } if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" { - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) return } @@ -150,7 +150,7 @@ func (h *IndexHandler) handlePostSignup(w http.ResponseWriter, r *http.Request) } if cookie, err := r.Cookie(models.AuthCookieKey); err == nil && cookie.Value != "" { - http.Redirect(w, r, "/summary", http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/summary", h.config.BasePath), http.StatusFound) return } @@ -180,7 +180,7 @@ func (h *IndexHandler) handlePostSignup(w http.ResponseWriter, r *http.Request) } msg := url.QueryEscape("account created successfully") - http.Redirect(w, r, fmt.Sprintf("/?success=%s", msg), http.StatusFound) + http.Redirect(w, r, fmt.Sprintf("%s/?success=%s", h.config.BasePath, msg), http.StatusFound) } func respondAlert(w http.ResponseWriter, error, success, tplName string, status int) {