лаба 4 на оценку 5

This commit is contained in:
2026-10-30 12:27:49 +03:00
parent 7860d81fa1
commit f9444a3965
59 changed files with 4696 additions and 90 deletions
+75
View File
@@ -0,0 +1,75 @@
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"os"
"time"
"mtusi2026lab4/internal/adapter/objectstore"
"mtusi2026lab4/internal/adapter/postgres"
"mtusi2026lab4/internal/config"
"mtusi2026lab4/internal/database"
"mtusi2026lab4/internal/reconcile"
)
func main() {
apply := flag.Bool("apply", false, "apply safe repairs; default is dry-run")
gracePeriod := flag.Duration("grace-period", 15*time.Minute,
"ignore metadata and objects newer than this duration")
timeout := flag.Duration("timeout", 2*time.Minute, "maximum reconciliation runtime")
flag.Parse()
if *gracePeriod <= 0 || *timeout <= 0 {
log.Fatal("[FATAL] grace-period and timeout must be positive")
}
cfg, err := config.Load()
if err != nil {
log.Fatalf("[FATAL] Invalid configuration: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
defer cancel()
databasePool, err := database.Open(ctx, cfg)
if err != nil {
log.Fatalf("[FATAL] PostgreSQL initialization failed: %v", err)
}
defer databasePool.Close()
metadataStore := postgres.NewPostgreSQLStore(databasePool, cfg.DatabaseQueryTimeout)
objectStore, err := objectstore.NewMinIOStore(
cfg.S3Endpoint,
cfg.S3Region,
cfg.S3Bucket,
cfg.S3AccessKeyID,
cfg.S3SecretAccessKey,
cfg.S3OperationTimeout,
)
if err != nil {
log.Fatalf("[FATAL] Invalid object storage configuration: %v", err)
}
if err = objectStore.Ping(ctx); err != nil {
log.Fatalf("[FATAL] Object storage unavailable: %v", err)
}
reconciler, err := reconcile.New(metadataStore, objectStore)
if err != nil {
log.Fatalf("[FATAL] Reconciler initialization failed: %v", err)
}
result, err := reconciler.Run(ctx, *gracePeriod, *apply)
if err != nil {
log.Fatalf("[FATAL] Reconciliation failed: %v", err)
}
encoder := json.NewEncoder(os.Stdout)
encoder.SetIndent("", " ")
if err = encoder.Encode(result); err != nil {
log.Fatalf("[FATAL] Encode reconciliation result: %v", err)
}
if !*apply && len(result.Issues) > 0 {
fmt.Fprintln(os.Stderr, "dry-run found reconciliation issues; rerun with --apply after review")
}
}
+36 -1
View File
@@ -11,6 +11,8 @@ import (
"time"
cacheadapter "mtusi2026lab4/internal/adapter/cache"
"mtusi2026lab4/internal/adapter/health"
"mtusi2026lab4/internal/adapter/objectstore"
"mtusi2026lab4/internal/adapter/postgres"
redisadapter "mtusi2026lab4/internal/adapter/redis"
"mtusi2026lab4/internal/application"
@@ -43,6 +45,25 @@ func main() {
databasePool,
cfg.DatabaseQueryTimeout,
)
minioStore, err := objectstore.NewMinIOStore(
cfg.S3Endpoint,
cfg.S3Region,
cfg.S3Bucket,
cfg.S3AccessKeyID,
cfg.S3SecretAccessKey,
cfg.S3OperationTimeout,
)
if err != nil {
log.Fatalf("[FATAL] Invalid object storage configuration: %v", err)
}
storageContext, storageCancel := context.WithTimeout(
context.Background(),
cfg.S3OperationTimeout,
)
if err = minioStore.Ping(storageContext); err != nil {
log.Printf("[WARN] Object storage unavailable at startup: %v", err)
}
storageCancel()
var applicationStore application.Repository = postgresStore
@@ -90,6 +111,7 @@ func main() {
cursorCodec,
cfg.SessionTTL,
application.WithIdempotencyTTL(cfg.IdempotencyTTL),
application.WithObjectStore(minioStore),
)
if err != nil {
log.Fatalf("[FATAL] Application initialization failed: %v", err)
@@ -98,7 +120,7 @@ func main() {
handler := handlers.NewHandler(
service,
handlers.WithReadiness(
databasePool,
health.NewMultiChecker(databasePool, minioStore),
cfg.ReadinessTimeout,
),
handlers.WithSessionCookieSecure(cfg.SessionCookieSecure),
@@ -107,6 +129,7 @@ func main() {
mux := http.NewServeMux()
mux.HandleFunc("GET /health/live", handler.HealthLive)
mux.HandleFunc("GET /health/lb", handler.HealthLive)
mux.HandleFunc("GET /health/ready", handler.HealthReady)
mux.HandleFunc(
@@ -150,6 +173,18 @@ func main() {
"GET /api/v1/chats/{chat_id}/messages",
requireAuth(handler.GetMessages),
)
mux.HandleFunc(
"POST /api/v1/chats/{chat_id}/attachments",
requireAuth(handler.UploadAttachment),
)
mux.HandleFunc(
"GET /api/v1/attachments/{attachment_id}",
requireAuth(handler.GetAttachment),
)
mux.HandleFunc(
"GET /api/v1/attachments/{attachment_id}/content",
requireAuth(handler.DownloadAttachment),
)
var rootHandler http.Handler = mux
rootHandler = middleware.Recovery(rootHandler)