Torjus Håkestad
d4b7702bad
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
40 lines
716 B
Go
40 lines
716 B
Go
package users
|
|
|
|
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
|
|
}
|