Store values in session, and automatically save changes

This commit is contained in:
nemunaire 2020-07-08 13:25:41 +02:00
parent fe3bba74bd
commit bbafb1bc6d
2 changed files with 42 additions and 3 deletions

View File

@ -214,6 +214,10 @@ func apiAuthHandler(f func(*config.Options, *RequestResources, io.Reader) Respon
User: user,
}
f(opts, req, r.Body).WriteResponse(w)
if session.HasChanged() {
storage.MainStore.UpdateSession(session)
}
}
}
}

View File

@ -37,9 +37,11 @@ import (
)
type Session struct {
Id []byte `json:"id"`
IdUser int64 `json:"login"`
Time time.Time `json:"time"`
Id []byte `json:"id"`
IdUser int64 `json:"login"`
Time time.Time `json:"time"`
Content map[string]interface{} `json:"content,omitempty"`
changed bool
}
func NewSession(user *User) (s *Session, err error) {
@ -55,3 +57,36 @@ func NewSession(user *User) (s *Session, err error) {
return
}
func (s *Session) HasChanged() bool {
return s.changed
}
func (s *Session) SetValue(key string, value interface{}) {
if s.Content == nil && value != nil {
s.Content = map[string]interface{}{}
}
if value == nil {
if s.Content == nil {
return
} else if _, ok := s.Content[key]; !ok {
return
} else {
delete(s.Content, key)
s.changed = true
}
} else {
s.Content[key] = value
s.changed = true
}
}
func (s *Session) GetValue(key string, value interface{}) (ok bool) {
value, ok = s.Content[key]
return
}
func (s *Session) DropKey(key string) {
s.SetValue(key, nil)
}