package sessions import ( "net/http" "sync" "github.com/google/uuid" ) type sessionvalues map[string]any type Session struct { id string values sessionvalues mu *sync.Mutex } type SessionCtxKey string var _sessions map[string]Session func NewSession(w http.ResponseWriter, values map[string]any) Session { token := uuid.NewString() // set secure cookie in http.ResponseWriter // TODO make secure http.SetCookie(w, &http.Cookie{ Name: "session_token", Value: token, }) // create session and store s := Session{ id: token, values: sessionvalues(values), } _sessions[token] = s return s } func GetSession(id string) (Session, bool) { s, ok := _sessions[id] return s, ok } func (s *Session) Id() string { return s.id } func (s *Session) Get(key string) interface{} { s.mu.Lock() defer s.mu.Unlock() return s.values[key] } func (s *Session) Set(key string, value interface{}) bool { s.mu.Lock() defer s.mu.Unlock() s.values[key] = value return true }