1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

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] [server]
listen = 127.0.0.1 listen = 127.0.0.1
port = 3000 port = 3000
base_path = /
[app] [app]
cleanup = true cleanup = true

View File

@@ -77,6 +77,11 @@ func readConfig() *models.Config {
port = cfg.Section("server").Key("port").MustInt() 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) cleanUp := cfg.Section("app").Key("cleanup").MustBool(false)
// Read custom languages // Read custom languages
@@ -117,6 +122,7 @@ func readConfig() *models.Config {
Env: env, Env: env,
Port: port, Port: port,
Addr: addr, Addr: addr,
BasePath: basePath,
DbHost: dbHost, DbHost: dbHost,
DbPort: uint(dbPort), DbPort: uint(dbPort),
DbUser: dbUser, DbUser: dbUser,

View File

@@ -6,6 +6,7 @@ type Config struct {
Env string Env string
Port int Port int
Addr string Addr string
BasePath string
DbHost string DbHost string
DbPort uint DbPort uint
DbUser string 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 != "" { 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 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 != "" { 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 return
} }
@@ -95,7 +95,7 @@ func (h *IndexHandler) Login(w http.ResponseWriter, r *http.Request) {
HttpOnly: true, HttpOnly: true,
} }
http.SetCookie(w, cookie) 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) { 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) 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) { 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 != "" { 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 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 != "" { 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 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 != "" { 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 return
} }
@@ -180,7 +180,7 @@ func (h *IndexHandler) handlePostSignup(w http.ResponseWriter, r *http.Request)
} }
msg := url.QueryEscape("account created successfully") 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) { func respondAlert(w http.ResponseWriter, error, success, tplName string, status int) {