Перенести Go API и контейнер из лабораторной 1

This commit is contained in:
Dima
2026-09-23 21:22:49 +03:00
parent 65003b6119
commit a858d3325b
21 changed files with 2375 additions and 0 deletions
+58
View File
@@ -0,0 +1,58 @@
package main
import (
"sync"
"time"
"github.com/google/uuid"
)
type User struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
CreatedAt time.Time `json:"created_at"`
}
type userStore struct {
mu sync.RWMutex
byID map[string]User
byUsername map[string]string
}
func newUserStore() *userStore {
return &userStore{
byID: make(map[string]User),
byUsername: make(map[string]string),
}
}
func (s *userStore) create(username string, displayName string) (User, bool) {
s.mu.Lock()
defer s.mu.Unlock()
if _, exists := s.byUsername[username]; exists {
return User{}, false
}
user := User{
ID: uuid.New().String(),
Username: username,
DisplayName: displayName,
CreatedAt: time.Now().UTC(),
}
s.byID[user.ID] = user
s.byUsername[user.Username] = user.ID
return user, true
}
func (s *userStore) getByID(id string) (User, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
user, exists := s.byID[id]
return user, exists
}