40 lines
717 B
Go
40 lines
717 B
Go
|
package gpaste
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
type MemoryUserStore struct {
|
||
|
users map[string]*User
|
||
|
lock sync.Mutex
|
||
|
}
|
||
|
|
||
|
func NewMemoryUserStore() *MemoryUserStore {
|
||
|
return &MemoryUserStore{users: make(map[string]*User)}
|
||
|
}
|
||
|
func (s *MemoryUserStore) Get(username string) (*User, error) {
|
||
|
s.lock.Lock()
|
||
|
defer s.lock.Unlock()
|
||
|
user, ok := s.users[username]
|
||
|
if !ok {
|
||
|
return nil, fmt.Errorf("no such user: %s", username)
|
||
|
}
|
||
|
|
||
|
return user, nil
|
||
|
}
|
||
|
|
||
|
func (s *MemoryUserStore) Store(user *User) error {
|
||
|
s.lock.Lock()
|
||
|
defer s.lock.Unlock()
|
||
|
s.users[user.Username] = user
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (s *MemoryUserStore) Delete(username string) error {
|
||
|
s.lock.Lock()
|
||
|
defer s.lock.Unlock()
|
||
|
delete(s.users, username)
|
||
|
return nil
|
||
|
}
|