lab 4 na 4

This commit is contained in:
2026-11-05 13:52:11 +03:00
parent 14166003b8
commit f6292ac308
48 changed files with 2255 additions and 183 deletions
+13 -1
View File
@@ -7,6 +7,7 @@ import (
"github.com/jackc/pgx/v5/pgxpool"
"github.com/redis/go-redis/v9"
"mtusi2026/internal/blobstore"
"mtusi2026/internal/cache"
"mtusi2026/internal/model"
"mtusi2026/internal/sessionstore"
@@ -17,6 +18,8 @@ type Options struct {
InstanceID string
Redis *redis.Client
Draining *atomic.Bool
Blobs blobstore.Store
MaxUploads int
}
func NewRouter(s *store.Store, pool *pgxpool.Pool, sessionStores ...sessionstore.Store) http.Handler {
@@ -45,10 +48,16 @@ func NewRouterWithCache(
o.Draining = new(atomic.Bool)
}
if o.MaxUploads <= 0 {
o.MaxUploads = 4
}
a := &api{
store: s,
sessionStore: sessionStore,
chatCache: chatCache,
blobs: o.Blobs,
uploadSem: make(chan struct{}, o.MaxUploads),
}
apiMux := http.NewServeMux()
@@ -60,6 +69,9 @@ func NewRouterWithCache(
apiMux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleCreateMessage))
apiMux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", a.authMiddleware(a.handleListMessages))
apiMux.HandleFunc("POST /api/v1/auth/sessions", a.handleCreateSession)
apiMux.HandleFunc("POST /api/v1/chats/{chat_id}/attachments", a.authMiddleware(a.handleUploadAttachment))
apiMux.HandleFunc("GET /api/v1/attachments/{attachment_id}", a.authMiddleware(a.handleGetAttachment))
apiMux.HandleFunc("GET /api/v1/attachments/{attachment_id}/content", a.authMiddleware(a.handleDownloadAttachment))
apiMux.HandleFunc("DELETE /api/v1/auth/sessions/current", a.authMiddleware(
func(w http.ResponseWriter, r *http.Request, u model.User, requestID string) {
a.handleDeleteSession(w, r)
@@ -67,7 +79,7 @@ func NewRouterWithCache(
root := http.NewServeMux()
root.HandleFunc("GET /health/live", handleHealthLive)
root.HandleFunc("GET /health/ready", handleHealthReady(pool, o.Redis, o.Draining))
root.HandleFunc("GET /health/ready", handleHealthReady(pool, o.Redis, o.Blobs, o.Draining))
root.Handle("/", concurrencyMiddleware(s.Limiter, apiMux))
var h http.Handler = root