lab4: S3-вложения, presigned upload и проверки

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 22:42:48 +03:00
parent d342b2a227
commit 49c2fe3071
79 changed files with 12266 additions and 910 deletions
+59 -1
View File
@@ -8,6 +8,7 @@ import (
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
@@ -15,7 +16,9 @@ import (
"mtusi-messenger/internal/health"
"mtusi-messenger/internal/logger"
"mtusi-messenger/internal/platform/cache"
"mtusi-messenger/internal/platform/objectstore"
"mtusi-messenger/internal/platform/token"
"mtusi-messenger/internal/service/attachment"
"mtusi-messenger/internal/service/chat"
"mtusi-messenger/internal/service/message"
"mtusi-messenger/internal/service/session"
@@ -57,6 +60,57 @@ func run(cfg config.Config, log *slog.Logger) error {
defer store.Close()
objectStore, err := objectstore.New(objectstore.Config{
Endpoint: cfg.S3.Endpoint,
PublicEndpoint: cfg.S3.PublicEndpoint,
Region: cfg.S3.Region,
Bucket: cfg.S3.Bucket,
AccessKeyID: cfg.S3.AccessKeyID,
SecretAccessKey: cfg.S3.SecretAccessKey,
UsePathStyle: cfg.S3.UsePathStyle,
Timeout: cfg.S3.Timeout,
})
if err != nil {
return fmt.Errorf("connect object store: %w", err)
}
attachmentRepository := store.Attachments()
attachmentService := attachment.NewService(
attachmentRepository,
objectStore,
attachment.Config{
MaxUploadBytes: cfg.S3.MaxUploadBytes,
MaxImagePixels: cfg.S3.MaxImagePixels,
PresignTTL: cfg.S3.PresignTTL,
},
log,
)
sweeper := attachment.NewSweeper(
attachmentRepository,
objectStore,
store,
cfg.S3.SweepGrace,
cfg.S3.SweepInterval,
log,
)
sweepCtx, stopSweeper := context.WithCancel(context.Background())
var sweepWait sync.WaitGroup
sweepWait.Add(1)
go func() {
defer sweepWait.Done()
sweeper.Run(sweepCtx)
}()
defer func() {
stopSweeper()
sweepWait.Wait()
}()
redisCache, err := connectRedis(cfg.Redis)
if err != nil {
return err
@@ -97,13 +151,16 @@ func run(cfg config.Config, log *slog.Logger) error {
rest.NewHandler(
user.NewService(store.Users()),
chat.NewService(chatRepo),
message.NewService(store.Messages()),
message.NewService(store.Messages(), attachmentRepository),
attachmentService,
health.New(pingers...),
sessionService,
),
rest.NewSecurityHandler(sessionService),
rest.NewExtensions(attachmentService, sessionService, log),
log,
cfg.Server.MaxBodyBytes,
cfg.S3.MaxUploadBytes,
)
if err != nil {
return fmt.Errorf("build router: %w", err)
@@ -113,6 +170,7 @@ func run(cfg config.Config, log *slog.Logger) error {
Addr: cfg.Addr(),
Handler: router,
ReadHeaderTimeout: cfg.Server.ReadHeaderTimeout,
ReadTimeout: cfg.Server.ReadTimeout,
}
errc := make(chan error, 1)