Files
vpn-panel/internal/handlers/flash.go
T

41 lines
815 B
Go

package handlers
import (
"net/http"
)
const flashCookie = "vpn_panel_flash"
func flashSet(w http.ResponseWriter, msg, level string) {
http.SetCookie(w, &http.Cookie{
Name: flashCookie,
Value: level + ":" + msg,
Path: "/",
MaxAge: 30,
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
})
}
func flashGet(w http.ResponseWriter, r *http.Request) map[string]string {
c, err := r.Cookie(flashCookie)
if err != nil {
return nil
}
http.SetCookie(w, &http.Cookie{Name: flashCookie, Path: "/", MaxAge: -1})
parts := splitFlash(c.Value)
if len(parts) != 2 {
return nil
}
return map[string]string{"Level": parts[0], "Message": parts[1]}
}
func splitFlash(v string) []string {
for i := 0; i < len(v); i++ {
if v[i] == ':' {
return []string{v[:i], v[i+1:]}
}
}
return nil
}