Выполнение lab4 на 4

This commit is contained in:
Daria0w0
2026-11-06 17:35:48 +03:00
parent 9197e151ba
commit 879463c947
89 changed files with 5551 additions and 108 deletions
+15 -2
View File
@@ -10,19 +10,22 @@ import (
"gitflic.ru/project/d-sotnikova5/mtusi2026lab1-fork/internal/store"
)
func NewPostgresHandler(db *store.PostgresStore, stopping *atomic.Bool, ttl int64) (http.Handler, error) {
func NewPostgresHandler(db *store.PostgresStore, stopping *atomic.Bool, ttl int64, attachmentServices ...*service.Attachments) (http.Handler, error) {
registration := service.NewRegistration(db)
sessions, err := service.NewSessions(db, registration, ttl)
if err != nil {
return nil, err
}
chats := &persistentAPI{messenger: service.NewPersistentMessenger(db)}
if len(attachmentServices) > 0 {
chats.attachments = attachmentServices[0]
}
return withInstanceID(withRequestID(withAccessLog(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if stopping.Load() && r.URL.Path != "/health/live" {
writeAppError(w, store.ErrUnavailable)
return
}
if r.URL.Path == "/api/v1/chats" || strings.HasPrefix(r.URL.Path, "/api/v1/chats/") {
if r.URL.Path == "/api/v1/chats" || strings.HasPrefix(r.URL.Path, "/api/v1/chats/") || strings.HasPrefix(r.URL.Path, "/api/v1/attachments/") {
token, err := bearerToken(r)
if err != nil {
sessionError(w, err, false)
@@ -34,6 +37,10 @@ func NewPostgresHandler(db *store.PostgresStore, stopping *atomic.Bool, ttl int6
return
}
ctx := context.WithValue(r.Context(), userContextKey{}, user)
if strings.HasPrefix(r.URL.Path, "/api/v1/attachments/") {
attachmentRoutes(w, r.WithContext(ctx), chats.attachments)
return
}
chats.chatRoutes(w, r.WithContext(ctx))
return
}
@@ -54,6 +61,12 @@ func NewPostgresHandler(db *store.PostgresStore, stopping *atomic.Bool, ttl int6
writeAppError(w, err)
return
}
if chats.attachments != nil {
if err := chats.attachments.Ready(r.Context()); err != nil {
writeAppError(w, err)
return
}
}
handleHealth(w, r)
case "/api/v1/users":
if !requireMethod(w, r, http.MethodPost) {