From 2ecf4ce7e37cb7a6fb1cfc7464d7ac40fb326605 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Tue, 20 Jun 2023 21:31:12 +0200 Subject: Turn session cookie to persistent cookie --- server.go | 28 +++++++++++++++++----------- 1 file 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: "/", }) } } -- cgit v1.2.3