lishwist/http/session/session.go
2026-02-13 10:36:24 +09:00

88 lines
1.4 KiB
Go

package session
import (
"net/http"
"github.com/gorilla/sessions"
)
type Session struct {
inner *sessions.Session
written bool
}
func New(inner *sessions.Session) *Session {
return &Session{inner: inner}
}
const flashKey = "_flash"
func (s *Session) FlashGet() any {
val, ok := s.inner.Values[flashKey]
if !ok {
return nil
}
delete(s.inner.Values, flashKey)
s.written = true
return val
}
func (s *Session) FlashPeek() any {
val, ok := s.inner.Values[flashKey]
if !ok {
return nil
}
return val
}
func (s *Session) FlashSet(value any) {
s.inner.Values[flashKey] = value
s.written = true
}
func (s *Session) SetID(value string) {
s.inner.ID = value
s.written = true
}
func (s *Session) SetValue(key any, value any) {
s.inner.Values[key] = value
s.written = true
}
func (s *Session) RemoveValue(key any) {
delete(s.inner.Values, key)
s.written = true
}
func (s *Session) GetValue(key any) (any, bool) {
val, ok := s.inner.Values[key]
return val, ok
}
func (s *Session) GetString(key any) (string, bool) {
val, ok := s.GetValue(key)
if !ok {
return "", false
}
str, ok := val.(string)
return str, ok
}
func (s *Session) ClearValues() {
s.inner.Values = make(map[any]any)
s.written = true
}
func (s *Session) Options() *sessions.Options {
return s.inner.Options
}
func (s *Session) WasWritten() bool {
return s.written
}
func (s *Session) Save(r *http.Request, w http.ResponseWriter) error {
return s.inner.Save(r, w)
}