diff options
author | Thomas Voss <mail@thomasvoss.com> | 2023-06-20 21:31:12 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-06-20 21:31:12 +0200 |
commit | 2ecf4ce7e37cb7a6fb1cfc7464d7ac40fb326605 (patch) | |
tree | f24194f96b5f31fff0b5f588d0f5b361ffb05119 | |
parent | bf17ee6590b353dd9ad4cfc6269fe65631bf6c5b (diff) |
Turn session cookie to persistent cookie
-rw-r--r-- | server.go | 28 |
1 files changed, 17 insertions, 11 deletions
@@ -44,7 +44,7 @@ func router(w http.ResponseWriter, r *http.Request) { Name: "redirect", Path: "/", }) - http.Redirect(w, r, url + lang + "/", http.StatusTemporaryRedirect) + http.Redirect(w, r, url+lang+"/", http.StatusTemporaryRedirect) return } @@ -54,21 +54,27 @@ func router(w http.ResponseWriter, r *http.Request) { _, err = r.Cookie("redirect") if isSupportedLanguage(parts[0]) && parts[0] != lang { if r.Header.Get("Referer") == "" && err != nil { - parts[0] = lang - path = strings.Join(parts, "/") - http.SetCookie(w, &http.Cookie{ - Name: "redirect", - Path: "/", - }) - http.Redirect(w, r, url + path, http.StatusTemporaryRedirect) + if !errors.Is(err, http.ErrNoCookie) { + log.Println(err) + http.Error(w, "An error occured", http.StatusInternalServerError) + } else { + parts[0] = lang + path = strings.Join(parts, "/") + http.SetCookie(w, &http.Cookie{ + Name: "redirect", + Path: "/", + }) + http.Redirect(w, r, url+path, http.StatusTemporaryRedirect) + } return } if r.Header.Get("Referer") != "" { http.SetCookie(w, &http.Cookie{ - Name: CookieName, - Value: parts[0], - Path: "/", + Name: CookieName, + Value: parts[0], + Expires: time.Now().AddDate(100, 0, 0), + Path: "/", }) } } |