summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2023-06-20 21:31:12 +0200
committerThomas Voss <mail@thomasvoss.com> 2023-06-20 21:31:12 +0200
commit2ecf4ce7e37cb7a6fb1cfc7464d7ac40fb326605 (patch)
treef24194f96b5f31fff0b5f588d0f5b361ffb05119
parentbf17ee6590b353dd9ad4cfc6269fe65631bf6c5b (diff)
Turn session cookie to persistent cookie
-rw-r--r--server.go28
1 files changed, 17 insertions, 11 deletions
diff --git a/server.go b/server.go
index 10cb474..4788de7 100644
--- a/server.go
+++ b/server.go
@@ -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: "/",
})
}
}