Created
October 5, 2025 23:57
-
-
Save KaiserWerk/f9f160ec0be5a4bef20986513ea4f995 to your computer and use it in GitHub Desktop.
Message passing in templates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package templating | |
| import ( | |
| "encoding/base64" | |
| "net/http" | |
| "time" | |
| "reload-helper/internal/global" | |
| ) | |
| func GetSuccessMessage(w http.ResponseWriter, r *http.Request) string { | |
| return getMessageFromCookie(w, r, global.SuccessCookieName) | |
| } | |
| func SetSuccessMessage(w http.ResponseWriter, message string) { | |
| c := &http.Cookie{ | |
| Name: global.SuccessCookieName, | |
| Value: base64.RawURLEncoding.EncodeToString([]byte(message)), | |
| HttpOnly: true, | |
| Expires: time.Now().Add(24 * time.Hour), | |
| Path: "/", | |
| } | |
| http.SetCookie(w, c) | |
| } | |
| func GetErrorMessage(w http.ResponseWriter, r *http.Request) string { | |
| return getMessageFromCookie(w, r, global.ErrorCookieName) | |
| } | |
| func SetErrorMessage(w http.ResponseWriter, message string) { | |
| c := &http.Cookie{ | |
| Name: global.ErrorCookieName, | |
| Value: base64.RawURLEncoding.EncodeToString([]byte(message)), | |
| HttpOnly: true, | |
| Expires: time.Now().Add(24 * time.Hour), | |
| Path: "/", | |
| } | |
| http.SetCookie(w, c) | |
| } | |
| func getMessageFromCookie(w http.ResponseWriter, r *http.Request, cookieName string) string { | |
| c, err := r.Cookie(cookieName) | |
| if err != nil { | |
| return "" | |
| } | |
| if c == nil { | |
| return "" | |
| } | |
| if c.Value == "" { | |
| removeCookie(w, cookieName) | |
| return "" | |
| } | |
| // Clear the cookie after reading it | |
| removeCookie(w, cookieName) | |
| v, _ := base64.RawURLEncoding.DecodeString(c.Value) | |
| return string(v) | |
| } | |
| func removeCookie(w http.ResponseWriter, cookieName string) { | |
| c2 := &http.Cookie{ | |
| Name: cookieName, | |
| Value: "", | |
| Path: "/", | |
| Expires: time.Unix(0, 0), | |
| HttpOnly: true, | |
| } | |
| http.SetCookie(w, c2) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment