feat: introduce base path config option to better support redirections behind a proxy

This commit is contained in:
Ferdinand Mütsch 2020-05-24 17:08:44 +02:00
parent 411ae49206
commit d6e9f0295a
4 changed files with 16 additions and 8 deletions

View File

@ -1,6 +1,7 @@
[server]
listen = 127.0.0.1
port = 3000
base_path = /
[app]
cleanup = true

View File

@ -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,

View File

@ -6,6 +6,7 @@ type Config struct {
Env string
Port int
Addr string
BasePath string
DbHost string
DbPort uint
DbUser string

View File

@ -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) {