forked from mtusi2026/mtusi2026lab6
Compare commits
30
Commits
+65
-11
@@ -1,30 +1,84 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
# Run ./setup_secrets.sh before starting to generate required secrets.
|
||||
|
||||
# ─────────────── Приложение ───────────────
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SHUTDOWN_DRAIN_SECONDS=3
|
||||
|
||||
# ─────────────── Сессии ───────────────
|
||||
# redis | postgres
|
||||
SESSION_STORE=redis
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
|
||||
# ─────────────── Хранилища (секреты через файлы) ───────────────
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
|
||||
# ─────────────── S3 / MinIO ───────────────
|
||||
# Сервис в compose называется "minio", не "s3"
|
||||
S3_ENDPOINT=http://minio:9000
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
S3_USE_SSL=false
|
||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
MINIO_ROOT_USER_FILE=/run/secrets/minio_root_user
|
||||
MINIO_ROOT_PASSWORD_FILE=/run/secrets/minio_root_password
|
||||
|
||||
# ─────────────── Лимиты загрузки ───────────────
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
BROKER_URL_FILE=/run/secrets/broker_url
|
||||
|
||||
# ─────────────── RabbitMQ ───────────────
|
||||
# Должно совпадать с содержимым ./secrets/rabbitmq_password
|
||||
RABBITMQ_PASSWORD=secret_rabbit
|
||||
RABBITMQ_URL=amqp://messenger:secret_rabbit@rabbitmq:5672/
|
||||
|
||||
# ─────────────── Outbox и worker ───────────────
|
||||
OUTBOX_INTERVAL_MS=1000
|
||||
WORKER_CONCURRENCY=2
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
STAGING_RETENTION_SECONDS=86400
|
||||
OTEL_SERVICE_NAME=messenger-api
|
||||
# Worker must set a distinct service.name.
|
||||
|
||||
# ─────────────── Reconciler (отдельный сервис в lab6) ───────────────
|
||||
# 60с удобно для демонстрации; 300с — дефолт в коде
|
||||
RECONCILER_INTERVAL_SECONDS=60
|
||||
RECONCILER_GRACE_SECONDS=3600
|
||||
|
||||
# ─────────────── Масштабирование и ресурсы ───────────────
|
||||
API_REPLICAS=2
|
||||
LB_PORT=8080
|
||||
|
||||
API_CPUS=0.5
|
||||
API_MEMORY=256M
|
||||
API_CPU_RESERVATION=0.1
|
||||
API_MEMORY_RESERVATION=64M
|
||||
|
||||
POSTGRES_CPUS=1.0
|
||||
POSTGRES_MEMORY=1G
|
||||
POSTGRES_CPU_RESERVATION=0.25
|
||||
POSTGRES_MEMORY_RESERVATION=256M
|
||||
|
||||
REDIS_CPUS=0.5
|
||||
REDIS_MEMORY=256M
|
||||
|
||||
MINIO_CPUS=0.5
|
||||
MINIO_MEMORY=512M
|
||||
|
||||
# ─────────────── OpenTelemetry ───────────────
|
||||
# service.name задаётся отдельно для каждого сервиса в compose.yaml:
|
||||
# messenger-api / messenger-worker / messenger-reconciler
|
||||
OTEL_EXPORTER_OTLP_ENDPOINT=http://otel-collector:4318
|
||||
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
|
||||
OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=lab
|
||||
OTEL_TRACES_SAMPLER=parentbased_always_on
|
||||
OTEL_METRIC_EXPORT_INTERVAL=15000
|
||||
|
||||
# ─────────────── Grafana ───────────────
|
||||
GRAFANA_PORT=3000
|
||||
GRAFANA_ADMIN_USER=admin
|
||||
GRAFANA_ADMIN_PASSWORD=admin # только локально; не использовать вне лабы
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
FROM golang:1.26.1-alpine AS builder
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY backend/go.mod backend/go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY backend/ .
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/server \
|
||||
./cmd/server
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/worker \
|
||||
./cmd/worker
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/reconciler \
|
||||
./cmd/reconciler
|
||||
|
||||
FROM alpine:3.20
|
||||
|
||||
RUN adduser -D -u 1000 app
|
||||
|
||||
COPY --from=builder /out/server /server
|
||||
COPY --from=builder /out/worker /worker
|
||||
COPY --from=builder /out/reconciler /reconciler
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
USER app
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O - http://127.0.0.1:8080/health/live >/dev/null || exit 1
|
||||
|
||||
CMD ["/server"]
|
||||
@@ -0,0 +1,116 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/config"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
"high-loud-apps-labs/internal/storage/postgres"
|
||||
s3store "high-loud-apps-labs/internal/storage/s3"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("reconciler: конфигурация: %v", err)
|
||||
}
|
||||
|
||||
// if !cfg.ReconcilerEnabled {
|
||||
// log.Println("reconciler: RECONCILER_ENABLED=false, выходим")
|
||||
// return
|
||||
// }
|
||||
|
||||
instanceID := os.Getenv("INSTANCE_ID")
|
||||
if instanceID == "" {
|
||||
instanceID, _ = os.Hostname()
|
||||
}
|
||||
tel, err := telemetry.Setup(context.Background(), telemetry.SetupConfig{
|
||||
ServiceName: "messenger-reconciler",
|
||||
ServiceVersion: "1.6.0",
|
||||
InstanceID: instanceID,
|
||||
Environment: "lab",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("reconciler: телеметрия: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := tel.Shutdown(ctx); err != nil {
|
||||
log.Printf("reconciler: остановка телеметрии: %v", err)
|
||||
}
|
||||
}()
|
||||
slog.SetDefault(tel.Logger)
|
||||
|
||||
if !cfg.S3Configured() {
|
||||
slog.Error("reconciler: S3 не настроен: задайте S3_ENDPOINT, S3_BUCKET, S3_ACCESS_KEY_ID_FILE/S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY_FILE/S3_SECRET_ACCESS_KEY")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
pool, err := postgres.Connect(ctx, postgres.Config{
|
||||
URL: cfg.DatabaseURL,
|
||||
MaxConns: 5,
|
||||
MinConns: 1,
|
||||
MaxConnLifetime: time.Hour,
|
||||
MaxConnIdleTime: 30 * time.Minute,
|
||||
ConnectTimeout: 5 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("reconciler: ошибка БД", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
registration, err := postgres.ObservePool(pool)
|
||||
if err != nil {
|
||||
slog.Warn("Не удалось зарегистрировать метрики пула", "error", err)
|
||||
} else {
|
||||
defer registration.Unregister()
|
||||
}
|
||||
|
||||
store := postgres.New(pool)
|
||||
|
||||
objectStorage, err := s3store.New(ctx, s3store.Config{
|
||||
Endpoint: cfg.S3Endpoint,
|
||||
Bucket: cfg.S3Bucket,
|
||||
AccessKey: cfg.S3AccessKey,
|
||||
SecretKey: cfg.S3SecretKey,
|
||||
UseSSL: cfg.S3UseSSL,
|
||||
Region: cfg.S3Region,
|
||||
})
|
||||
if err != nil {
|
||||
var unavailable *domain.UnavailableError
|
||||
if errors.As(err, &unavailable) {
|
||||
slog.Error("reconciler: ошибка s3", "error", err, "reason", unavailable.Reason)
|
||||
} else {
|
||||
slog.Error("reconciler: ошибка s3", "error", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rec := service.NewReconciler(
|
||||
store.Attachments(),
|
||||
objectStorage,
|
||||
store.Outbox(),
|
||||
pool,
|
||||
cfg.ReconcilerGracePeriod,
|
||||
)
|
||||
|
||||
runCtx, stop := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
slog.Info("reconciler: запущен", "interval", cfg.ReconcilerInterval, "grace", cfg.ReconcilerGracePeriod)
|
||||
rec.Run(runCtx, cfg.ReconcilerInterval)
|
||||
slog.Info("reconciler: остановлен")
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/broker"
|
||||
"high-loud-apps-labs/internal/config"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi"
|
||||
"high-loud-apps-labs/internal/httpapi/handler"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/outbox"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
"high-loud-apps-labs/internal/storage/postgres"
|
||||
redisstore "high-loud-apps-labs/internal/storage/redis"
|
||||
s3store "high-loud-apps-labs/internal/storage/s3"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
if err := runMigrate(); err != nil {
|
||||
log.Fatalf("миграции: %v", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("конфигурация: %v", err)
|
||||
}
|
||||
|
||||
instanceID := os.Getenv("INSTANCE_ID")
|
||||
if instanceID == "" {
|
||||
instanceID, _ = os.Hostname()
|
||||
}
|
||||
tel, err := telemetry.Setup(context.Background(), telemetry.SetupConfig{
|
||||
ServiceName: "messenger-api",
|
||||
ServiceVersion: "1.6.0",
|
||||
InstanceID: instanceID,
|
||||
Environment: "lab",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("телеметрия: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := tel.Shutdown(ctx); err != nil {
|
||||
log.Printf("остановка телеметрии: %v", err)
|
||||
}
|
||||
}()
|
||||
// Делаем логгер глобальным для всего приложения
|
||||
slog.SetDefault(tel.Logger)
|
||||
|
||||
addr := cfg.Host + ":" + cfg.Port
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := postgres.Connect(ctx, postgres.Config{
|
||||
URL: cfg.DatabaseURL,
|
||||
MaxConns: 10,
|
||||
MinConns: 1,
|
||||
MaxConnLifetime: time.Hour,
|
||||
MaxConnIdleTime: 30 * time.Minute,
|
||||
ConnectTimeout: 5 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Ошибка подключения к БД", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
registration, err := postgres.ObservePool(pool)
|
||||
if err != nil {
|
||||
slog.Warn("Не удалось зарегистрировать метрики пула", "error", err)
|
||||
} else {
|
||||
defer registration.Unregister()
|
||||
}
|
||||
|
||||
store := postgres.New(pool)
|
||||
|
||||
rabbitPub, err := broker.NewRabbitPublisher(cfg.RabbitMQURL)
|
||||
if err != nil {
|
||||
slog.Error("Ошибка подключения к RabbitMQ", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer rabbitPub.Close()
|
||||
|
||||
outboxPub := outbox.NewPublisher(store.Outbox(), rabbitPub, 50)
|
||||
outboxCtx, outboxCancel := context.WithCancel(context.Background())
|
||||
defer outboxCancel()
|
||||
go outboxPub.Run(outboxCtx, cfg.OutboxInterval)
|
||||
|
||||
sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store)
|
||||
defer closeSession()
|
||||
|
||||
hasher := service.NewArgon2idHasher()
|
||||
|
||||
userSvc := service.NewUserService(store.Users(), hasher)
|
||||
sessionSvc := service.NewSessionService(store.Users(), sessionRepo, hasher, cfg.SessionTTL)
|
||||
chatSvc := service.NewChatService(store.Chats(), store.Users())
|
||||
messageSvc := service.NewMessageService(store.Messages(), store.Chats(), cfg.IdempotencyTTL)
|
||||
|
||||
if !cfg.S3Configured() {
|
||||
slog.Error("S3 не настроен: задайте S3_ENDPOINT, S3_BUCKET, S3_ACCESS_KEY_ID_FILE/S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY_FILE/S3_SECRET_ACCESS_KEY")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
objectStorage, err := s3store.New(ctx, s3store.Config{
|
||||
Endpoint: cfg.S3Endpoint,
|
||||
Bucket: cfg.S3Bucket,
|
||||
AccessKey: cfg.S3AccessKey,
|
||||
SecretKey: cfg.S3SecretKey,
|
||||
UseSSL: cfg.S3UseSSL,
|
||||
Region: cfg.S3Region,
|
||||
})
|
||||
if err != nil {
|
||||
var unavailable *domain.UnavailableError
|
||||
if errors.As(err, &unavailable) {
|
||||
slog.Error("Ошибка S3", "error", err, "reason", unavailable.Reason)
|
||||
} else {
|
||||
slog.Error("Ошибка S3", "error", err)
|
||||
}
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
attachmentSvc := service.NewAttachmentService(
|
||||
store.Attachments(),
|
||||
store.Chats(),
|
||||
objectStorage,
|
||||
store.Outbox(),
|
||||
store.FailedJobs(),
|
||||
store,
|
||||
cfg.MaxUploadBytes,
|
||||
cfg.MaxImagePixels,
|
||||
)
|
||||
|
||||
healthChecks := []handler.HealthCheck{
|
||||
func(ctx context.Context) error { return pool.Ping(ctx) },
|
||||
func(ctx context.Context) error { return objectStorage.Ping(ctx) },
|
||||
}
|
||||
if cachePing != nil {
|
||||
healthChecks = append(healthChecks, cachePing)
|
||||
}
|
||||
|
||||
var ready atomic.Bool
|
||||
ready.Store(true)
|
||||
|
||||
router := httpapi.NewRouter(
|
||||
httpapi.Handlers{
|
||||
Health: handler.NewHealthHandler(healthChecks...).WithReadyFlag(&ready),
|
||||
User: handler.NewUserHandler(userSvc),
|
||||
Chat: handler.NewChatHandler(chatSvc),
|
||||
Message: handler.NewMessageHandler(messageSvc),
|
||||
Auth: handler.NewAuthHandler(sessionSvc),
|
||||
Attachment: handler.NewAttachmentHandler(attachmentSvc),
|
||||
},
|
||||
middleware.BearerAuth(sessionSvc),
|
||||
)
|
||||
|
||||
srv := &http.Server{Addr: addr, Handler: router}
|
||||
|
||||
// reconcilerCtx, reconcilerCancel := context.WithCancel(context.Background())
|
||||
// defer reconcilerCancel()
|
||||
// if cfg.ReconcilerEnabled {
|
||||
// reconciler := service.NewReconciler(
|
||||
// store.Attachments(),
|
||||
// objectStorage,
|
||||
// store.Outbox(),
|
||||
// pool,
|
||||
// cfg.ReconcilerGracePeriod,
|
||||
// )
|
||||
// go reconciler.Run(reconcilerCtx, cfg.ReconcilerInterval)
|
||||
// }
|
||||
|
||||
go func() {
|
||||
slog.Info("Запускаем сервер", "addr", addr, "session_store", cfg.SessionStore)
|
||||
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("Ошибка запуска сервера", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}()
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
slog.Info("Получен сигнал завершения; readiness выключен", "drain", cfg.ShutdownDrain)
|
||||
ready.Store(false)
|
||||
|
||||
outboxCancel()
|
||||
|
||||
if cfg.ShutdownDrain > 0 {
|
||||
time.Sleep(cfg.ShutdownDrain)
|
||||
}
|
||||
|
||||
slog.Info("Drain завершён, останавливаем HTTP-сервер...")
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(shutdownCtx); err != nil {
|
||||
slog.Error("Ошибка при остановке", "error", err)
|
||||
}
|
||||
slog.Info("Сервер остановлен")
|
||||
}
|
||||
|
||||
func runMigrate() error {
|
||||
// Пока стандартный log, поменяем потом на slog
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
return fmt.Errorf("конфигурация: %w", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
pool, err := postgres.Connect(ctx, postgres.Config{
|
||||
URL: cfg.DatabaseURL,
|
||||
ConnectTimeout: 5 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("БД: %w", err)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := postgres.RunMigrations(ctx, pool); err != nil {
|
||||
return fmt.Errorf("применение миграций: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) {
|
||||
switch strings.ToLower(cfg.SessionStore) {
|
||||
case "", "postgres":
|
||||
return store.Sessions(), nil, func() {}
|
||||
case "redis":
|
||||
if cfg.CacheURL == "" {
|
||||
log.Fatalf("SESSION_STORE=%s требует CACHE_URL_FILE или CACHE_URL", cfg.SessionStore)
|
||||
}
|
||||
client, err := redisstore.NewClient(cfg.CacheURL)
|
||||
if err != nil {
|
||||
log.Fatalf("redis: %v", err)
|
||||
}
|
||||
ping := func(ctx context.Context) error {
|
||||
return client.Ping(ctx).Err()
|
||||
}
|
||||
return redisstore.NewSessionRepo(client), ping, func() { _ = client.Close() }
|
||||
default:
|
||||
log.Fatalf("SESSION_STORE=%q не поддерживается", cfg.SessionStore)
|
||||
return nil, nil, nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/config"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/storage/postgres"
|
||||
s3store "high-loud-apps-labs/internal/storage/s3"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
"high-loud-apps-labs/internal/worker"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("конфигурация: %v", err)
|
||||
}
|
||||
|
||||
instanceID := os.Getenv("INSTANCE_ID")
|
||||
if instanceID == "" {
|
||||
instanceID, _ = os.Hostname()
|
||||
}
|
||||
tel, err := telemetry.Setup(context.Background(), telemetry.SetupConfig{
|
||||
ServiceName: "messenger-worker",
|
||||
ServiceVersion: "1.6.0",
|
||||
InstanceID: instanceID,
|
||||
Environment: "lab",
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("телеметрия: %v", err)
|
||||
}
|
||||
defer func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if err := tel.Shutdown(ctx); err != nil {
|
||||
log.Printf("остановка телеметрии: %v", err)
|
||||
}
|
||||
}()
|
||||
slog.SetDefault(tel.Logger)
|
||||
|
||||
ctx := context.Background()
|
||||
pool, err := postgres.Connect(ctx, postgres.Config{URL: cfg.DatabaseURL})
|
||||
if err != nil {
|
||||
slog.Error("Ошибка подключения к БД", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
registration, err := postgres.ObservePool(pool)
|
||||
if err != nil {
|
||||
slog.Warn("Не удалось зарегистрировать метрики пула", "error", err)
|
||||
} else {
|
||||
defer registration.Unregister()
|
||||
}
|
||||
|
||||
store := postgres.New(pool)
|
||||
objectStorage, err := s3store.New(ctx, s3store.Config{
|
||||
Endpoint: cfg.S3Endpoint,
|
||||
Bucket: cfg.S3Bucket,
|
||||
AccessKey: cfg.S3AccessKey,
|
||||
SecretKey: cfg.S3SecretKey,
|
||||
UseSSL: cfg.S3UseSSL,
|
||||
Region: cfg.S3Region,
|
||||
})
|
||||
if err != nil {
|
||||
slog.Error("Ошибка подключения к S3", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pipeline := worker.NewPipeline(store.Attachments(), objectStorage, store.Variants(), store.FailedJobs(), store.Outbox())
|
||||
|
||||
// Цикл переподключения к RabbitMQ
|
||||
for {
|
||||
err := runWorker(ctx, cfg, pipeline)
|
||||
if err == nil {
|
||||
break // Нормальное завершение по сигналу
|
||||
}
|
||||
slog.Error("Ошибка соединения worker, перезапуск через 5 секунд", "error", err)
|
||||
time.Sleep(5 * time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
func runWorker(ctx context.Context, cfg *config.Config, pipeline *worker.Pipeline) error {
|
||||
conn, err := amqp.Dial(cfg.RabbitMQURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
ch, err := conn.Channel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer ch.Close()
|
||||
|
||||
ch.Qos(2, 0, false)
|
||||
|
||||
q, err := ch.QueueDeclare("attachments.process", true, false, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
msgs, err := ch.Consume(q.Name, "", false, false, false, false, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
slog.Info("Worker запущен и слушает очередь")
|
||||
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(quit)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-quit:
|
||||
slog.Info("Worker получил сигнал завершения, останавливаемся...")
|
||||
return nil
|
||||
case d, ok := <-msgs:
|
||||
if !ok {
|
||||
return context.Canceled // Канал закрыт (соединение разорвано)
|
||||
}
|
||||
if err := consume(ctx, d, pipeline); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func consume(ctx context.Context, d amqp.Delivery, pipeline *worker.Pipeline) (ackErr error) {
|
||||
headers := make(map[string]string, 2)
|
||||
for _, key := range []string{"traceparent", "tracestate"} {
|
||||
if value, ok := d.Headers[key].(string); ok {
|
||||
headers[key] = value
|
||||
}
|
||||
}
|
||||
ctx, span := telemetry.Tracer("worker").Start(telemetry.ExtractTrace(ctx, headers),
|
||||
"attachments.process process", trace.WithSpanKind(trace.SpanKindConsumer),
|
||||
trace.WithAttributes(attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.destination.name", "attachments.process")))
|
||||
defer func() {
|
||||
if ackErr != nil {
|
||||
span.SetStatus(codes.Error, "ошибка подтверждения доставки")
|
||||
}
|
||||
span.End()
|
||||
}()
|
||||
|
||||
var msg domain.JobMessage
|
||||
if err := json.Unmarshal(d.Body, &msg); err != nil {
|
||||
span.SetStatus(codes.Error, "повреждённое сообщение")
|
||||
slog.ErrorContext(ctx, "Неверный JSON задания", "event", "job.invalid")
|
||||
return d.Nack(false, false)
|
||||
}
|
||||
if err := pipeline.Process(ctx, msg.AttachmentID, msg.PipelineVersion); err != nil {
|
||||
span.SetStatus(codes.Error, "ошибка обработки")
|
||||
slog.ErrorContext(ctx, "Повторная доставка задания", "event", "job.requeue")
|
||||
return d.Nack(false, true)
|
||||
}
|
||||
return d.Ack(false)
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
module high-loud-apps-labs
|
||||
|
||||
go 1.26.1
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6
|
||||
github.com/jackc/pgx/v5 v5.7.1
|
||||
github.com/minio/minio-go/v7 v7.4.1
|
||||
github.com/pressly/goose/v3 v3.22.1
|
||||
github.com/redis/go-redis/v9 v9.22.0
|
||||
go.opentelemetry.io/otel v1.47.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.23.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.47.0
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0
|
||||
go.opentelemetry.io/otel/metric v1.47.0
|
||||
go.opentelemetry.io/otel/sdk v1.47.0
|
||||
go.opentelemetry.io/otel/sdk/log v1.47.0
|
||||
go.opentelemetry.io/otel/sdk/metric v1.47.0
|
||||
go.opentelemetry.io/otel/trace v1.47.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/felixge/httpsnoop v1.1.0 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.72.0 // indirect
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 // indirect
|
||||
go.opentelemetry.io/otel/log v1.47.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.11.0 // indirect
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260928230214-8a89bd6388cc // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260928230214-8a89bd6388cc // indirect
|
||||
google.golang.org/grpc v1.83.2 // indirect
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/bytedance/gopkg v0.1.4 // indirect
|
||||
github.com/bytedance/sonic v1.15.4 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.2 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.7 // indirect
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 // indirect
|
||||
github.com/gen2brain/webp v0.6.4
|
||||
github.com/gin-contrib/sse v1.1.2 // indirect
|
||||
github.com/go-playground/locales v0.14.2 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.2 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.5 // indirect
|
||||
github.com/goccy/go-json v0.11.2 // indirect
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/leodido/go-urn v1.5.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.63.0 // indirect
|
||||
github.com/rabbitmq/amqp091-go v1.16.0
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sethvargo/go-retry v0.3.0 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.2 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.9.1 // indirect
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.21.0
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.72.0
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/arch v0.31.0 // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/protobuf v1.36.12 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
)
|
||||
+267
@@ -0,0 +1,267 @@
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/gopkg v0.1.4 h1:oZnQwnX82KAIWb7033bEwtxvTqXcYMxDBaQxo5JJHWM=
|
||||
github.com/bytedance/gopkg v0.1.4/go.mod h1:v1zWfPm21Fb+OsyXN2VAHdL6TBb2L88anLQgdyje6R4=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
|
||||
github.com/bytedance/sonic v1.15.4 h1:FgtV/4aBHpla9AxuMpuuzVUpa/Cf3izufkxNmnEzdI8=
|
||||
github.com/bytedance/sonic v1.15.4/go.mod h1:8e51yTPdY8M6t+vvGL1c2Y1xL9i+frEeIAQAEl75NUc=
|
||||
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
|
||||
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/bytedance/sonic/loader v0.5.2 h1:0QtP1gevc1OZ6/H8Lb9BRZiCXd1Ftjd3OKuj1T1lBIo=
|
||||
github.com/bytedance/sonic/loader v0.5.2/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM=
|
||||
github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/cloudwego/base64x v0.1.7 h1:NppS+Fgzg5ovhn4NkUXaDT3x9jldgH5ToMCqzBSi2zI=
|
||||
github.com/cloudwego/base64x v0.1.7/go.mod h1:Cu1PV9zfrSf7ET2tIbWbbEy7jO7HHJ13q4X2SQ8aWYg=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
|
||||
github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
|
||||
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15/go.mod h1:azpTcoLcDZRNgFou5j+APrqQx9HqVPWa6ijYQIIVswQ=
|
||||
github.com/gen2brain/webp v0.6.4 h1:SUDdmxADOAiPQ+5ylNmuHhuYf2dOi0KgKZHL5vpVCNU=
|
||||
github.com/gen2brain/webp v0.6.4/go.mod h1:iGWMaCSw7t3I/Cv9llzEKmpnR36S8lS8VL/ZVjxU0JE=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-contrib/sse v1.1.2 h1:MU2fgl1RrdYTMcgJLtz2kJF+vPg3xrqaaKfUUU18tCo=
|
||||
github.com/gin-contrib/sse v1.1.2/go.mod h1:QXzuVkA0YO7o/gun03UI1Q+FTI8ZV/n5t03kIQAI89s=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/locales v0.14.2 h1:d8UmcrM6Nip0hfGZKLGpAvZH37XB4TS0xzK9B56YNCY=
|
||||
github.com/go-playground/locales v0.14.2/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/universal-translator v0.18.2 h1:LCsMLC9RzmbUMNUPVYD15dmcjwYAJhmX8mPZRW4rAVU=
|
||||
github.com/go-playground/universal-translator v0.18.2/go.mod h1:67VZIMp5lQpDWlnStOct22q1bkdJGJqHghbOtmkawxk=
|
||||
github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
|
||||
github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
|
||||
github.com/go-playground/validator/v10 v10.30.5 h1:YyCXvVShZbs2Sm3Mb53eNOlhRXctSOzW5QJAouCTZL4=
|
||||
github.com/go-playground/validator/v10 v10.30.5/go.mod h1:wEqiaov48pXX1kjhc3Da8y0M0Dtg/BK7gurFBLgwFrQ=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-json v0.11.2 h1:jdZv93Tt4ioR8yW1CoNsvSxrcZlCXAUU1aZXN7gpXUA=
|
||||
github.com/goccy/go-json v0.11.2/go.mod h1:3NdmfEkZlB7YI5UFw/qdFKq8XN1aiWR0YyRPWZNQltY=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0 h1:/Tnpcb2E0Pz/tN9s3bfEY2Q8ePCEX9iuS+cneUwncnw=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.30.0/go.mod h1:zOBXOsUaBSjKgmH4OGzV1esUpR3oUSCPYVd2cUBjKYY=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 h1:D/V0gu4zQ3cL2WKeVNVM4r2gLxGGf6McLwgXzRTo2RQ=
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs=
|
||||
github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/leodido/go-urn v1.5.0 h1:pLqT2kq1zpHW/1D18QMjMpdtX7cekxqtJJjg5ANyWw0=
|
||||
github.com/leodido/go-urn v1.5.0/go.mod h1:9BORnCDhdPBJNDEX+w1bJisa8yOKYi116VeO96s4ifE=
|
||||
github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4=
|
||||
github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
|
||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
|
||||
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pressly/goose/v3 v3.22.1 h1:2zICEfr1O3yTP9BRZMGPj7qFxQ+ik6yeo+z1LMuioLc=
|
||||
github.com/pressly/goose/v3 v3.22.1/go.mod h1:xtMpbstWyCpyH+0cxLTMCENWBG+0CSxvTsXhW95d5eo=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
github.com/quic-go/quic-go v0.63.0 h1:LIFGHI4PFUhhw2dDD1ARHdCff143ffMHwZtbnbuJ78A=
|
||||
github.com/quic-go/quic-go v0.63.0/go.mod h1:RAro2j2yN9a9EiPACLHT9IB2NXCvGQmmo/alT0yYI0w=
|
||||
github.com/rabbitmq/amqp091-go v1.16.0 h1:1O8oQqnj9hLACoo6CVMgn57z2pLWXvgemhh4GsquV+Y=
|
||||
github.com/rabbitmq/amqp091-go v1.16.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||
github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
|
||||
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
|
||||
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
github.com/ugorji/go/codec v1.3.2 h1:zkEASHHyEClGeURfgNT9PJZVfAbs9oEX9QXggwWNJbc=
|
||||
github.com/ugorji/go/codec v1.3.2/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.mongodb.org/mongo-driver/v2 v2.9.1 h1:jewiFs2m1/VOQp8qhFshX6hWZ+EAXDhZHXExAUMcOgQ=
|
||||
go.mongodb.org/mongo-driver/v2 v2.9.1/go.mod h1:SHKN0IWkKmEVGHLjXnni6s4wPKX4v86FTgOeJJFuXcA=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.21.0 h1:/CdgE52i1OEPHhsGUqrggYNRaZy4v4YgVenGd52dRBI=
|
||||
go.opentelemetry.io/contrib/bridges/otelslog v0.21.0/go.mod h1:9pBdbVbuehIDzxN4accnzzxEUPT8Nq7grbZMo+J1VN4=
|
||||
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.72.0 h1:Vgm7vrC26vvecffpDcy2UINqBvASUg5qkfZle3Yrkug=
|
||||
go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin v0.72.0/go.mod h1:zgCChxie/dA3qG7IgZGswax6voxkOR4+33msLT/RgNg=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.72.0 h1:LxwW/9ctSCv+QkE/cLR7M91ZIkXNMqJtEMi1vCw9U8s=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.72.0/go.mod h1:tOsftB4SslBwwErVEPaenU2RpThXWPIU8DoJHEC4dyw=
|
||||
go.opentelemetry.io/otel v1.47.0 h1:j7ALJ/zgkS7Z6aeJW09p8VC9804bC+PpeTfCD4XPnOM=
|
||||
go.opentelemetry.io/otel v1.47.0/go.mod h1:8wS9O2qfXrYrzp6hIF/HOYJJf/wIhFPhR2xLuP+iXQU=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.23.0 h1:5WVp40k2fJHuc5BtNTgKk+EugKFT8Eg3lywFfCPI/nE=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.23.0/go.mod h1:59nH0RYCGHRlwt15WyioeSYINBbuObeBpTowPhEKWHY=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.47.0 h1:7kszRN4Ze5GeAkUnBjBSUIunVBMA+mZIG04auueqguM=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.47.0/go.mod h1:CLzakYy8jOdkBfcveCWYkeKJxujcF4xPhh2Z7IfE98U=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0 h1:julhjPeUH/q/7hinbSdDdqt5h7Zw9YWmRlWRhI0jd54=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.47.0/go.mod h1:Ao2mz688LH/tFf0yMAenidq6k2YNSx6SIY2q6jDACck=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0 h1:aXZXsZ012wOgVkqaiQv+Z/d8V0xKjmg70F6m4CE94lc=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.47.0/go.mod h1:ziu1gUIJhAaxM3EV1e3Ralk5AAyL1UOYz3lJJ4VSI38=
|
||||
go.opentelemetry.io/otel/log v1.47.0 h1:cOTS1CcLbSQeZKanGJ+0JpF/+t4PELi3O3bbl2lqCcI=
|
||||
go.opentelemetry.io/otel/log v1.47.0/go.mod h1:9byitSQ5pLC6PpqwGXjqdMKya6ZTswHRZh2vvXT33nw=
|
||||
go.opentelemetry.io/otel/metric v1.47.0 h1:4PptaldXx3Eat1XjMZ68pPJEs5wrhlemctZE9a3UdWY=
|
||||
go.opentelemetry.io/otel/metric v1.47.0/go.mod h1:ADGSXxRrXM6bjbvLo535EstVFlPpPYZm4LBKixjDHwU=
|
||||
go.opentelemetry.io/otel/metric/x v0.69.0 h1:DjRLr15H83v+hCW7JA9NoJvOkYTtmq5YoDRbe9deYpM=
|
||||
go.opentelemetry.io/otel/metric/x v0.69.0/go.mod h1:uVvsMPMFFyj/HUQfrUnH3JjnOQ1dwFDorgFLRBasM0k=
|
||||
go.opentelemetry.io/otel/sdk v1.47.0 h1:zWXEr4j2lFefG87TU6Yg8a7ngfohIKFZHKp0Hf5hC6I=
|
||||
go.opentelemetry.io/otel/sdk v1.47.0/go.mod h1:VUc24kiOeoGsxG8G9ULx3fWKvB7jMhnGE8Oi607lgR0=
|
||||
go.opentelemetry.io/otel/sdk/log v1.47.0 h1:W4N/ZfgpkRRnmARnqYoQ6BrWA0mrj8IzVDNLlpmUJZY=
|
||||
go.opentelemetry.io/otel/sdk/log v1.47.0/go.mod h1:Wu74Z2EmOKXi2FleSNAb0Hyau2G+r5kubSEMJw5aAMs=
|
||||
go.opentelemetry.io/otel/sdk/log/logtest v0.23.0 h1:j3pm42QCrKYYLLkI6OrV1QHFkUfDA6/4EP+Y1PzggLg=
|
||||
go.opentelemetry.io/otel/sdk/log/logtest v0.23.0/go.mod h1:zt0gEnuTZgDvqvczHwYswz93eqMaIr6upTTsdV/HUlU=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.47.0 h1:lfISg2j93VT6yqdk9OfUaZmw/GfcZqCCV3jdXtsPnKw=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.47.0/go.mod h1:ypLp+mW1Nt2x+Szt3b5/i1syodyts49lMOwxpDI3VGw=
|
||||
go.opentelemetry.io/otel/trace v1.47.0 h1:JOjX/Oci8K94QHddo+bbfya/Ai/nf6/dt9ZfrFNWSrM=
|
||||
go.opentelemetry.io/otel/trace v1.47.0/go.mod h1:jNaSLa2PZEYFG6fRjJABAu+bw4FS08uDmPg28lTghu0=
|
||||
go.opentelemetry.io/proto/otlp v1.11.0 h1:5rrYs0Ykyj50sdU/JU0x8etU+LubXWb+gED6TbEdMIk=
|
||||
go.opentelemetry.io/proto/otlp v1.11.0/go.mod h1:SmVizdCOAm3XBtG1g1NnOdhW6jtddT72hLMhv8VwA8E=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/arch v0.31.0 h1:22MlEb14/O/EPCYHFxsDdv5TuLD5dMjT5e2QeJw4ULk=
|
||||
golang.org/x/arch v0.31.0/go.mod h1:KcJSod3cqT2dKcjBxqTyGfbumNikqU9p5tHJinPJnuY=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
gonum.org/v1/gonum v0.17.0 h1:VbpOemQlsSMrYmn7T2OUvQ4dqxQXU+ouZFQsZOx50z4=
|
||||
gonum.org/v1/gonum v0.17.0/go.mod h1:El3tOrEuMpv2UdMrbNlKEh9vd86bmQ6vqIcDwxEOc1E=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260928230214-8a89bd6388cc h1:rZHRz0ogq4Pid1IKtA/ivHi8XONVF5tqUQXA2TtgpkE=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260928230214-8a89bd6388cc/go.mod h1:K/n0XLazCJHjD73YuMyNJsN38BPPy1kfFnKxUXaThFU=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260928230214-8a89bd6388cc h1:4bNTbnb44EqGVy9HaQxSY2jnafSUdgV3qHtedvNpDKg=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260928230214-8a89bd6388cc/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
|
||||
google.golang.org/grpc v1.83.2 h1:EManeRomTObA0BU7I8vXgg/78uE5MJ9M8B39EX2WscU=
|
||||
google.golang.org/grpc v1.83.2/go.mod h1:YPI1hK3kDked6iHvgX3tR0y+nX/qpMFKhPgFsokw1S8=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
|
||||
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI=
|
||||
modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4=
|
||||
modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4=
|
||||
modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo=
|
||||
modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E=
|
||||
modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU=
|
||||
modernc.org/sqlite v1.33.0 h1:WWkA/T2G17okiLGgKAj4/RMIvgyMT19yQ038160IeYk=
|
||||
modernc.org/sqlite v1.33.0/go.mod h1:9uQ9hF/pCZoYZK73D/ud5Z7cIRIILSZI8NdIemVMTX8=
|
||||
modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA=
|
||||
modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
@@ -0,0 +1,130 @@
|
||||
package broker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type Publisher interface {
|
||||
Publish(ctx context.Context, routingKey string, body []byte) error
|
||||
Close() error
|
||||
}
|
||||
|
||||
type RabbitPublisher struct {
|
||||
url string
|
||||
mu sync.Mutex
|
||||
conn *amqp.Connection
|
||||
ch *amqp.Channel
|
||||
}
|
||||
|
||||
func NewRabbitPublisher(url string) (*RabbitPublisher, error) {
|
||||
p := &RabbitPublisher{url: url}
|
||||
if err := p.connect(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p, nil
|
||||
}
|
||||
|
||||
func (p *RabbitPublisher) connect() error {
|
||||
var err error
|
||||
p.conn, err = amqp.Dial(p.url)
|
||||
if err != nil {
|
||||
return fmt.Errorf("подключение к rabbitmq: %w", err)
|
||||
}
|
||||
|
||||
p.ch, err = p.conn.Channel()
|
||||
if err != nil {
|
||||
return fmt.Errorf("открытие канала rabbitmq: %w", err)
|
||||
}
|
||||
|
||||
if err := p.ch.Confirm(false); err != nil {
|
||||
return fmt.Errorf("включение режима confirm: %w", err)
|
||||
}
|
||||
|
||||
err = p.ch.ExchangeDeclare("attachments", "direct", true, false, false, false, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("объявление exchange: %w", err)
|
||||
}
|
||||
|
||||
_, err = p.ch.QueueDeclare("attachments.process", true, false, false, false, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("объявление очереди: %w", err)
|
||||
}
|
||||
|
||||
err = p.ch.QueueBind("attachments.process", "process", "attachments", false, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("привязка очереди: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *RabbitPublisher) Publish(ctx context.Context, routingKey string, body []byte) (publishErr error) {
|
||||
ctx, span := telemetry.Tracer("broker").Start(ctx, "attachments publish",
|
||||
trace.WithSpanKind(trace.SpanKindProducer), trace.WithAttributes(
|
||||
attribute.String("messaging.system", "rabbitmq"),
|
||||
attribute.String("messaging.destination.name", "attachments"),
|
||||
))
|
||||
defer func() {
|
||||
if publishErr != nil {
|
||||
span.SetStatus(codes.Error, "публикация не подтверждена")
|
||||
}
|
||||
span.End()
|
||||
}()
|
||||
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
|
||||
if p.conn == nil || p.conn.IsClosed() {
|
||||
if err := p.connect(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
headers := amqp.Table{}
|
||||
for key, value := range telemetry.TraceHeaders(ctx) {
|
||||
headers[key] = value
|
||||
}
|
||||
confirmation, err := p.ch.PublishWithDeferredConfirmWithContext(ctx, "attachments", routingKey, false, false, amqp.Publishing{
|
||||
Headers: headers,
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
Body: body,
|
||||
Timestamp: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
waitCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
acked, err := confirmation.WaitContext(waitCtx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("ожидание подтверждения RabbitMQ: %w", err)
|
||||
}
|
||||
if !acked {
|
||||
return fmt.Errorf("сообщение отклонено брокером")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *RabbitPublisher) Close() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
if p.ch != nil {
|
||||
_ = p.ch.Close()
|
||||
}
|
||||
if p.conn != nil {
|
||||
return p.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port string
|
||||
DatabaseURL string
|
||||
SessionStore string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
ShutdownDrain time.Duration
|
||||
|
||||
S3Endpoint string
|
||||
S3Bucket string
|
||||
S3UseSSL bool
|
||||
S3Region string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
|
||||
MaxUploadBytes int64
|
||||
MaxImagePixels int64
|
||||
|
||||
// ReconcilerEnabled bool
|
||||
ReconcilerInterval time.Duration
|
||||
ReconcilerGracePeriod time.Duration
|
||||
|
||||
RabbitMQURL string
|
||||
OutboxInterval time.Duration
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
dsn, err := readSecret("DATABASE_URL_FILE", "DATABASE_URL")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("database url: %w", err)
|
||||
}
|
||||
|
||||
cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("cache url: %w", err)
|
||||
}
|
||||
|
||||
ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err)
|
||||
}
|
||||
if ttlSeconds <= 0 {
|
||||
return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
idemSeconds, err := readInt("IDEMPOTENCY_TTL_SECONDS", 86400)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS: %w", err)
|
||||
}
|
||||
if idemSeconds <= 0 {
|
||||
return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
drainSeconds, err := readInt("SHUTDOWN_DRAIN_SECONDS", 3)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS: %w", err)
|
||||
}
|
||||
if drainSeconds < 0 {
|
||||
return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS не может быть отрицательным")
|
||||
}
|
||||
|
||||
store := strings.ToLower(getenv("SESSION_STORE", "postgres"))
|
||||
switch store {
|
||||
case "postgres", "redis", "valkey":
|
||||
default:
|
||||
return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store)
|
||||
}
|
||||
|
||||
s3Endpoint := getenv("S3_ENDPOINT", "")
|
||||
s3Bucket := getenv("S3_BUCKET", "")
|
||||
s3UseSSL, err := readBool("S3_USE_SSL", false)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("S3_USE_SSL: %w", err)
|
||||
}
|
||||
s3Region := getenv("S3_REGION", "us-east-1")
|
||||
|
||||
s3AccessKey, err := readSecretOptional("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 access key: %w", err)
|
||||
}
|
||||
s3SecretKey, err := readSecretOptional("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 secret key: %w", err)
|
||||
}
|
||||
|
||||
maxUploadBytes, err := readInt64("MAX_UPLOAD_BYTES", 10485760)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MAX_UPLOAD_BYTES: %w", err)
|
||||
}
|
||||
if maxUploadBytes <= 0 {
|
||||
return nil, fmt.Errorf("MAX_UPLOAD_BYTES должен быть положительным")
|
||||
}
|
||||
|
||||
maxImagePixels, err := readInt64("MAX_IMAGE_PIXELS", 40000000)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("MAX_IMAGE_PIXELS: %w", err)
|
||||
}
|
||||
if maxImagePixels <= 0 {
|
||||
return nil, fmt.Errorf("MAX_IMAGE_PIXELS должен быть положительным")
|
||||
}
|
||||
|
||||
// reconcilerEnabled, err := readBool("RECONCILER_ENABLED", true)
|
||||
// if err != nil {
|
||||
// return nil, fmt.Errorf("RECONCILER_ENABLED: %w", err)
|
||||
// }
|
||||
reconcilerIntervalSec, err := readInt("RECONCILER_INTERVAL_SECONDS", 300)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS: %w", err)
|
||||
}
|
||||
if reconcilerIntervalSec <= 0 {
|
||||
return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS должен быть положительным")
|
||||
}
|
||||
reconcilerGraceSec, err := readInt("RECONCILER_GRACE_SECONDS", 3600)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS: %w", err)
|
||||
}
|
||||
if reconcilerGraceSec <= 0 {
|
||||
return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS должен быть положительным")
|
||||
}
|
||||
|
||||
rabbitURL, err := readSecretOptional("RABBITMQ_URL_FILE", "RABBITMQ_URL")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("rabbitmq url: %w", err)
|
||||
}
|
||||
|
||||
outboxMs, err := readInt("OUTBOX_INTERVAL_MS", 1000)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("OUTBOX_INTERVAL_MS: %w", err)
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Host: getenv("APP_HOST", "0.0.0.0"),
|
||||
Port: getenv("APP_PORT", "8080"),
|
||||
DatabaseURL: dsn,
|
||||
SessionStore: store,
|
||||
CacheURL: cacheURL,
|
||||
SessionTTL: time.Duration(ttlSeconds) * time.Second,
|
||||
IdempotencyTTL: time.Duration(idemSeconds) * time.Second,
|
||||
ShutdownDrain: time.Duration(drainSeconds) * time.Second,
|
||||
|
||||
S3Endpoint: s3Endpoint,
|
||||
S3Bucket: s3Bucket,
|
||||
S3UseSSL: s3UseSSL,
|
||||
S3Region: s3Region,
|
||||
S3AccessKey: s3AccessKey,
|
||||
S3SecretKey: s3SecretKey,
|
||||
|
||||
MaxUploadBytes: maxUploadBytes,
|
||||
MaxImagePixels: maxImagePixels,
|
||||
|
||||
// ReconcilerEnabled: reconcilerEnabled,
|
||||
ReconcilerInterval: time.Duration(reconcilerIntervalSec) * time.Second,
|
||||
ReconcilerGracePeriod: time.Duration(reconcilerGraceSec) * time.Second,
|
||||
|
||||
RabbitMQURL: rabbitURL,
|
||||
OutboxInterval: time.Duration(outboxMs) * time.Millisecond,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Config) S3Configured() bool {
|
||||
return c.S3Endpoint != "" &&
|
||||
c.S3Bucket != "" &&
|
||||
c.S3AccessKey != "" &&
|
||||
c.S3SecretKey != ""
|
||||
}
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func readInt(key string, def int) (int, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("не целое число: %q", v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func readInt64(key string, def int64) (int64, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
n, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("не целое число: %q", v)
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func readBool(key string, def bool) (bool, error) {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def, nil
|
||||
}
|
||||
b, err := strconv.ParseBool(v)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("не bool: %q", v)
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func readSecret(fileEnv, envKey string) (string, error) {
|
||||
v, err := readSecretOptional(fileEnv, envKey)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if v == "" {
|
||||
return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func readSecretOptional(fileEnv, envKey string) (string, error) {
|
||||
if path := os.Getenv(fileEnv); path != "" {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("чтение %s=%s: %w", fileEnv, path, err)
|
||||
}
|
||||
s := strings.TrimSpace(string(b))
|
||||
if s == "" {
|
||||
return "", fmt.Errorf("файл %s пуст", path)
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
return os.Getenv(envKey), nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type AttachmentVariant struct {
|
||||
AttachmentID string
|
||||
Name string
|
||||
ObjectKey string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Width int
|
||||
Height int
|
||||
SHA256 []byte
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
const (
|
||||
VariantThumbnail = "thumbnail"
|
||||
VariantCrop = "crop"
|
||||
VariantOptimized = "optimized"
|
||||
)
|
||||
@@ -0,0 +1,70 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Health struct {
|
||||
Status string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string // в DTO UserResponse его не будет
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
ChatID string
|
||||
SenderID string
|
||||
Text string
|
||||
AttachmentIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
TokenHash []byte
|
||||
UserID string
|
||||
ExpiresAt time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
ID string
|
||||
ChatID string
|
||||
OwnerID string
|
||||
Filename string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 []byte
|
||||
ObjectKey string
|
||||
Status AttachmentStatus
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
|
||||
Attempts int // авторитетный счётчик попыток (в БД)
|
||||
NextAttemptAt *time.Time // "не раньше"; nil = можно брать сразу
|
||||
LockedUntil *time.Time // lease; nil = не арендовано
|
||||
PipelineVersion int // guard в CompleteJob; в БД default 1
|
||||
StagingKey string // S3-ключ оригинала до 202; "" в lab4-пути
|
||||
|
||||
Variants []AttachmentVariant
|
||||
}
|
||||
@@ -0,0 +1,197 @@
|
||||
package domain
|
||||
|
||||
// Этот файл — единственный источник доменных ошибок.
|
||||
// Правила:
|
||||
// 1. sentinel-ошибки (ErrXxx) нужны для errors.Is.
|
||||
// 2. типы *XxxError нужны для errors.As и для поля code в JSON.
|
||||
// 3. У каждого типа есть Code() ErrorCode — через него DTO маппит
|
||||
// в HTTP-код (StatusFromError в httpapi/dto/error.go).
|
||||
// 4. В Error() пишем только то, что безопасно показать клиенту:
|
||||
// без SQL, stack trace, путей, credential'ов.
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// ===================================================================
|
||||
// ErrorCode — машинный код для JSON {"error":{"code":"..."}}
|
||||
// ===================================================================
|
||||
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
CodeInvalidRequest ErrorCode = "invalid_request" // 400
|
||||
CodeUnauthorized ErrorCode = "unauthorized" // 401
|
||||
CodeNotFound ErrorCode = "not_found" // 404
|
||||
CodeConflict ErrorCode = "conflict" // 409
|
||||
CodePayloadTooLarge ErrorCode = "payload_too_large" // 413
|
||||
CodeUnsupportedMediaType ErrorCode = "unsupported_media_type" // 415
|
||||
CodeUnavailable ErrorCode = "unavailable" // 503
|
||||
CodeInternalError ErrorCode = "internal_error" // 500
|
||||
)
|
||||
|
||||
// ===================================================================
|
||||
// Sentinel-ошибки (errors.Is)
|
||||
// ===================================================================
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("не найдено")
|
||||
ErrAlreadyExists = errors.New("уже существует")
|
||||
ErrInvalidInput = errors.New("некорректные данные")
|
||||
ErrUnauthorized = errors.New("требуется аутентификация")
|
||||
ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого")
|
||||
ErrPayloadTooLarge = errors.New("превышен допустимый размер")
|
||||
ErrUnavailable = errors.New("сервис недоступен")
|
||||
ErrConflict = errors.New("конфликт")
|
||||
)
|
||||
|
||||
// ===================================================================
|
||||
// 404 Not Found
|
||||
// ===================================================================
|
||||
|
||||
type NotFoundError struct {
|
||||
Entity string
|
||||
ID string
|
||||
}
|
||||
|
||||
func (e *NotFoundError) Error() string {
|
||||
return fmt.Sprintf("%s с id %q не найден", e.Entity, e.ID)
|
||||
}
|
||||
func (e *NotFoundError) Unwrap() error { return ErrNotFound }
|
||||
func (e *NotFoundError) Code() ErrorCode { return CodeNotFound }
|
||||
|
||||
// ===================================================================
|
||||
// 409 Conflict
|
||||
// ===================================================================
|
||||
|
||||
// AlreadyExistsError — конфликт уникальности (например, username занят).
|
||||
// Отдельно от ConflictError: другой смысл, но оба дают HTTP 409.
|
||||
type AlreadyExistsError struct {
|
||||
Entity string
|
||||
Field string
|
||||
Value string
|
||||
}
|
||||
|
||||
func (e *AlreadyExistsError) Error() string {
|
||||
return fmt.Sprintf("%s с %s %q уже существует", e.Entity, e.Field, e.Value)
|
||||
}
|
||||
func (e *AlreadyExistsError) Unwrap() error { return ErrAlreadyExists }
|
||||
func (e *AlreadyExistsError) Code() ErrorCode { return CodeConflict }
|
||||
|
||||
// IdempotencyConflictError — тот же Idempotency-Key, но другое тело запроса.
|
||||
type IdempotencyConflictError struct {
|
||||
Key string
|
||||
}
|
||||
|
||||
func (e *IdempotencyConflictError) Error() string {
|
||||
return "Idempotency-Key " + e.Key + " уже использован с другим телом запроса"
|
||||
}
|
||||
func (e *IdempotencyConflictError) Unwrap() error { return ErrAlreadyExists }
|
||||
func (e *IdempotencyConflictError) Code() ErrorCode { return CodeConflict }
|
||||
|
||||
// ConflictError — конфликт бизнес-состояния, а не уникальности.
|
||||
// Примеры: "вложение ещё не готово", "нельзя перевести failed-вложение в ready".
|
||||
// Reason — короткая безопасная фраза, идёт клиенту как message.
|
||||
type ConflictError struct{ Reason string }
|
||||
|
||||
func (e *ConflictError) Error() string { return e.Reason }
|
||||
func (e *ConflictError) Unwrap() error { return ErrConflict }
|
||||
func (e *ConflictError) Code() ErrorCode { return CodeConflict }
|
||||
|
||||
// ===================================================================
|
||||
// 400 Bad Request
|
||||
// ===================================================================
|
||||
|
||||
// ValidationError — неверное поле или формат.
|
||||
type ValidationError struct {
|
||||
Field string
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *ValidationError) Error() string {
|
||||
return fmt.Sprintf("ошибка валидации поля %q: %s", e.Field, e.Message)
|
||||
}
|
||||
func (e *ValidationError) Unwrap() error { return ErrInvalidInput }
|
||||
func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest }
|
||||
|
||||
// ===================================================================
|
||||
// 401 Unauthorized
|
||||
// ===================================================================
|
||||
|
||||
// UnauthorizedError — нет/просрочен/отозван токен, неверные credentials.
|
||||
// Reason — только для логов, в JSON не попадает (по контракту все 401
|
||||
// должны иметь одинаковое сообщение, чтобы не перебирать username).
|
||||
type UnauthorizedError struct {
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (e *UnauthorizedError) Error() string {
|
||||
return "требуется аутентификация"
|
||||
}
|
||||
func (e *UnauthorizedError) Unwrap() error { return ErrUnauthorized }
|
||||
func (e *UnauthorizedError) Code() ErrorCode { return CodeUnauthorized }
|
||||
|
||||
// ===================================================================
|
||||
// 415 Unsupported Media Type
|
||||
// ===================================================================
|
||||
|
||||
// UnsupportedMediaTypeError — неверный Content-Type.
|
||||
//
|
||||
// ВАЖНО: сейчас текст жёстко про application/json. Для lab4 (вложения)
|
||||
// ожидается image/png или image/jpeg. Прежде чем использовать этот тип
|
||||
// в AttachmentHandler, добавить поле Expected string (дефолт application/json)
|
||||
// или сделать отдельный тип — не переиспользовать как есть, иначе клиент
|
||||
// увидит неверную подсказку.
|
||||
type UnsupportedMediaTypeError struct {
|
||||
MediaType string
|
||||
Expected string
|
||||
}
|
||||
|
||||
func (e *UnsupportedMediaTypeError) Error() string {
|
||||
expected := e.Expected
|
||||
if expected == "" {
|
||||
expected = "application/json"
|
||||
}
|
||||
if e.MediaType == "" {
|
||||
return "требуется Content-Type: " + expected
|
||||
}
|
||||
return fmt.Sprintf("неподдерживаемый Content-Type %q; требуется %s", e.MediaType, expected)
|
||||
}
|
||||
func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType }
|
||||
func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType }
|
||||
|
||||
// ===================================================================
|
||||
// 413 Payload Too Large
|
||||
// ===================================================================
|
||||
|
||||
// PayloadTooLargeError — тело больше лимита.
|
||||
// Message опционален: если пуст — формируется из Limit.
|
||||
type PayloadTooLargeError struct {
|
||||
Limit int64
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e *PayloadTooLargeError) Error() string {
|
||||
if e.Message != "" {
|
||||
return e.Message
|
||||
}
|
||||
return fmt.Sprintf("превышен лимит %d байт", e.Limit)
|
||||
}
|
||||
func (e *PayloadTooLargeError) Unwrap() error { return ErrPayloadTooLarge }
|
||||
func (e *PayloadTooLargeError) Code() ErrorCode { return CodePayloadTooLarge }
|
||||
|
||||
// ===================================================================
|
||||
// 503 Service Unavailable
|
||||
// ===================================================================
|
||||
|
||||
// UnavailableError — недоступна критичная зависимость (БД, кэш, S3).
|
||||
// Reason — только для логов; клиент видит общее сообщение по контракту,
|
||||
// чтобы не раскрывать имена сервисов и топологию.
|
||||
type UnavailableError struct{ Reason string }
|
||||
|
||||
func (e *UnavailableError) Error() string {
|
||||
return "сервис временно недоступен"
|
||||
}
|
||||
func (e *UnavailableError) Unwrap() error { return ErrUnavailable }
|
||||
func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable }
|
||||
@@ -0,0 +1,17 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
// FailedJob — реестр окончательно проваленных задач (DLQ-сторона).
|
||||
// Заполняется worker'ом после исчерпания retry или при permanent-ошибке.
|
||||
// Читается для операторского просмотра и для явного replay (Этап 5).
|
||||
type FailedJob struct {
|
||||
ID string
|
||||
AttachmentID string
|
||||
ReasonCode string // из jobcodes.go
|
||||
Attempts int
|
||||
LastErrorAt time.Time
|
||||
ResolvedAt *time.Time // nil = не разобрана
|
||||
Resolution string // "replayed", "manual_fix", ...
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
)
|
||||
|
||||
type IdempotencyScope struct {
|
||||
UserID string
|
||||
Method string
|
||||
Path string
|
||||
Key string
|
||||
}
|
||||
|
||||
type CreateIdempotentResult struct {
|
||||
Status int
|
||||
Body []byte
|
||||
Replayed bool
|
||||
}
|
||||
|
||||
func RequestHash(text string, attachmentIDs []string) []byte {
|
||||
if len(attachmentIDs) == 0 {
|
||||
sum := sha256.Sum256([]byte(text))
|
||||
return sum[:]
|
||||
}
|
||||
h := sha256.New()
|
||||
h.Write([]byte(text))
|
||||
h.Write([]byte{0})
|
||||
for _, id := range attachmentIDs {
|
||||
h.Write([]byte(id))
|
||||
h.Write([]byte{0})
|
||||
}
|
||||
return h.Sum(nil)
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package domain
|
||||
|
||||
type JobMessage struct {
|
||||
AttachmentID string `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
const (
|
||||
JobErrorDecodeFailed = "decode_failed"
|
||||
JobErrorTooLarge = "too_large"
|
||||
JobErrorUnsupportedFormat = "unsupported_format"
|
||||
JobErrorStorage = "storage_error"
|
||||
JobErrorInternal = "internal_error"
|
||||
JobErrorRetryExhausted = "retry_exhausted"
|
||||
)
|
||||
@@ -0,0 +1,29 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
const (
|
||||
OutboxAggregateAttachment = "attachment"
|
||||
OutboxEventProcess = "attachment.process"
|
||||
)
|
||||
|
||||
// OutboxEntry — намерение опубликовать сообщение, записанное
|
||||
// в одной транзакции с бизнес-изменением (transactional outbox).
|
||||
//
|
||||
// Инварианты:
|
||||
// - PublishedAt == nil => ещё не опубликовано, publisher обязан дослать;
|
||||
// - PublishedAt != nil => RabbitMQ принял сообщение,
|
||||
// а не «мы вызвали Publish». At-least-once: повторная доставка
|
||||
// возможна и нормальна, гасится идемпотентностью consumer'а.
|
||||
//
|
||||
type OutboxEntry struct {
|
||||
ID string
|
||||
AggregateType string
|
||||
AggregateID string
|
||||
EventType string
|
||||
Payload []byte
|
||||
Headers map[string]string
|
||||
CreatedAt time.Time
|
||||
PublishedAt *time.Time
|
||||
Attempts int
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
Create(ctx context.Context, user *User) error
|
||||
GetByID(ctx context.Context, id string) (*User, error)
|
||||
GetByUsername(ctx context.Context, username string) (*User, error)
|
||||
}
|
||||
|
||||
type ChatRepository interface {
|
||||
Create(ctx context.Context, chat *Chat) error
|
||||
GetByID(ctx context.Context, id string) (*Chat, error)
|
||||
ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error)
|
||||
}
|
||||
|
||||
type MessageRepository interface {
|
||||
Create(ctx context.Context, message *Message) error
|
||||
ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error)
|
||||
CreateIdempotent(
|
||||
ctx context.Context,
|
||||
scope IdempotencyScope,
|
||||
requestHash []byte,
|
||||
fresh *Message,
|
||||
freshResponse []byte,
|
||||
ttl time.Duration,
|
||||
) (*CreateIdempotentResult, error)
|
||||
}
|
||||
|
||||
type SessionRepository interface {
|
||||
Create(ctx context.Context, session *Session) error
|
||||
GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error)
|
||||
DeleteByTokenHash(ctx context.Context, tokenHash []byte) error
|
||||
}
|
||||
|
||||
type AttachmentRepository interface {
|
||||
Create(ctx context.Context, a *Attachment) error
|
||||
GetByID(ctx context.Context, id string) (*Attachment, error)
|
||||
MarkReady(ctx context.Context, id string) error
|
||||
MarkFailed(ctx context.Context, id string, errorCode string) error
|
||||
ExistingKeys(ctx context.Context, keys []string) ([]string, error)
|
||||
CreateInTx(ctx context.Context, tx Tx, a *Attachment) error
|
||||
ResetForReplay(ctx context.Context, tx Tx, id string) error
|
||||
|
||||
ListExpiredLeases(ctx context.Context, limit int) ([]Attachment, error)
|
||||
ResetLease(ctx context.Context, id string) error
|
||||
ListStagingToCleanup(ctx context.Context, limit int) ([]Attachment, error)
|
||||
ClearStagingKey(ctx context.Context, id string) error
|
||||
}
|
||||
|
||||
type OutboxRepository interface {
|
||||
Enqueue(ctx context.Context, tx Tx, e *OutboxEntry) error
|
||||
ClaimBatch(ctx context.Context, limit int) ([]OutboxEntry, error)
|
||||
MarkPublished(ctx context.Context, ids []string) error
|
||||
EnqueueDirect(ctx context.Context, e *OutboxEntry) error
|
||||
}
|
||||
|
||||
type AttachmentJobRepository interface {
|
||||
ClaimForProcessing(ctx context.Context, id string, version int, lease time.Duration) (*Attachment, error)
|
||||
CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []AttachmentVariant) (bool, error)
|
||||
FailJob(ctx context.Context, id string, errorCode string) error
|
||||
ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error
|
||||
}
|
||||
|
||||
type AttachmentVariantRepository interface {
|
||||
Upsert(ctx context.Context, tx Tx, v *AttachmentVariant) error
|
||||
ListByAttachment(ctx context.Context, id string) ([]AttachmentVariant, error)
|
||||
}
|
||||
|
||||
type FailedJobRepository interface {
|
||||
Record(ctx context.Context, f *FailedJob) error
|
||||
Resolve(ctx context.Context, attachmentID string, resolution string) error
|
||||
ResolveInTx(ctx context.Context, tx Tx, id string) error
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
LastModified time.Time
|
||||
Size int64
|
||||
}
|
||||
|
||||
type ObjectStorage interface {
|
||||
Ping(ctx context.Context) error
|
||||
Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
|
||||
Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
List(ctx context.Context, prefix string) ([]ObjectInfo, error)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package domain
|
||||
|
||||
import "context"
|
||||
|
||||
// Tx — минимальная абстракция транзакции, нужная domain-слою.
|
||||
// Реализуется адаптером над pgx.Tx в storage/postgres, поэтому
|
||||
// domain не зависит от драйвера БД.
|
||||
//
|
||||
// Зачем: transactional outbox требует, чтобы бизнес-изменение
|
||||
// (например, INSERT attachments) и INSERT outbox шли в одной
|
||||
// транзакции. Без передачи транзакции в репозиторий эту гарантию
|
||||
// не выразить.
|
||||
//
|
||||
// Метод один — Exec. Расширять по мере появления реальных
|
||||
// потребителей, а не на будущее.
|
||||
type Tx interface {
|
||||
Exec(ctx context.Context, sql string, args ...any) error
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type VariantResponse struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type AttachmentResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"`
|
||||
Variants []VariantResponse `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
UpdatedAt string `json:"updated_at"`
|
||||
}
|
||||
|
||||
func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse {
|
||||
variants := make([]VariantResponse, len(a.Variants))
|
||||
for i, v := range a.Variants {
|
||||
variants[i] = VariantResponse{
|
||||
Name: v.Name,
|
||||
ContentType: v.ContentType,
|
||||
SizeBytes: v.SizeBytes,
|
||||
Width: v.Width,
|
||||
Height: v.Height,
|
||||
SHA256: hex.EncodeToString(v.SHA256),
|
||||
}
|
||||
}
|
||||
errCode := a.ErrorCode
|
||||
return AttachmentResponse{
|
||||
ID: a.ID,
|
||||
ChatID: a.ChatID,
|
||||
OwnerID: a.OwnerID,
|
||||
Filename: a.Filename,
|
||||
ContentType: a.ContentType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
SHA256: hex.EncodeToString(a.SHA256),
|
||||
Status: string(a.Status),
|
||||
Variants: variants,
|
||||
ErrorCode: errCode,
|
||||
CreatedAt: a.CreatedAt.UTC().Format(timeFormat),
|
||||
UpdatedAt: a.UpdatedAt.UTC().Format(timeFormat),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type CreateChatRequest struct {
|
||||
Title string `json:"title" binding:"required,min=1,max=100"`
|
||||
MemberIDs []string `json:"member_ids" binding:"required,min=1,max=100,dive,uuid"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatPageResponse struct {
|
||||
Items []ChatResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func ChatFromDomain(c *domain.Chat) ChatResponse {
|
||||
return ChatResponse{
|
||||
ID: c.ID,
|
||||
Title: c.Title,
|
||||
MemberIDs: append([]string(nil), c.MemberIDs...),
|
||||
CreatedAt: c.CreatedAt.UTC().Format(timeFormat),
|
||||
}
|
||||
}
|
||||
|
||||
func ChatsFromDomain(items []domain.Chat) []ChatResponse {
|
||||
out := make([]ChatResponse, len(items))
|
||||
for i := range items {
|
||||
out[i] = ChatFromDomain(&items[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
// делаем вложенный ответ через вкладку ErrorBody в обертку.
|
||||
|
||||
type ErrorBody struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error ErrorBody `json:"error"`
|
||||
}
|
||||
|
||||
type coded interface {
|
||||
Code() domain.ErrorCode
|
||||
}
|
||||
|
||||
func ErrorFromDomain(err error, requestID string) ErrorResponse {
|
||||
var c coded
|
||||
if errors.As(err, &c) {
|
||||
code := c.Code()
|
||||
message := err.Error()
|
||||
if code == domain.CodeInternalError {
|
||||
message = "внутренняя ошибка"
|
||||
}
|
||||
return ErrorResponse{Error: ErrorBody{
|
||||
Code: string(code),
|
||||
Message: message,
|
||||
RequestID: requestID,
|
||||
}}
|
||||
}
|
||||
code := domain.CodeInternalError
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
code = domain.CodeNotFound
|
||||
case errors.Is(err, domain.ErrAlreadyExists):
|
||||
code = domain.CodeConflict
|
||||
case errors.Is(err, domain.ErrInvalidInput):
|
||||
code = domain.CodeInvalidRequest
|
||||
case errors.Is(err, domain.ErrUnauthorized):
|
||||
code = domain.CodeUnauthorized
|
||||
case errors.Is(err, domain.ErrUnsupportedMediaType):
|
||||
code = domain.CodeUnsupportedMediaType
|
||||
case errors.Is(err, domain.ErrUnavailable):
|
||||
code = domain.CodeUnavailable
|
||||
}
|
||||
message := err.Error()
|
||||
if code == domain.CodeInternalError {
|
||||
message = "внутренняя ошибка"
|
||||
}
|
||||
return ErrorResponse{Error: ErrorBody{
|
||||
Code: string(code),
|
||||
Message: message,
|
||||
RequestID: requestID,
|
||||
}}
|
||||
}
|
||||
|
||||
func StatusFromError(err error) int {
|
||||
switch {
|
||||
case errors.Is(err, domain.ErrNotFound):
|
||||
return 404
|
||||
case errors.Is(err, domain.ErrAlreadyExists):
|
||||
return 409
|
||||
case errors.Is(err, domain.ErrInvalidInput):
|
||||
return 400
|
||||
case errors.Is(err, domain.ErrUnauthorized):
|
||||
return 401
|
||||
case errors.Is(err, domain.ErrUnsupportedMediaType):
|
||||
return 415
|
||||
case errors.Is(err, domain.ErrUnavailable):
|
||||
return 503
|
||||
case errors.Is(err, domain.ErrPayloadTooLarge):
|
||||
return 413
|
||||
case errors.Is(err, domain.ErrConflict):
|
||||
return 409
|
||||
default:
|
||||
return 500
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text" binding:"max=4000"`
|
||||
AttachmentIDs []string `json:"attachment_ids" binding:"max=10,dive,uuid"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessagePageResponse struct {
|
||||
Items []MessageResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func MessageFromDomain(m *domain.Message) MessageResponse {
|
||||
ids := m.AttachmentIDs
|
||||
if ids == nil {
|
||||
ids = []string{}
|
||||
}
|
||||
return MessageResponse{
|
||||
ID: m.ID,
|
||||
ChatID: m.ChatID,
|
||||
SenderID: m.SenderID,
|
||||
Text: m.Text,
|
||||
AttachmentIDs: ids,
|
||||
CreatedAt: m.CreatedAt.UTC().Format(timeFormat),
|
||||
}
|
||||
}
|
||||
|
||||
func MessagesFromDomain(items []domain.Message) []MessageResponse {
|
||||
out := make([]MessageResponse, len(items))
|
||||
for i := range items {
|
||||
out[i] = MessageFromDomain(&items[i])
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type SessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User UserResponse `json:"user"`
|
||||
}
|
||||
|
||||
func SessionResponseFromDomain(s *domain.Session, token string, u *domain.User) SessionResponse {
|
||||
return SessionResponse{
|
||||
Token: token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: s.ExpiresAt.UTC().Format(timeFormat),
|
||||
User: UserFromDomain(u),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
const timeFormat = "2006-01-02T15:04:05.000000Z07:00"
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username" binding:"required,min=3,max=32"`
|
||||
DisplayName string `json:"display_name" binding:"required,min=1,max=100"`
|
||||
Password string `json:"password" binding:"required,min=12,max=512"`
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func UserFromDomain(u *domain.User) UserResponse {
|
||||
return UserResponse{
|
||||
ID: u.ID,
|
||||
Username: u.Username,
|
||||
DisplayName: u.DisplayName,
|
||||
CreatedAt: u.CreatedAt.UTC().Format(timeFormat),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
type AttachmentHandler struct {
|
||||
svc *service.AttachmentService
|
||||
}
|
||||
|
||||
func NewAttachmentHandler(svc *service.AttachmentService) *AttachmentHandler {
|
||||
return &AttachmentHandler{svc: svc}
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Upload(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
filename := c.Query("filename")
|
||||
if filename == "" {
|
||||
respondError(c, &domain.ValidationError{
|
||||
Field: "filename",
|
||||
Message: "обязателен",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
||||
if err != nil || (mediaType != "image/png" && mediaType != "image/jpeg") {
|
||||
respondError(c, &domain.UnsupportedMediaTypeError{
|
||||
MediaType: c.GetHeader("Content-Type"),
|
||||
Expected: "image/png или image/jpeg",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
att, err := h.svc.Upload(c.Request.Context(), me.ID, chatID, filename, c.Request.Body)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusAccepted, dto.AttachmentFromDomain(att))
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Get(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
attachmentID, err := parseUUIDPathParam(c, "attachment_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
a, err := h.svc.Get(c.Request.Context(), me.ID, attachmentID)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.AttachmentFromDomain(a))
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Content(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
attachmentID, err := parseUUIDPathParam(c, "attachment_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
variant := c.DefaultQuery("variant", "original")
|
||||
|
||||
rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID, variant)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = rc.Close() }()
|
||||
|
||||
c.DataFromReader(http.StatusOK, size, contentType, rc, nil)
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Replay(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
attachmentID, err := parseUUIDPathParam(c, "attachment_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.svc.Replay(c.Request.Context(), me.ID, attachmentID); err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusAccepted)
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
sessions *service.SessionService
|
||||
}
|
||||
|
||||
func NewAuthHandler(sessions *service.SessionService) *AuthHandler {
|
||||
return &AuthHandler{sessions: sessions}
|
||||
}
|
||||
|
||||
func (h *AuthHandler) CreateSession(c *gin.Context) {
|
||||
username, password, err := parseBasicAuth(c)
|
||||
if err != nil {
|
||||
respondBasicChallenge(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
session, token, user, err := h.sessions.Login(c.Request.Context(), username, password)
|
||||
if err != nil {
|
||||
respondBasicChallenge(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, dto.SessionResponseFromDomain(session, token, user))
|
||||
}
|
||||
|
||||
// RevokeCurrent отзывает текущую сессию. Токен уже проверен BearerAuth,
|
||||
// поэтому здесь его достаточно передать в SessionService.
|
||||
func (h *AuthHandler) RevokeCurrent(c *gin.Context) {
|
||||
token := middleware.CurrentToken(c)
|
||||
if token == "" {
|
||||
respondError(c, &domain.UnauthorizedError{Reason: "нет токена"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.sessions.Revoke(c.Request.Context(), token); err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// parseBasicAuth читает Authorization: Basic base64(username:password).
|
||||
func parseBasicAuth(c *gin.Context) (string, string, error) {
|
||||
const prefix = "Basic "
|
||||
|
||||
auth := c.GetHeader("Authorization")
|
||||
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
|
||||
return "", "", &domain.UnauthorizedError{Reason: "требуется Basic"}
|
||||
}
|
||||
|
||||
decoded, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
|
||||
if err != nil {
|
||||
return "", "", &domain.UnauthorizedError{Reason: "некорректный Base64"}
|
||||
}
|
||||
|
||||
username, password, ok := strings.Cut(string(decoded), ":")
|
||||
if !ok || username == "" {
|
||||
return "", "", &domain.UnauthorizedError{Reason: "некорректный Basic-формат"}
|
||||
}
|
||||
|
||||
return username, password, nil
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gin-gonic/gin/binding"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
func bindStrictJSON(c *gin.Context, dst any) error {
|
||||
ct := c.GetHeader("Content-Type")
|
||||
mediaType, _, err := mime.ParseMediaType(ct)
|
||||
if ct == "" || err != nil || !strings.EqualFold(mediaType, "application/json") {
|
||||
return &domain.UnsupportedMediaTypeError{MediaType: ct}
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
return &badRequestError{msg: "не удалось прочитать тело запроса"}
|
||||
}
|
||||
_ = c.Request.Body.Close()
|
||||
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
dec.DisallowUnknownFields()
|
||||
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
if errors.Is(err, io.EOF) {
|
||||
return &badRequestError{msg: "тело запроса обязательно"}
|
||||
}
|
||||
return &badRequestError{msg: err.Error()}
|
||||
}
|
||||
|
||||
// Отклоняем любой непустой остаток после первого JSON-значения.
|
||||
var extra json.RawMessage
|
||||
if err := dec.Decode(&extra); !errors.Is(err, io.EOF) {
|
||||
return &badRequestError{msg: "тело запроса содержит данные после JSON"}
|
||||
}
|
||||
|
||||
if v := binding.Validator; v != nil {
|
||||
if err := v.ValidateStruct(dst); err != nil {
|
||||
return &badRequestError{msg: err.Error()}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
chats *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(chats *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{chats: chats}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Create(c *gin.Context) {
|
||||
var req dto.CreateChatRequest
|
||||
if err := bindStrictJSON(c, &req); err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chat, err := h.chats.Create(c.Request.Context(), me.ID, req.Title, req.MemberIDs)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.ChatFromDomain(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Get(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.chats.Get(c.Request.Context(), me.ID, chatID)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.ChatFromDomain(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) List(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
limit, cursor, err := parseListQuery(c)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
items, next, err := h.chats.List(c.Request.Context(), me.ID, limit, cursor)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.ChatPageResponse{
|
||||
Items: dto.ChatsFromDomain(items),
|
||||
NextCursor: next,
|
||||
})
|
||||
}
|
||||
|
||||
func parseListQuery(c *gin.Context) (int, *string, error) {
|
||||
limit := 50
|
||||
if v := c.Query("limit"); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
return 0, nil, &badRequestError{msg: "limit должен быть целым 1..100"}
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
var cursor *string
|
||||
if v := c.Query("cursor"); v != "" {
|
||||
cursor = &v
|
||||
}
|
||||
return limit, cursor, nil
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type HealthCheck func(ctx context.Context) error
|
||||
|
||||
type HealthHandler struct {
|
||||
checks []HealthCheck
|
||||
ready *atomic.Bool
|
||||
}
|
||||
|
||||
func NewHealthHandler(checks ...HealthCheck) *HealthHandler {
|
||||
return &HealthHandler{checks: checks}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) WithReadyFlag(v *atomic.Bool) *HealthHandler {
|
||||
h.ready = v
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Live(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
ctx, span := telemetry.Tracer("httpapi").Start(c.Request.Context(), "health.ready")
|
||||
defer span.End()
|
||||
|
||||
if h.ready != nil && !h.ready.Load() {
|
||||
span.SetStatus(codes.Error, "shutting down")
|
||||
respondError(c, &domain.UnavailableError{Reason: "shutting down"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, check := range h.checks {
|
||||
if check == nil {
|
||||
continue
|
||||
}
|
||||
if err := check(ctx); err != nil {
|
||||
span.SetStatus(codes.Error, "зависимость недоступна")
|
||||
respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"})
|
||||
return
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"})
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type MessageHandler struct {
|
||||
messages *service.MessageService
|
||||
}
|
||||
|
||||
func NewMessageHandler(messages *service.MessageService) *MessageHandler {
|
||||
return &MessageHandler{messages: messages}
|
||||
}
|
||||
|
||||
func (h *MessageHandler) Create(c *gin.Context) {
|
||||
var req dto.CreateMessageRequest
|
||||
if err := bindStrictJSON(c, &req); err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
key := c.GetHeader("Idempotency-Key")
|
||||
if key == "" || !idempotencyKeyPattern.MatchString(key) {
|
||||
respondError(c, &badRequestError{
|
||||
msg: "Idempotency-Key обязателен и должен соответствовать ^[A-Za-z0-9._:-]{1,128}$",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.messages.Create(
|
||||
c.Request.Context(),
|
||||
me.ID,
|
||||
chatID,
|
||||
req.Text,
|
||||
req.AttachmentIDs,
|
||||
key,
|
||||
c.Request.URL.Path,
|
||||
)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Data(result.Status, "application/json", result.Body)
|
||||
}
|
||||
|
||||
func (h *MessageHandler) List(c *gin.Context) {
|
||||
me := middleware.CurrentUser(c)
|
||||
|
||||
chatID, err := parseUUIDPathParam(c, "chat_id")
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
limit, cursor, err := parseListQuery(c)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
items, next, err := h.messages.List(c.Request.Context(), me.ID, chatID, limit, cursor)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.MessagePageResponse{
|
||||
Items: dto.MessagesFromDomain(items),
|
||||
NextCursor: next,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
)
|
||||
|
||||
type badRequestError struct{ msg string }
|
||||
|
||||
func (e *badRequestError) Error() string { return e.msg }
|
||||
func (e *badRequestError) Unwrap() error { return domain.ErrInvalidInput }
|
||||
func (e *badRequestError) Code() domain.ErrorCode { return domain.CodeInvalidRequest }
|
||||
|
||||
func parseUUIDPathParam(c *gin.Context, name string) (string, error) {
|
||||
raw := c.Param(name)
|
||||
if raw == "" {
|
||||
return "", &badRequestError{msg: "параметр " + name + " обязателен"}
|
||||
}
|
||||
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return "", &badRequestError{msg: "параметр " + name + " должен быть корректным UUID"}
|
||||
}
|
||||
|
||||
return id.String(), nil
|
||||
}
|
||||
|
||||
func respondError(c *gin.Context, err error) {
|
||||
reqID := middleware.GetRequestID(c)
|
||||
c.JSON(dto.StatusFromError(err), dto.ErrorFromDomain(err, reqID))
|
||||
}
|
||||
|
||||
// respondBasicChallenge выставляет WWW-Authenticate для 401 на /auth/sessions.
|
||||
// На не-401 ответах заголовок не ставится как по контракту нужно
|
||||
func respondBasicChallenge(c *gin.Context, err error) {
|
||||
if dto.StatusFromError(err) == 401 {
|
||||
c.Header("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
}
|
||||
respondError(c, err)
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
users *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(users *service.UserService) *UserHandler {
|
||||
return &UserHandler{users: users}
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(c *gin.Context) {
|
||||
var req dto.CreateUserRequest
|
||||
if err := bindStrictJSON(c, &req); err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName, req.Password)
|
||||
if err != nil {
|
||||
respondError(c, err)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, dto.UserFromDomain(u))
|
||||
}
|
||||
|
||||
func (h *UserHandler) Current(c *gin.Context) {
|
||||
u := middleware.CurrentUser(c)
|
||||
if u == nil {
|
||||
respondError(c, &badRequestError{msg: "нет текущего пользователя"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, dto.UserFromDomain(u))
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
)
|
||||
|
||||
func Auth(users domain.UserRepository) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id := c.GetHeader("X-User-Id")
|
||||
if id == "" {
|
||||
abortUnauthorized(c, "требуется X-User-Id")
|
||||
return
|
||||
}
|
||||
|
||||
u, err := users.GetByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
abortUnauthorized(c, "неизвестный пользователь")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set(CurrentUserKey, u)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func abortUnauthorized(c *gin.Context, msg string) {
|
||||
reqID := GetRequestID(c)
|
||||
body := dto.ErrorFromDomain(
|
||||
&domain.UnauthorizedError{Reason: msg},
|
||||
reqID,
|
||||
)
|
||||
c.AbortWithStatusJSON(http.StatusUnauthorized, body)
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/service"
|
||||
)
|
||||
|
||||
const authTimeout = 3 * time.Second
|
||||
|
||||
// BearerAuth читает Authorization: Bearer <token>, проверяет сессию
|
||||
// через SessionService и кладёт пользователя и токен в контекст.
|
||||
func BearerAuth(sessions *service.SessionService) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token, ok := parseBearerToken(c)
|
||||
if !ok {
|
||||
abortBearer(c, "требуется Bearer")
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), authTimeout)
|
||||
defer cancel()
|
||||
|
||||
user, err := sessions.Resolve(ctx, token)
|
||||
if err != nil {
|
||||
abortBearerWithErr(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
c.Set(CurrentUserKey, user)
|
||||
c.Set(CurrentTokenKey, token)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// parseBearerToken читает Authorization: Bearer <opaque-token>.
|
||||
func parseBearerToken(c *gin.Context) (string, bool) {
|
||||
const prefix = "Bearer "
|
||||
auth := c.GetHeader("Authorization")
|
||||
if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) {
|
||||
return "", false
|
||||
}
|
||||
token := strings.TrimSpace(auth[len(prefix):])
|
||||
if token == "" {
|
||||
return "", false
|
||||
}
|
||||
return token, true
|
||||
}
|
||||
|
||||
func abortBearer(c *gin.Context, reason string) {
|
||||
abortBearerWithErr(c, &domain.UnauthorizedError{Reason: reason})
|
||||
}
|
||||
|
||||
// abortBearerWithErr формирует ответ на ошибку аутентификации.
|
||||
// Заголовок WWW-Authenticate: Bearer выставляется только на 401;
|
||||
// при 503 (недоступность БД/кэша) — обычный ответ ошибки без challenge.
|
||||
func abortBearerWithErr(c *gin.Context, err error) {
|
||||
reqID := GetRequestID(c)
|
||||
status := dto.StatusFromError(err)
|
||||
if status == 401 {
|
||||
c.Header("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
}
|
||||
c.AbortWithStatusJSON(status, dto.ErrorFromDomain(err, reqID))
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
CurrentUserKey = "current_user"
|
||||
CurrentTokenKey = "current_token"
|
||||
)
|
||||
|
||||
func CurrentUser(c *gin.Context) *domain.User {
|
||||
if v, ok := c.Get(CurrentUserKey); ok {
|
||||
if u, ok := v.(*domain.User); ok {
|
||||
return u
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CurrentToken(c *gin.Context) string {
|
||||
if v, ok := c.Get(CurrentTokenKey); ok {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const InstanceIDHeader = "X-Instance-Id"
|
||||
|
||||
var instanceID = resolveInstanceID()
|
||||
|
||||
func resolveInstanceID() string {
|
||||
if v := os.Getenv("INSTANCE_ID"); v != "" {
|
||||
return v
|
||||
}
|
||||
if v, err := os.Hostname(); err == nil && v != "" {
|
||||
return v
|
||||
}
|
||||
return uuid.NewString()
|
||||
}
|
||||
|
||||
func Instance() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
c.Writer.Header().Set(InstanceIDHeader, instanceID)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetInstanceID() string { return instanceID }
|
||||
@@ -0,0 +1,35 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Logging записывает результат запроса с шаблоном маршрута и контекстом серверного спана.
|
||||
func Logging() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
route := c.FullPath()
|
||||
if route == "" {
|
||||
route = "unknown"
|
||||
}
|
||||
status := c.Writer.Status()
|
||||
level := slog.LevelInfo
|
||||
if status >= 500 {
|
||||
level = slog.LevelError
|
||||
}
|
||||
slog.LogAttrs(c.Request.Context(), level, "HTTP-запрос завершён",
|
||||
slog.String("instance_id", GetInstanceID()),
|
||||
slog.String("event", "http.request.completed"),
|
||||
slog.String("request_id", GetRequestID(c)),
|
||||
slog.String("http.request.method", c.Request.Method),
|
||||
slog.String("http.route", route),
|
||||
slog.Int("http.response.status_code", status),
|
||||
slog.Float64("duration_seconds", time.Since(start).Seconds()),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type httpMetrics struct {
|
||||
requestsTotal metric.Int64Counter
|
||||
requestDuration metric.Float64Histogram
|
||||
}
|
||||
|
||||
func newHTTPMetrics() *httpMetrics {
|
||||
meter := telemetry.Meter("httpapi")
|
||||
m := &httpMetrics{}
|
||||
var err error
|
||||
|
||||
m.requestsTotal, err = meter.Int64Counter(
|
||||
"http_requests_total",
|
||||
metric.WithDescription("Общее количество HTTP-запросов"),
|
||||
)
|
||||
if err != nil {
|
||||
panic("метрика http_requests_total: " + err.Error())
|
||||
}
|
||||
|
||||
m.requestDuration, err = meter.Float64Histogram(
|
||||
"http_request_duration_seconds",
|
||||
metric.WithDescription("Длительность HTTP-запроса"),
|
||||
metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(
|
||||
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10,
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
panic("метрика http_request_duration_seconds: " + err.Error())
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func Metrics() gin.HandlerFunc {
|
||||
m := newHTTPMetrics()
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
|
||||
c.Next()
|
||||
|
||||
route := c.FullPath()
|
||||
if route == "" {
|
||||
route = "unknown"
|
||||
}
|
||||
status := c.Writer.Status()
|
||||
statusClass := "unknown"
|
||||
if status >= 100 {
|
||||
statusClass = strconv.Itoa(status/100) + "xx"
|
||||
}
|
||||
|
||||
attrs := metric.WithAttributes(
|
||||
attribute.String("http.request.method", c.Request.Method),
|
||||
attribute.String("http.route", route),
|
||||
attribute.String("http.response.status_class", statusClass),
|
||||
)
|
||||
|
||||
duration := time.Since(start).Seconds()
|
||||
m.requestsTotal.Add(c.Request.Context(), 1, attrs)
|
||||
m.requestDuration.Record(c.Request.Context(), duration, attrs)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
// OTel создаёт серверный span для каждого HTTP-запроса.
|
||||
//
|
||||
// Имя span: "<METHOD> <route_template>", например "GET /api/v1/chats/:chat_id".
|
||||
// Родительский контекст извлекается из заголовков W3C traceparent чтобы связать внешний клиент (или LB) с внутренними спанами.
|
||||
|
||||
func OTel() gin.HandlerFunc {
|
||||
tracer := telemetry.Tracer("httpapi")
|
||||
return func(c *gin.Context) {
|
||||
// 1. Извлекаем родительский контекст из заголовков
|
||||
parentCtx := otel.GetTextMapPropagator().Extract(
|
||||
c.Request.Context(),
|
||||
propagation.HeaderCarrier(c.Request.Header),
|
||||
)
|
||||
|
||||
// 2. Стартуем span с временным именем (route ещё неизвестен)
|
||||
ctx, span := tracer.Start(parentCtx,
|
||||
c.Request.Method+" unknown",
|
||||
trace.WithSpanKind(trace.SpanKindServer),
|
||||
)
|
||||
defer span.End()
|
||||
|
||||
// 3. Кладём контекст в запрос, чтобы дочерние спаны (БД, S3, Redis) видели родителя
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
|
||||
// 4. Базовые атрибуты, известные до выполнения handler
|
||||
span.SetAttributes(
|
||||
attribute.String("http.request.method", c.Request.Method),
|
||||
attribute.String("url.path", c.Request.URL.Path),
|
||||
attribute.String("url.scheme", scheme(c)),
|
||||
attribute.String("server.address", c.Request.Host),
|
||||
attribute.String("client.address", c.ClientIP()),
|
||||
attribute.String("user_agent.original", c.Request.UserAgent()),
|
||||
)
|
||||
|
||||
// 5. Выполняем цепочку middleware и handler
|
||||
c.Next()
|
||||
|
||||
// 6. После выполнения знаем route template — обновляем имя span
|
||||
route := c.FullPath()
|
||||
if route == "" {
|
||||
route = "unknown"
|
||||
}
|
||||
span.SetName(c.Request.Method + " " + route)
|
||||
span.SetAttributes(attribute.String("http.route", route))
|
||||
|
||||
// 7. Статус ответа
|
||||
status := c.Writer.Status()
|
||||
span.SetAttributes(attribute.Int("http.response.status_code", status))
|
||||
if status >= 500 {
|
||||
span.SetStatus(codes.Error, http.StatusText(status))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func scheme(c *gin.Context) string {
|
||||
if c.Request.TLS != nil {
|
||||
return "https"
|
||||
}
|
||||
if v := c.GetHeader("X-Forwarded-Proto"); v != "" {
|
||||
return v
|
||||
}
|
||||
return "http"
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
RequestIDKey = "request_id"
|
||||
RequestIDHeader = "X-Request-Id"
|
||||
)
|
||||
|
||||
func RequestID() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
id := c.GetHeader(RequestIDHeader)
|
||||
if id == "" {
|
||||
id = uuid.NewString()
|
||||
}
|
||||
c.Set(RequestIDKey, id)
|
||||
c.Writer.Header().Set(RequestIDHeader, id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func GetRequestID(c *gin.Context) string {
|
||||
if v, ok := c.Get(RequestIDKey); ok {
|
||||
if s, ok := v.(string); ok {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package httpapi
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"high-loud-apps-labs/internal/httpapi/handler"
|
||||
"high-loud-apps-labs/internal/httpapi/middleware"
|
||||
)
|
||||
|
||||
type Handlers struct {
|
||||
Health *handler.HealthHandler
|
||||
User *handler.UserHandler
|
||||
Chat *handler.ChatHandler
|
||||
Message *handler.MessageHandler
|
||||
Auth *handler.AuthHandler
|
||||
Attachment *handler.AttachmentHandler
|
||||
}
|
||||
|
||||
func NewRouter(h Handlers, auth gin.HandlerFunc) *gin.Engine {
|
||||
gin.EnableJsonDecoderDisallowUnknownFields()
|
||||
|
||||
r := gin.New()
|
||||
r.Use(middleware.RequestID())
|
||||
r.Use(middleware.Instance())
|
||||
r.Use(middleware.OTel())
|
||||
r.Use(middleware.Metrics())
|
||||
r.Use(middleware.Logging())
|
||||
r.Use(gin.CustomRecoveryWithWriter(nil, func(c *gin.Context, err any) {
|
||||
slog.Error("Паника при обработке HTTP-запроса",
|
||||
"panic", err,
|
||||
"http.route", c.FullPath(),
|
||||
"request_id", middleware.GetRequestID(c),
|
||||
)
|
||||
c.AbortWithStatus(http.StatusInternalServerError)
|
||||
}))
|
||||
|
||||
r.GET("/health/live", h.Health.Live)
|
||||
r.GET("/health/ready", h.Health.Ready)
|
||||
|
||||
api := r.Group("/api/v1")
|
||||
{
|
||||
api.POST("/users", h.User.Create)
|
||||
api.POST("/auth/sessions", h.Auth.CreateSession)
|
||||
}
|
||||
|
||||
protected := api.Group("")
|
||||
protected.Use(auth)
|
||||
{
|
||||
protected.DELETE("/auth/sessions/current", h.Auth.RevokeCurrent)
|
||||
|
||||
protected.GET("/users/me", h.User.Current)
|
||||
protected.POST("/chats", h.Chat.Create)
|
||||
protected.GET("/chats", h.Chat.List)
|
||||
protected.GET("/chats/:chat_id", h.Chat.Get)
|
||||
protected.POST("/chats/:chat_id/messages", h.Message.Create)
|
||||
protected.GET("/chats/:chat_id/messages", h.Message.List)
|
||||
|
||||
if h.Attachment != nil {
|
||||
protected.POST("/chats/:chat_id/attachments", h.Attachment.Upload)
|
||||
protected.GET("/attachments/:attachment_id", h.Attachment.Get)
|
||||
protected.GET("/attachments/:attachment_id/content", h.Attachment.Content)
|
||||
protected.POST("/attachments/:attachment_id/replay", h.Attachment.Replay)
|
||||
}
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
func Crop(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) {
|
||||
img, _, err := image.Decode(bytes.NewReader(imgData))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
|
||||
}
|
||||
|
||||
cropImg := imaging.Fill(img, 128, 128, imaging.Center, imaging.Lanczos)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := imaging.Encode(&buf, cropImg, imaging.JPEG, imaging.JPEGQuality(85)); err != nil {
|
||||
return nil, nil, fmt.Errorf("кодирование jpeg: %w", err)
|
||||
}
|
||||
|
||||
data := buf.Bytes()
|
||||
sha := sha256.Sum256(data)
|
||||
|
||||
v := &domain.AttachmentVariant{
|
||||
AttachmentID: attachmentID,
|
||||
Name: domain.VariantCrop,
|
||||
ContentType: "image/jpeg",
|
||||
SizeBytes: int64(len(data)),
|
||||
Width: cropImg.Bounds().Dx(),
|
||||
Height: cropImg.Bounds().Dy(),
|
||||
SHA256: sha[:],
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
return v, data, nil
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/gen2brain/webp"
|
||||
)
|
||||
|
||||
func Optimized(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) {
|
||||
img, _, err := image.Decode(bytes.NewReader(imgData))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
|
||||
}
|
||||
|
||||
optimizedImg := imaging.Fit(img, 1280, 1280, imaging.Lanczos)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := webp.Encode(&buf, optimizedImg, webp.Options{Quality: 80}); err != nil {
|
||||
return nil, nil, fmt.Errorf("кодирование webp: %w", err)
|
||||
}
|
||||
|
||||
data := buf.Bytes()
|
||||
sha := sha256.Sum256(data)
|
||||
|
||||
v := &domain.AttachmentVariant{
|
||||
AttachmentID: attachmentID,
|
||||
Name: domain.VariantOptimized,
|
||||
ContentType: "image/webp",
|
||||
SizeBytes: int64(len(data)),
|
||||
Width: optimizedImg.Bounds().Dx(),
|
||||
Height: optimizedImg.Bounds().Dy(),
|
||||
SHA256: sha[:],
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
return v, data, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package imaging
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"time"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
)
|
||||
|
||||
func Thumbnail(attachmentID string, imgData []byte) (*domain.AttachmentVariant, []byte, error) {
|
||||
img, _, err := image.Decode(bytes.NewReader(imgData))
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("декодирование изображения: %w", err)
|
||||
}
|
||||
|
||||
thumb := imaging.Fit(img, 256, 256, imaging.Lanczos)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if err := imaging.Encode(&buf, thumb, imaging.JPEG, imaging.JPEGQuality(85)); err != nil {
|
||||
return nil, nil, fmt.Errorf("кодирование jpeg: %w", err)
|
||||
}
|
||||
|
||||
data := buf.Bytes()
|
||||
sha := sha256.Sum256(data)
|
||||
|
||||
v := &domain.AttachmentVariant{
|
||||
AttachmentID: attachmentID,
|
||||
Name: domain.VariantThumbnail,
|
||||
ContentType: "image/jpeg",
|
||||
SizeBytes: int64(len(data)),
|
||||
Width: thumb.Bounds().Dx(),
|
||||
Height: thumb.Bounds().Dy(),
|
||||
SHA256: sha[:],
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
return v, data, nil
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package outbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/broker"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type Publisher struct {
|
||||
repo domain.OutboxRepository
|
||||
broker broker.Publisher
|
||||
batch int
|
||||
}
|
||||
|
||||
func NewPublisher(repo domain.OutboxRepository, b broker.Publisher, batch int) *Publisher {
|
||||
if batch <= 0 {
|
||||
batch = 50
|
||||
}
|
||||
return &Publisher{repo: repo, broker: b, batch: batch}
|
||||
}
|
||||
|
||||
func (p *Publisher) Run(ctx context.Context, interval time.Duration) {
|
||||
if interval <= 0 {
|
||||
interval = 1 * time.Second
|
||||
}
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
log.Printf("outbox: publisher запущен (interval=%s)", interval)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("outbox: publisher остановлен")
|
||||
return
|
||||
case <-ticker.C:
|
||||
p.tick(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Publisher) tick(ctx context.Context) {
|
||||
entries, err := p.repo.ClaimBatch(ctx, p.batch)
|
||||
if err != nil {
|
||||
log.Printf("outbox: ошибка ClaimBatch: %v", err)
|
||||
return
|
||||
}
|
||||
if len(entries) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
var successIDs []string
|
||||
for _, e := range entries {
|
||||
err := p.broker.Publish(telemetry.ExtractTrace(ctx, e.Headers), "process", e.Payload)
|
||||
if err != nil {
|
||||
log.Printf("outbox: ошибка публикации %s: %v", e.ID, err)
|
||||
break // Прерываем пачку
|
||||
}
|
||||
successIDs = append(successIDs, e.ID)
|
||||
}
|
||||
|
||||
if len(successIDs) > 0 {
|
||||
if err := p.repo.MarkPublished(ctx, successIDs); err != nil {
|
||||
log.Printf("outbox: ошибка MarkPublished: %v", err)
|
||||
} else {
|
||||
log.Printf("outbox: успешно опубликовано %d сообщений", len(successIDs))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type txRunner interface {
|
||||
WithTx(ctx context.Context, fn func(tx domain.Tx) error) error
|
||||
}
|
||||
|
||||
type AttachmentService struct {
|
||||
attachments domain.AttachmentRepository
|
||||
chats domain.ChatRepository
|
||||
storage domain.ObjectStorage
|
||||
outbox domain.OutboxRepository
|
||||
failedJobs domain.FailedJobRepository
|
||||
tx txRunner
|
||||
maxBytes int64
|
||||
maxPixels int64
|
||||
}
|
||||
|
||||
func NewAttachmentService(
|
||||
attachments domain.AttachmentRepository,
|
||||
chats domain.ChatRepository,
|
||||
storage domain.ObjectStorage,
|
||||
outbox domain.OutboxRepository,
|
||||
failedJobs domain.FailedJobRepository,
|
||||
tx txRunner,
|
||||
maxBytes, maxPixels int64,
|
||||
) *AttachmentService {
|
||||
return &AttachmentService{
|
||||
attachments: attachments,
|
||||
chats: chats,
|
||||
storage: storage,
|
||||
outbox: outbox,
|
||||
failedJobs: failedJobs,
|
||||
tx: tx,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
}
|
||||
}
|
||||
|
||||
// 1. Валидация, sniff Content-Type, проверка пикселей по заголовку.
|
||||
// 2. Оригинал в S3 staging/{id}.
|
||||
// 3. Транзакция: INSERT attachments(status=queued) + INSERT outbox.
|
||||
func (s *AttachmentService) Upload(
|
||||
ctx context.Context,
|
||||
ownerID, chatID, filename string,
|
||||
body io.Reader,
|
||||
) (*domain.Attachment, error) {
|
||||
if err := s.ensureMember(ctx, ownerID, chatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateFilename(filename); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
limited := io.LimitReader(body, s.maxBytes+1)
|
||||
buf, err := io.ReadAll(limited)
|
||||
if err != nil {
|
||||
return nil, &domain.UnavailableError{Reason: "не удалось прочитать тело загрузки"}
|
||||
}
|
||||
if int64(len(buf)) > s.maxBytes {
|
||||
return nil, &domain.PayloadTooLargeError{Limit: s.maxBytes}
|
||||
}
|
||||
if len(buf) == 0 {
|
||||
return nil, &domain.ValidationError{Field: "body", Message: "пустой файл"}
|
||||
}
|
||||
|
||||
contentType := http.DetectContentType(buf)
|
||||
if contentType != "image/png" && contentType != "image/jpeg" {
|
||||
return nil, &domain.UnsupportedMediaTypeError{
|
||||
MediaType: contentType,
|
||||
Expected: "image/png или image/jpeg",
|
||||
}
|
||||
}
|
||||
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(buf))
|
||||
if err != nil {
|
||||
return nil, &domain.UnsupportedMediaTypeError{
|
||||
MediaType: contentType,
|
||||
Expected: "целостный image/png или image/jpeg",
|
||||
}
|
||||
}
|
||||
if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels {
|
||||
return nil, &domain.PayloadTooLargeError{
|
||||
Limit: s.maxPixels,
|
||||
Message: "изображение превышает лимит пикселей",
|
||||
}
|
||||
}
|
||||
|
||||
sha := sha256.Sum256(buf)
|
||||
id := uuid.NewString()
|
||||
stagingKey := "staging/" + id
|
||||
|
||||
a := &domain.Attachment{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
OwnerID: ownerID,
|
||||
Filename: filename,
|
||||
ContentType: contentType,
|
||||
SizeBytes: int64(len(buf)),
|
||||
SHA256: sha[:],
|
||||
ObjectKey: "chats/" + chatID + "/" + id,
|
||||
Status: domain.AttachmentStatusQueued,
|
||||
StagingKey: stagingKey,
|
||||
PipelineVersion: 1,
|
||||
}
|
||||
|
||||
if err := s.storage.Put(ctx, stagingKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(domain.JobMessage{
|
||||
AttachmentID: id,
|
||||
PipelineVersion: 1,
|
||||
Attempt: 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = s.tx.WithTx(ctx, func(tx domain.Tx) error {
|
||||
a.CreatedAt = time.Now().UTC()
|
||||
a.UpdatedAt = a.CreatedAt
|
||||
if err := s.attachments.CreateInTx(ctx, tx, a); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.outbox.Enqueue(ctx, tx, &domain.OutboxEntry{
|
||||
AggregateType: domain.OutboxAggregateAttachment,
|
||||
AggregateID: id,
|
||||
EventType: domain.OutboxEventProcess,
|
||||
Payload: payload,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) {
|
||||
a, err := s.attachments.GetByID(ctx, attachmentID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := s.ensureMember(ctx, userID, a.ChatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Download(
|
||||
ctx context.Context,
|
||||
userID, attachmentID, variantName string,
|
||||
) (io.ReadCloser, int64, string, error) {
|
||||
a, err := s.Get(ctx, userID, attachmentID)
|
||||
if err != nil {
|
||||
return nil, 0, "", err
|
||||
}
|
||||
if a.Status != domain.AttachmentStatusReady {
|
||||
return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"}
|
||||
}
|
||||
|
||||
if variantName == "original" || variantName == "" {
|
||||
return s.storage.Get(ctx, a.ObjectKey)
|
||||
}
|
||||
|
||||
for _, v := range a.Variants {
|
||||
if v.Name == variantName {
|
||||
return s.storage.Get(ctx, v.ObjectKey)
|
||||
}
|
||||
}
|
||||
|
||||
return nil, 0, "", &domain.NotFoundError{Entity: "variant", ID: variantName}
|
||||
}
|
||||
|
||||
// Replay позволяет оператору перезапустить упавшую задачу.
|
||||
func (s *AttachmentService) Replay(ctx context.Context, userID, attachmentID string) error {
|
||||
a, err := s.Get(ctx, userID, attachmentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if a.Status != domain.AttachmentStatusFailed {
|
||||
return &domain.ConflictError{Reason: "перезапустить можно только failed-вложение"}
|
||||
}
|
||||
|
||||
// Формируем новое сообщение
|
||||
payload, err := json.Marshal(domain.JobMessage{
|
||||
AttachmentID: a.ID,
|
||||
PipelineVersion: a.PipelineVersion,
|
||||
Attempt: 1, // Сбрасываем счетчик попыток
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = s.tx.WithTx(ctx, func(tx domain.Tx) error {
|
||||
if err := s.attachments.ResetForReplay(ctx, tx, a.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
// Помечаем в реестре упавших задач (внутри транзакции)
|
||||
if err := s.failedJobs.ResolveInTx(ctx, tx, a.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.outbox.Enqueue(ctx, tx, &domain.OutboxEntry{
|
||||
AggregateType: domain.OutboxAggregateAttachment,
|
||||
AggregateID: a.ID,
|
||||
EventType: domain.OutboxEventProcess,
|
||||
Payload: payload,
|
||||
})
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error {
|
||||
c, err := s.chats.GetByID(ctx, chatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range c.MemberIDs {
|
||||
if m == userID {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return &domain.NotFoundError{Entity: "chat", ID: chatID}
|
||||
}
|
||||
|
||||
func validateFilename(name string) error {
|
||||
if strings.TrimSpace(name) == "" {
|
||||
return &domain.ValidationError{Field: "filename", Message: "не может быть пустым или состоять только из пробелов"}
|
||||
}
|
||||
if utf8.RuneCountInString(name) > 255 {
|
||||
return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"}
|
||||
}
|
||||
if strings.ContainsAny(name, "/\\\x00") {
|
||||
return &domain.ValidationError{Field: "filename", Message: "содержит недопустимые символы"}
|
||||
}
|
||||
for _, r := range name {
|
||||
if unicode.IsControl(r) {
|
||||
return &domain.ValidationError{Field: "filename", Message: "содержит управляющие символы"}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type ChatService struct {
|
||||
chats domain.ChatRepository
|
||||
users domain.UserRepository
|
||||
}
|
||||
|
||||
func NewChatService(chats domain.ChatRepository, users domain.UserRepository) *ChatService {
|
||||
return &ChatService{chats: chats, users: users}
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, creatorID, title string, memberIDs []string) (*domain.Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "title",
|
||||
Message: "не может состоять только из пробелов",
|
||||
}
|
||||
}
|
||||
if utf8.RuneCountInString(title) > 100 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "title",
|
||||
Message: "не более 100 символов",
|
||||
}
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
for _, id := range memberIDs {
|
||||
if _, dup := seen[id]; dup {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "member_ids",
|
||||
Message: "дубликаты запрещены",
|
||||
}
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
if _, ok := seen[creatorID]; !ok {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "member_ids",
|
||||
Message: "создатель должен входить в список участников",
|
||||
}
|
||||
}
|
||||
|
||||
for _, id := range memberIDs {
|
||||
if _, err := s.users.GetByID(ctx, id); err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "member_ids",
|
||||
Message: "неизвестный участник " + id,
|
||||
}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
c := &domain.Chat{
|
||||
ID: uuid.NewString(),
|
||||
Title: title,
|
||||
MemberIDs: append([]string(nil), memberIDs...),
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := s.chats.Create(ctx, c); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, userID, chatID string) (*domain.Chat, error) {
|
||||
c, err := s.chats.GetByID(ctx, chatID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, m := range c.MemberIDs {
|
||||
if m == userID {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
return nil, &domain.NotFoundError{Entity: "chat", ID: chatID}
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) {
|
||||
return s.chats.ListByMember(ctx, userID, limit, cursor)
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
)
|
||||
|
||||
type MessageService struct {
|
||||
messages domain.MessageRepository
|
||||
chats domain.ChatRepository
|
||||
idempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
func NewMessageService(
|
||||
messages domain.MessageRepository,
|
||||
chats domain.ChatRepository,
|
||||
idempotencyTTL time.Duration,
|
||||
) *MessageService {
|
||||
return &MessageService{
|
||||
messages: messages,
|
||||
chats: chats,
|
||||
idempotencyTTL: idempotencyTTL,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MessageService) Create(
|
||||
ctx context.Context,
|
||||
senderID, chatID, text string,
|
||||
attachmentIDs []string,
|
||||
idempotencyKey, requestPath string,
|
||||
) (*domain.CreateIdempotentResult, error) {
|
||||
trimmed := strings.TrimSpace(text)
|
||||
if trimmed == "" && len(attachmentIDs) == 0 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "text",
|
||||
Message: "text или attachment_ids обязательны",
|
||||
}
|
||||
}
|
||||
if utf8.RuneCountInString(text) > 4000 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "text",
|
||||
Message: "не более 4000 символов",
|
||||
}
|
||||
}
|
||||
if len(attachmentIDs) > 10 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "attachment_ids",
|
||||
Message: "не более 10",
|
||||
}
|
||||
}
|
||||
seen := make(map[string]struct{}, len(attachmentIDs))
|
||||
for _, id := range attachmentIDs {
|
||||
if _, dup := seen[id]; dup {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "attachment_ids",
|
||||
Message: "дубликаты запрещены",
|
||||
}
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
if err := s.ensureMember(ctx, senderID, chatID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fresh := &domain.Message{
|
||||
ID: uuid.NewString(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
AttachmentIDs: append([]string(nil), attachmentIDs...),
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
body, err := json.Marshal(dto.MessageFromDomain(fresh))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s.messages.CreateIdempotent(
|
||||
ctx,
|
||||
domain.IdempotencyScope{
|
||||
UserID: senderID,
|
||||
Method: http.MethodPost,
|
||||
Path: requestPath,
|
||||
Key: idempotencyKey,
|
||||
},
|
||||
domain.RequestHash(text, attachmentIDs),
|
||||
fresh,
|
||||
body,
|
||||
s.idempotencyTTL,
|
||||
)
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, userID, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
|
||||
if err := s.ensureMember(ctx, userID, chatID); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return s.messages.ListByChat(ctx, chatID, limit, cursor)
|
||||
}
|
||||
|
||||
func (s *MessageService) ensureMember(ctx context.Context, userID, chatID string) error {
|
||||
c, err := s.chats.GetByID(ctx, chatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, m := range c.MemberIDs {
|
||||
if m == userID {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return &domain.NotFoundError{Entity: "chat", ID: chatID}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
// PasswordHasher хеширует пароли и проверяет пароль сохранённого хеша.
|
||||
type PasswordHasher interface {
|
||||
Hash(password string) (string, error)
|
||||
Verify(password, encoded string) (bool, error)
|
||||
}
|
||||
|
||||
type Argon2idHasher struct {
|
||||
memory uint32
|
||||
iterations uint32
|
||||
parallelism uint8
|
||||
saltLength uint32
|
||||
keyLength uint32
|
||||
}
|
||||
|
||||
func NewArgon2idHasher() *Argon2idHasher {
|
||||
return &Argon2idHasher{
|
||||
memory: 64 * 1024,
|
||||
iterations: 3,
|
||||
parallelism: 4,
|
||||
saltLength: 16,
|
||||
keyLength: 32,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Argon2idHasher) Hash(password string) (string, error) {
|
||||
salt := make([]byte, h.saltLength)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", fmt.Errorf("генерация соли: %w", err)
|
||||
}
|
||||
|
||||
key := argon2.IDKey(
|
||||
[]byte(password),
|
||||
salt,
|
||||
h.iterations,
|
||||
h.memory,
|
||||
h.parallelism,
|
||||
h.keyLength,
|
||||
)
|
||||
|
||||
return fmt.Sprintf(
|
||||
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version,
|
||||
h.memory,
|
||||
h.iterations,
|
||||
h.parallelism,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(key),
|
||||
), nil
|
||||
}
|
||||
|
||||
// Verify сравнивает пароль с сохранённым PHC-хешем.
|
||||
// Возвращает false и nil при неверном пароле.
|
||||
// Возвращает false и ошибку, если строка хеша повреждена или несовместима.
|
||||
func (h *Argon2idHasher) Verify(password, encoded string) (bool, error) {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[1] != "argon2id" {
|
||||
return false, errors.New("некорректный формат хеша")
|
||||
}
|
||||
|
||||
var version int
|
||||
if n, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || n != 1 {
|
||||
return false, errors.New("некорректная версия хеша")
|
||||
}
|
||||
if version != argon2.Version {
|
||||
return false, errors.New("неподдерживаемая версия Argon2")
|
||||
}
|
||||
|
||||
var (
|
||||
memory uint32
|
||||
iterations uint32
|
||||
parallelism uint8
|
||||
)
|
||||
if n, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil || n != 3 {
|
||||
return false, errors.New("некорректные параметры хеша")
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
if err != nil {
|
||||
return false, errors.New("некорректная соль")
|
||||
}
|
||||
|
||||
wantKey, err := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if err != nil {
|
||||
return false, errors.New("некорректный ключ")
|
||||
}
|
||||
|
||||
gotKey := argon2.IDKey(
|
||||
[]byte(password),
|
||||
salt,
|
||||
iterations,
|
||||
memory,
|
||||
parallelism,
|
||||
uint32(len(wantKey)),
|
||||
)
|
||||
|
||||
return subtle.ConstantTimeCompare(gotKey, wantKey) == 1, nil
|
||||
}
|
||||
|
||||
var _ PasswordHasher = (*Argon2idHasher)(nil)
|
||||
@@ -0,0 +1,257 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
const advisoryLockKey int64 = 0x4D5455534904
|
||||
|
||||
type Reconciler struct {
|
||||
attachments domain.AttachmentRepository
|
||||
storage domain.ObjectStorage
|
||||
outbox domain.OutboxRepository
|
||||
pool *pgxpool.Pool
|
||||
gracePeriod time.Duration
|
||||
batchSize int
|
||||
metrics *reconcilerMetrics
|
||||
}
|
||||
|
||||
func NewReconciler(
|
||||
attachments domain.AttachmentRepository,
|
||||
storage domain.ObjectStorage,
|
||||
outbox domain.OutboxRepository,
|
||||
pool *pgxpool.Pool,
|
||||
gracePeriod time.Duration,
|
||||
) *Reconciler {
|
||||
if gracePeriod <= 0 {
|
||||
gracePeriod = time.Hour
|
||||
}
|
||||
return &Reconciler{
|
||||
attachments: attachments,
|
||||
storage: storage,
|
||||
outbox: outbox,
|
||||
pool: pool,
|
||||
gracePeriod: gracePeriod,
|
||||
batchSize: 100,
|
||||
metrics: newReconcilerMetrics(),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reconciler) Run(ctx context.Context, interval time.Duration) {
|
||||
if interval <= 0 {
|
||||
interval = 5 * time.Minute
|
||||
}
|
||||
registration, err := r.metrics.observeLeader()
|
||||
if err != nil {
|
||||
log.Printf("reconciler: ошибка регистрации метрики лидерства: %v", err)
|
||||
} else {
|
||||
defer registration.Unregister()
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
log.Printf("reconciler: запущен (interval=%s, grace=%s)", interval, r.gracePeriod)
|
||||
r.tick(ctx)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
log.Println("reconciler: остановлен")
|
||||
return
|
||||
case <-ticker.C:
|
||||
r.tick(ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reconciler) tick(ctx context.Context) {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.tick")
|
||||
defer span.End()
|
||||
|
||||
acquired, release, err := r.acquireLock(ctx)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения блокировки: %v", err)
|
||||
return
|
||||
}
|
||||
if !acquired {
|
||||
return
|
||||
}
|
||||
r.metrics.leader.Store(true)
|
||||
defer func() {
|
||||
release()
|
||||
r.metrics.leader.Store(false)
|
||||
}()
|
||||
|
||||
leasesReset := r.sweepExpiredLeases(ctx)
|
||||
stagingDeleted := r.sweepStaging(ctx)
|
||||
orphansDeleted := r.sweepOrphans(ctx)
|
||||
|
||||
if leasesReset > 0 || stagingDeleted > 0 || orphansDeleted > 0 {
|
||||
log.Printf("reconciler: очистка завершена (восстановлено_задач=%d, удалено_staging=%d, удалено_сирот=%d)", leasesReset, stagingDeleted, orphansDeleted)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) {
|
||||
conn, err := r.pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return false, nil, err
|
||||
}
|
||||
|
||||
var ok bool
|
||||
if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock($1)`, advisoryLockKey).Scan(&ok); err != nil {
|
||||
conn.Release()
|
||||
return false, nil, err
|
||||
}
|
||||
if !ok {
|
||||
conn.Release()
|
||||
return false, nil, nil
|
||||
}
|
||||
|
||||
release := func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
|
||||
defer cancel()
|
||||
if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка снятия блокировки: %v", err)
|
||||
}
|
||||
conn.Release()
|
||||
}
|
||||
return true, release, nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) sweepExpiredLeases(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.leases")
|
||||
defer span.End()
|
||||
|
||||
expired, err := r.attachments.ListExpiredLeases(ctx, r.batchSize)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения истёкших аренд: %v", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
r.metrics.stale.Add(ctx, int64(len(expired)))
|
||||
recovered := 0
|
||||
for _, a := range expired {
|
||||
if err := r.attachments.ResetLease(ctx, a.ID); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка сброса аренды для %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
|
||||
payload, _ := json.Marshal(domain.JobMessage{
|
||||
AttachmentID: a.ID,
|
||||
PipelineVersion: a.PipelineVersion,
|
||||
Attempt: a.Attempts + 1,
|
||||
})
|
||||
err := r.outbox.EnqueueDirect(ctx, &domain.OutboxEntry{
|
||||
AggregateType: domain.OutboxAggregateAttachment,
|
||||
AggregateID: a.ID,
|
||||
EventType: domain.OutboxEventProcess,
|
||||
Payload: payload,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка постановки в очередь восстановленной задачи %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
recovered++
|
||||
}
|
||||
r.metrics.recovered.Add(ctx, int64(recovered))
|
||||
return recovered
|
||||
}
|
||||
|
||||
// Чистим staging в S3 для готовых задач
|
||||
func (r *Reconciler) sweepStaging(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.staging")
|
||||
defer span.End()
|
||||
|
||||
toCleanup, err := r.attachments.ListStagingToCleanup(ctx, r.batchSize)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения списка staging для очистки: %v", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
deleted := 0
|
||||
for _, a := range toCleanup {
|
||||
if err := r.storage.Delete(ctx, a.StagingKey); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка удаления объекта staging %s: %v", a.StagingKey, err)
|
||||
continue
|
||||
}
|
||||
if err := r.attachments.ClearStagingKey(ctx, a.ID); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка очистки ключа staging %s: %v", a.ID, err)
|
||||
continue
|
||||
}
|
||||
deleted++
|
||||
}
|
||||
return deleted
|
||||
}
|
||||
|
||||
// Удаляем старые сиротские объекты (без записи в БД)
|
||||
func (r *Reconciler) sweepOrphans(ctx context.Context) int {
|
||||
ctx, span := telemetry.Tracer("reconciler").Start(ctx, "reconciler.orphans")
|
||||
defer span.End()
|
||||
|
||||
objects, err := r.storage.List(ctx, "chats/")
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения списка объектов: %v", err)
|
||||
return 0
|
||||
}
|
||||
if len(objects) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
cutoff := time.Now().UTC().Add(-r.gracePeriod)
|
||||
keys := make([]string, 0, len(objects))
|
||||
for _, o := range objects {
|
||||
if o.LastModified.Before(cutoff) {
|
||||
keys = append(keys, o.Key)
|
||||
}
|
||||
}
|
||||
if len(keys) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
existing, err := r.attachments.ExistingKeys(ctx, keys)
|
||||
if err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка получения существующих ключей: %v", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
inDB := make(map[string]struct{}, len(existing))
|
||||
for _, k := range existing {
|
||||
inDB[k] = struct{}{}
|
||||
}
|
||||
|
||||
deleted := 0
|
||||
for _, k := range keys {
|
||||
if _, ok := inDB[k]; ok {
|
||||
continue
|
||||
}
|
||||
if err := r.storage.Delete(ctx, k); err != nil {
|
||||
trace.SpanFromContext(ctx).SetStatus(codes.Error, "ошибка операции сверщика")
|
||||
log.Printf("reconciler: ошибка удаления сиротского объекта %s: %v", k, err)
|
||||
continue
|
||||
}
|
||||
deleted++
|
||||
}
|
||||
return deleted
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync/atomic"
|
||||
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type reconcilerMetrics struct {
|
||||
stale metric.Int64Counter
|
||||
recovered metric.Int64Counter
|
||||
leader atomic.Bool
|
||||
}
|
||||
|
||||
func newReconcilerMetrics() *reconcilerMetrics {
|
||||
meter := telemetry.Meter("reconciler")
|
||||
stale, err := meter.Int64Counter("reconciler_stale_leases_total",
|
||||
metric.WithDescription("Число наблюдений просроченных аренд"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
recovered, err := meter.Int64Counter("reconciler_recovered_jobs_total",
|
||||
metric.WithDescription("Число задач после успешного сброса аренды и записи в outbox"))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return &reconcilerMetrics{stale: stale, recovered: recovered}
|
||||
}
|
||||
|
||||
func (m *reconcilerMetrics) observeLeader() (metric.Registration, error) {
|
||||
meter := telemetry.Meter("reconciler")
|
||||
gauge, err := meter.Int64ObservableGauge("reconciler_leader",
|
||||
metric.WithDescription("1, пока этот экземпляр держит advisory lock"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return meter.RegisterCallback(func(_ context.Context, o metric.Observer) error {
|
||||
var value int64
|
||||
if m.leader.Load() {
|
||||
value = 1
|
||||
}
|
||||
o.ObserveInt64(gauge, value)
|
||||
return nil
|
||||
}, gauge)
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
// SessionService отвечает за вход, проверку и отзыв сессий.
|
||||
type SessionService struct {
|
||||
users domain.UserRepository
|
||||
sessions domain.SessionRepository
|
||||
hasher PasswordHasher
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
// NewSessionService создаёт сервис с заданным TTL сессий.
|
||||
func NewSessionService(
|
||||
users domain.UserRepository,
|
||||
sessions domain.SessionRepository,
|
||||
hasher PasswordHasher,
|
||||
ttl time.Duration,
|
||||
) *SessionService {
|
||||
return &SessionService{
|
||||
users: users,
|
||||
sessions: sessions,
|
||||
hasher: hasher,
|
||||
ttl: ttl,
|
||||
}
|
||||
}
|
||||
|
||||
// Login проверяет username/password и создаёт новую сессию.
|
||||
// Возвращает сессию, открытый токен и пользователя.
|
||||
// Открытый токен уходит клиенту; в БД сохраняется только его SHA-256.
|
||||
func (s *SessionService) Login(ctx context.Context, username, password string) (*domain.Session, string, *domain.User, error) {
|
||||
user, err := s.users.GetByUsername(ctx, username)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"}
|
||||
}
|
||||
return nil, "", nil, err
|
||||
}
|
||||
|
||||
ok, err := s.hasher.Verify(password, user.PasswordHash)
|
||||
if err != nil {
|
||||
return nil, "", nil, err
|
||||
}
|
||||
if !ok {
|
||||
return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"}
|
||||
}
|
||||
|
||||
token, err := generateToken()
|
||||
if err != nil {
|
||||
return nil, "", nil, err
|
||||
}
|
||||
|
||||
tokenHash := sha256.Sum256([]byte(token))
|
||||
now := time.Now().UTC()
|
||||
|
||||
session := &domain.Session{
|
||||
TokenHash: tokenHash[:],
|
||||
UserID: user.ID,
|
||||
ExpiresAt: now.Add(s.ttl),
|
||||
CreatedAt: now,
|
||||
}
|
||||
if err := s.sessions.Create(ctx, session); err != nil {
|
||||
return nil, "", nil, err
|
||||
}
|
||||
|
||||
return session, token, user, nil
|
||||
}
|
||||
|
||||
// Resolve проверяет токен и возвращает пользователя сессии.
|
||||
// Истёкший, отозванный или неизвестный токен - UnauthorizedError по контракту
|
||||
func (s *SessionService) Resolve(ctx context.Context, token string) (*domain.User, error) {
|
||||
if token == "" {
|
||||
return nil, &domain.UnauthorizedError{Reason: "пустой токен"}
|
||||
}
|
||||
|
||||
tokenHash := sha256.Sum256([]byte(token))
|
||||
|
||||
session, err := s.sessions.GetByTokenHash(ctx, tokenHash[:])
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return nil, &domain.UnauthorizedError{Reason: "неизвестный токен"}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !session.ExpiresAt.After(time.Now().UTC()) {
|
||||
return nil, &domain.UnauthorizedError{Reason: "сессия истекла"}
|
||||
}
|
||||
|
||||
user, err := s.users.GetByID(ctx, session.UserID)
|
||||
if err != nil {
|
||||
if errors.Is(err, domain.ErrNotFound) {
|
||||
return nil, &domain.UnauthorizedError{Reason: "пользователь сессии не найден"}
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// Revoke удаляет сессию по токену.
|
||||
func (s *SessionService) Revoke(ctx context.Context, token string) error {
|
||||
if token == "" {
|
||||
return nil
|
||||
}
|
||||
tokenHash := sha256.Sum256([]byte(token))
|
||||
return s.sessions.DeleteByTokenHash(ctx, tokenHash[:])
|
||||
}
|
||||
|
||||
// generateToken возвращает 32 случайных байта в base64url без паддинга=.
|
||||
func generateToken() (string, error) {
|
||||
buf := make([]byte, 32)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", fmt.Errorf("генерация токена: %w", err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(buf), nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type UserService struct {
|
||||
users domain.UserRepository
|
||||
hasher PasswordHasher
|
||||
}
|
||||
|
||||
func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserService {
|
||||
return &UserService{users: users, hasher: hasher}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName, password string) (*domain.User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "username",
|
||||
Message: "должен соответствовать ^[a-z0-9_]{3,32}$",
|
||||
}
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "display_name",
|
||||
Message: "не может состоять только из пробелов",
|
||||
}
|
||||
}
|
||||
if utf8.RuneCountInString(displayName) > 100 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "display_name",
|
||||
Message: "не более 100 символов",
|
||||
}
|
||||
}
|
||||
|
||||
passwordRunes := utf8.RuneCountInString(password)
|
||||
if passwordRunes < 12 || passwordRunes > 128 {
|
||||
return nil, &domain.ValidationError{
|
||||
Field: "password",
|
||||
Message: "длина 12–128 символов",
|
||||
}
|
||||
}
|
||||
|
||||
hash, err := s.hasher.Hash(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
u := &domain.User{
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
PasswordHash: hash,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
if err := s.users.Create(ctx, u); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (s *UserService) GetByID(ctx context.Context, id string) (*domain.User, error) {
|
||||
return s.users.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
func (s *UserService) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||
return s.users.GetByUsername(ctx, username)
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
stored := *chat
|
||||
stored.MemberIDs = append([]string(nil), chat.MemberIDs...)
|
||||
r.chats[chat.ID] = &stored
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
c, ok := r.chats[id]
|
||||
if !ok {
|
||||
return nil, &domain.NotFoundError{Entity: "chat", ID: id}
|
||||
}
|
||||
copy := *c
|
||||
copy.MemberIDs = append([]string(nil), c.MemberIDs...)
|
||||
return ©, nil
|
||||
}
|
||||
|
||||
func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
scope := "chats:" + userID
|
||||
|
||||
all := make([]domain.Chat, 0)
|
||||
for _, c := range r.chats {
|
||||
for _, m := range c.MemberIDs {
|
||||
if m == userID {
|
||||
copy := *c
|
||||
copy.MemberIDs = append([]string(nil), c.MemberIDs...)
|
||||
all = append(all, copy)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(all, func(a, b domain.Chat) int {
|
||||
if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) {
|
||||
return -1
|
||||
}
|
||||
if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
start := 0
|
||||
if cursor != nil {
|
||||
t, id, err := decodeCursor(*cursor, scope)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
start = len(all)
|
||||
for i, c := range all {
|
||||
if lessThan(t, id, c.CreatedAt, c.ID) {
|
||||
start = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if start >= len(all) {
|
||||
return []domain.Chat{}, nil, nil
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(all) {
|
||||
end = len(all)
|
||||
}
|
||||
page := all[start:end]
|
||||
|
||||
var next *string
|
||||
if end < len(all) {
|
||||
last := page[len(page)-1]
|
||||
c := encodeCursor(scope, last.CreatedAt, last.ID)
|
||||
next = &c
|
||||
}
|
||||
|
||||
return page, next, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
func encodeCursor(scope string, createdAt time.Time, id string) string {
|
||||
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
parts := strings.SplitN(string(data), "|", 3)
|
||||
if len(parts) != 3 {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
if parts[0] != expectedScope {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, parts[1])
|
||||
if err != nil {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
return t, parts[2], nil
|
||||
}
|
||||
|
||||
func badCursor() error {
|
||||
return &domain.ValidationError{
|
||||
Field: "cursor",
|
||||
Message: "некорректный формат",
|
||||
}
|
||||
}
|
||||
|
||||
func lessThan(aTime time.Time, aID string, bTime time.Time, bID string) bool {
|
||||
if aTime.Equal(bTime) {
|
||||
return aID < bID
|
||||
}
|
||||
return aTime.Before(bTime)
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
func (r *MessageRepo) Create(ctx context.Context, message *domain.Message) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
stored := *message
|
||||
r.messages[message.ChatID] = append(r.messages[message.ChatID], stored)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
scope := "messages:" + chatID
|
||||
|
||||
var (
|
||||
afterTime time.Time
|
||||
afterID string
|
||||
hasCursor bool
|
||||
)
|
||||
if cursor != nil {
|
||||
t, id, err := decodeCursor(*cursor, scope)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
afterTime = t
|
||||
afterID = id
|
||||
hasCursor = true
|
||||
}
|
||||
|
||||
src := r.messages[chatID]
|
||||
if len(src) == 0 {
|
||||
return []domain.Message{}, nil, nil
|
||||
}
|
||||
|
||||
all := make([]domain.Message, len(src))
|
||||
copy(all, src)
|
||||
|
||||
slices.SortFunc(all, func(a, b domain.Message) int {
|
||||
if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) {
|
||||
return -1
|
||||
}
|
||||
if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
})
|
||||
|
||||
start := 0
|
||||
if hasCursor {
|
||||
start = len(all)
|
||||
for i, m := range all {
|
||||
if lessThan(afterTime, afterID, m.CreatedAt, m.ID) {
|
||||
start = i
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if start >= len(all) {
|
||||
return []domain.Message{}, nil, nil
|
||||
}
|
||||
end := start + limit
|
||||
if end > len(all) {
|
||||
end = len(all)
|
||||
}
|
||||
page := all[start:end]
|
||||
|
||||
var next *string
|
||||
if end < len(all) {
|
||||
last := page[len(page)-1]
|
||||
c := encodeCursor(scope, last.CreatedAt, last.ID)
|
||||
next = &c
|
||||
}
|
||||
|
||||
return page, next, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type Storage struct {
|
||||
mu sync.RWMutex
|
||||
users map[string]*domain.User
|
||||
chats map[string]*domain.Chat
|
||||
messages map[string][]domain.Message
|
||||
}
|
||||
|
||||
func New() *Storage {
|
||||
return &Storage{
|
||||
users: make(map[string]*domain.User),
|
||||
chats: make(map[string]*domain.Chat),
|
||||
messages: make(map[string][]domain.Message),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) Users() *UserRepo { return &UserRepo{s} }
|
||||
func (s *Storage) Chats() *ChatRepo { return &ChatRepo{s} }
|
||||
func (s *Storage) Messages() *MessageRepo { return &MessageRepo{s} }
|
||||
|
||||
type UserRepo struct{ *Storage }
|
||||
type ChatRepo struct{ *Storage }
|
||||
type MessageRepo struct{ *Storage }
|
||||
|
||||
var _ domain.UserRepository = (*UserRepo)(nil)
|
||||
var _ domain.ChatRepository = (*ChatRepo)(nil)
|
||||
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
||||
@@ -0,0 +1,59 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
func (r *UserRepo) Create(ctx context.Context, user *domain.User) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
for _, u := range r.users {
|
||||
if u.Username == user.Username {
|
||||
return &domain.AlreadyExistsError{
|
||||
Entity: "user",
|
||||
Field: "username",
|
||||
Value: user.Username,
|
||||
}
|
||||
}
|
||||
}
|
||||
stored := *user
|
||||
r.users[user.ID] = &stored
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
u, ok := r.users[id]
|
||||
if !ok {
|
||||
return nil, &domain.NotFoundError{Entity: "user", ID: id}
|
||||
}
|
||||
copy := *u
|
||||
return ©, nil
|
||||
}
|
||||
|
||||
func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r.mu.RLock()
|
||||
defer r.mu.RUnlock()
|
||||
|
||||
for _, u := range r.users {
|
||||
if u.Username == username {
|
||||
copy := *u
|
||||
return ©, nil
|
||||
}
|
||||
}
|
||||
return nil, &domain.NotFoundError{Entity: "user", ID: username}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type AttachmentVariantRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAttachmentVariantRepo(pool *pgxpool.Pool) *AttachmentVariantRepo {
|
||||
return &AttachmentVariantRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *AttachmentVariantRepo) Upsert(ctx context.Context, tx domain.Tx, v *domain.AttachmentVariant) error {
|
||||
const q = `
|
||||
INSERT INTO attachment_variants
|
||||
(attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at)
|
||||
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (attachment_id, name) DO NOTHING
|
||||
`
|
||||
return tx.Exec(ctx, q, v.AttachmentID, v.Name, v.ObjectKey, v.ContentType, v.SizeBytes, v.Width, v.Height, v.SHA256, v.CreatedAt)
|
||||
}
|
||||
|
||||
func (r *AttachmentVariantRepo) ListByAttachment(ctx context.Context, id string) ([]domain.AttachmentVariant, error) {
|
||||
const q = `SELECT attachment_id, name, object_key, content_type, size_bytes, width, height, sha256, created_at FROM attachment_variants WHERE attachment_id = $1::uuid`
|
||||
rows, err := r.pool.Query(ctx, q, id)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []domain.AttachmentVariant
|
||||
for rows.Next() {
|
||||
var v domain.AttachmentVariant
|
||||
if err := rows.Scan(&v.AttachmentID, &v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
out = append(out, v)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
var _ domain.AttachmentVariantRepository = (*AttachmentVariantRepo)(nil)
|
||||
@@ -0,0 +1,352 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type AttachmentRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewAttachmentRepo(pool *pgxpool.Pool) *AttachmentRepo {
|
||||
return &AttachmentRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error {
|
||||
const q = `
|
||||
INSERT INTO attachments
|
||||
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code, created_at, updated_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, $10, $10)`
|
||||
_, err := r.pool.Exec(ctx, q,
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes,
|
||||
a.SHA256, a.ObjectKey, string(a.Status), a.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if uniqueViolation(err) {
|
||||
return &domain.AlreadyExistsError{
|
||||
Entity: "attachment",
|
||||
Field: "object_key",
|
||||
Value: a.ObjectKey,
|
||||
}
|
||||
}
|
||||
if foreignKeyViolation(err) {
|
||||
return &domain.NotFoundError{Entity: "chat", ID: a.ChatID}
|
||||
}
|
||||
if checkViolation(err) {
|
||||
return &domain.ValidationError{
|
||||
Field: "attachment",
|
||||
Message: "нарушено ограничение целостности",
|
||||
}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) CreateInTx(ctx context.Context, tx domain.Tx, a *domain.Attachment) error {
|
||||
const q = `
|
||||
INSERT INTO attachments
|
||||
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code,
|
||||
attempts, pipeline_version, staging_key,
|
||||
created_at, updated_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL,
|
||||
0, $10, $11, $12, $12)
|
||||
`
|
||||
return tx.Exec(ctx, q,
|
||||
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes,
|
||||
a.SHA256, a.ObjectKey, string(a.Status),
|
||||
a.PipelineVersion, a.StagingKey, a.CreatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) {
|
||||
const q = `
|
||||
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code, created_at, updated_at,
|
||||
attempts, next_attempt_at, locked_until, pipeline_version, staging_key
|
||||
FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
a := &domain.Attachment{}
|
||||
var status string
|
||||
err := r.pool.QueryRow(ctx, q, id).
|
||||
Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType,
|
||||
&a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode,
|
||||
&a.CreatedAt, &a.UpdatedAt,
|
||||
&a.Attempts, &a.NextAttemptAt, &a.LockedUntil, &a.PipelineVersion, &a.StagingKey)
|
||||
if err != nil {
|
||||
return nil, notFound(err, "attachment", id)
|
||||
}
|
||||
a.Status = domain.AttachmentStatus(status)
|
||||
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT name, object_key, content_type, size_bytes, width, height, sha256, created_at
|
||||
FROM attachment_variants
|
||||
WHERE attachment_id = $1::uuid
|
||||
ORDER BY name
|
||||
`, id)
|
||||
if err != nil {
|
||||
log.Printf("GetByID: ошибка запроса variants: %v", err)
|
||||
} else {
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
var v domain.AttachmentVariant
|
||||
v.AttachmentID = id
|
||||
if err := rows.Scan(&v.Name, &v.ObjectKey, &v.ContentType, &v.SizeBytes, &v.Width, &v.Height, &v.SHA256, &v.CreatedAt); err != nil {
|
||||
log.Printf("GetByID: ошибка Scan variant: %v", err)
|
||||
} else {
|
||||
a.Variants = append(a.Variants, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'ready', updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'pending'
|
||||
`
|
||||
tag, err := r.pool.Exec(ctx, q, id)
|
||||
if err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
if tag.RowsAffected() > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var status string
|
||||
err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id).
|
||||
Scan(&status)
|
||||
if err != nil {
|
||||
return notFound(err, "attachment", id)
|
||||
}
|
||||
switch domain.AttachmentStatus(status) {
|
||||
case domain.AttachmentStatusReady:
|
||||
return nil
|
||||
case domain.AttachmentStatusFailed:
|
||||
return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"}
|
||||
default:
|
||||
return &domain.ConflictError{Reason: "некорректное состояние вложения"}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error {
|
||||
if errorCode == "" {
|
||||
return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"}
|
||||
}
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'failed', error_code = $2, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'pending'
|
||||
`
|
||||
tag, err := r.pool.Exec(ctx, q, id, errorCode)
|
||||
if err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
if tag.RowsAffected() > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var status string
|
||||
err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id).
|
||||
Scan(&status)
|
||||
if err != nil {
|
||||
return notFound(err, "attachment", id)
|
||||
}
|
||||
switch domain.AttachmentStatus(status) {
|
||||
case domain.AttachmentStatusFailed, domain.AttachmentStatusReady:
|
||||
return nil
|
||||
default:
|
||||
return &domain.ConflictError{Reason: "некорректное состояние вложения"}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) {
|
||||
if len(keys) == 0 {
|
||||
return []string{}, nil
|
||||
}
|
||||
const q = `SELECT object_key FROM attachments WHERE object_key = ANY($1::text[])`
|
||||
rows, err := r.pool.Query(ctx, q, keys)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
out := make([]string, 0, len(keys))
|
||||
for rows.Next() {
|
||||
var k string
|
||||
if err := rows.Scan(&k); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
out = append(out, k)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ClaimForProcessing(ctx context.Context, id string, version int, lease time.Duration) (*domain.Attachment, error) {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'processing', locked_until = now() + $2, updated_at = now(), attempts = attempts + 1
|
||||
WHERE id = $1::uuid AND status = 'queued' AND pipeline_version = $3
|
||||
RETURNING id, chat_id, staging_key, pipeline_version, object_key, content_type, attempts, created_at
|
||||
`
|
||||
var a domain.Attachment
|
||||
err := r.pool.QueryRow(ctx, q, id, lease, version).Scan(&a.ID, &a.ChatID, &a.StagingKey, &a.PipelineVersion, &a.ObjectKey, &a.ContentType, &a.Attempts, &a.CreatedAt)
|
||||
if err != nil {
|
||||
if err == pgx.ErrNoRows {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, mapError(err)
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) CompleteJob(ctx context.Context, id string, pipelineVersion int, variants []domain.AttachmentVariant) (bool, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, mapError(err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'ready', locked_until = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND pipeline_version = $2 AND status = 'processing'
|
||||
`
|
||||
tag, err := tx.Exec(ctx, q, id, pipelineVersion)
|
||||
if err != nil {
|
||||
return false, mapError(err)
|
||||
}
|
||||
if tag.RowsAffected() == 0 {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
variantRepo := NewAttachmentVariantRepo(r.pool)
|
||||
for _, v := range variants {
|
||||
if err := variantRepo.Upsert(ctx, &pgxTx{tx: tx}, &v); err != nil {
|
||||
log.Printf("CompleteJob: ошибка Upsert %s: %v", v.Name, err)
|
||||
return false, err
|
||||
} else {
|
||||
log.Printf("CompleteJob: успешно добавлен вариант %s", v.Name)
|
||||
}
|
||||
}
|
||||
|
||||
return true, mapError(tx.Commit(ctx))
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) FailJob(ctx context.Context, id string, errorCode string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'failed', error_code = $2, locked_until = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'processing'
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, id, errorCode)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ScheduleRetry(ctx context.Context, id string, nextAttemptAt time.Time, errorCode string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', error_code = NULL, next_attempt_at = $2, locked_until = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'processing'
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, id, nextAttemptAt)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ResetForReplay(ctx context.Context, tx domain.Tx, id string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', attempts = 0, error_code = NULL, locked_until = NULL, next_attempt_at = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'failed'
|
||||
`
|
||||
return tx.Exec(ctx, q, id)
|
||||
}
|
||||
|
||||
// для сверщика.
|
||||
func (r *AttachmentRepo) ListExpiredLeases(ctx context.Context, limit int) ([]domain.Attachment, error) {
|
||||
const q = `
|
||||
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256,
|
||||
object_key, status, error_code, created_at, updated_at,
|
||||
attempts, next_attempt_at, locked_until, pipeline_version, staging_key
|
||||
FROM attachments
|
||||
WHERE status = 'processing' AND locked_until < now()
|
||||
ORDER BY locked_until
|
||||
LIMIT $1
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []domain.Attachment
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
var status string
|
||||
err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType,
|
||||
&a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode,
|
||||
&a.CreatedAt, &a.UpdatedAt, &a.Attempts, &a.NextAttemptAt, &a.LockedUntil,
|
||||
&a.PipelineVersion, &a.StagingKey)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
a.Status = domain.AttachmentStatus(status)
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ResetLease(ctx context.Context, id string) error {
|
||||
const q = `
|
||||
UPDATE attachments
|
||||
SET status = 'queued', locked_until = NULL, updated_at = now()
|
||||
WHERE id = $1::uuid AND status = 'processing' AND locked_until < now()
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, id)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ListStagingToCleanup(ctx context.Context, limit int) ([]domain.Attachment, error) {
|
||||
const q = `
|
||||
SELECT id, staging_key
|
||||
FROM attachments
|
||||
WHERE status = 'ready' AND staging_key IS NOT NULL
|
||||
LIMIT $1
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var out []domain.Attachment
|
||||
for rows.Next() {
|
||||
var a domain.Attachment
|
||||
if err := rows.Scan(&a.ID, &a.StagingKey); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *AttachmentRepo) ClearStagingKey(ctx context.Context, id string) error {
|
||||
const q = `UPDATE attachments SET staging_key = NULL WHERE id = $1::uuid`
|
||||
_, err := r.pool.Exec(ctx, q, id)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
var _ domain.AttachmentRepository = (*AttachmentRepo)(nil)
|
||||
@@ -0,0 +1,195 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type ChatRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatRepo(pool *pgxpool.Pool) *ChatRepo {
|
||||
return &ChatRepo{pool: pool}
|
||||
}
|
||||
|
||||
// Create атомарно создаёт чат и его участников.
|
||||
func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
const qChat = `INSERT INTO chats (id, title, created_at) VALUES ($1::uuid, $2, $3)`
|
||||
if _, err := tx.Exec(ctx, qChat, chat.ID, chat.Title, chat.CreatedAt); err != nil {
|
||||
if checkViolation(err) {
|
||||
return &domain.ValidationError{Field: "chat", Message: "нарушено ограничение целостности"}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
const qMember = `INSERT INTO chat_members (chat_id, user_id) VALUES ($1::uuid, $2::uuid)`
|
||||
for _, uid := range chat.MemberIDs {
|
||||
if _, err := tx.Exec(ctx, qMember, chat.ID, uid); err != nil {
|
||||
if foreignKeyViolation(err) {
|
||||
return &domain.ValidationError{
|
||||
Field: "member_ids",
|
||||
Message: "неизвестный участник " + uid,
|
||||
}
|
||||
}
|
||||
if uniqueViolation(err) {
|
||||
return &domain.ValidationError{
|
||||
Field: "member_ids",
|
||||
Message: "дубликаты запрещены",
|
||||
}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
}
|
||||
|
||||
return mapError(tx.Commit(ctx))
|
||||
}
|
||||
|
||||
func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) {
|
||||
const q = `SELECT id, title, created_at FROM chats WHERE id = $1::uuid`
|
||||
c := &domain.Chat{}
|
||||
err := r.pool.QueryRow(ctx, q, id).Scan(&c.ID, &c.Title, &c.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err, "chat", id)
|
||||
}
|
||||
members, err := r.loadMembers(ctx, []string{c.ID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.MemberIDs = members[c.ID]
|
||||
if c.MemberIDs == nil {
|
||||
c.MemberIDs = []string{}
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) {
|
||||
scope := "chats:" + userID
|
||||
|
||||
var (
|
||||
afterTime time.Time
|
||||
afterID string
|
||||
hasCursor bool
|
||||
)
|
||||
if cursor != nil {
|
||||
t, id, err := decodeCursor(*cursor, scope)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
afterTime = t
|
||||
afterID = id
|
||||
hasCursor = true
|
||||
}
|
||||
|
||||
var (
|
||||
rows pgx.Rows
|
||||
err error
|
||||
)
|
||||
if hasCursor {
|
||||
const q = `
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1::uuid
|
||||
AND (c.created_at, c.id) > ($2, $3::uuid)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4
|
||||
`
|
||||
rows, err = r.pool.Query(ctx, q, userID, afterTime, afterID, limit+1)
|
||||
} else {
|
||||
const q = `
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1::uuid
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $2
|
||||
`
|
||||
rows, err = r.pool.Query(ctx, q, userID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
page := make([]domain.Chat, 0, limit)
|
||||
for rows.Next() {
|
||||
var c domain.Chat
|
||||
if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, nil, mapError(err)
|
||||
}
|
||||
page = append(page, c)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, nil, mapError(err)
|
||||
}
|
||||
|
||||
var next *string
|
||||
if len(page) > limit {
|
||||
page = page[:limit]
|
||||
last := page[len(page)-1]
|
||||
c := encodeCursor(scope, last.CreatedAt, last.ID)
|
||||
next = &c
|
||||
}
|
||||
|
||||
if len(page) > 0 {
|
||||
ids := make([]string, len(page))
|
||||
for i := range page {
|
||||
ids[i] = page[i].ID
|
||||
}
|
||||
members, err := r.loadMembers(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for i := range page {
|
||||
if m, ok := members[page[i].ID]; ok {
|
||||
page[i].MemberIDs = m
|
||||
} else {
|
||||
page[i].MemberIDs = []string{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return page, next, nil
|
||||
}
|
||||
|
||||
// loadMembers одним запросом собирает участников для указанных чатов.
|
||||
func (r *ChatRepo) loadMembers(ctx context.Context, chatIDs []string) (map[string][]string, error) {
|
||||
if len(chatIDs) == 0 {
|
||||
return map[string][]string{}, nil
|
||||
}
|
||||
const q = `
|
||||
SELECT chat_id, user_id
|
||||
FROM chat_members
|
||||
WHERE chat_id = ANY($1::uuid[])
|
||||
ORDER BY chat_id, user_id
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, chatIDs)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make(map[string][]string, len(chatIDs))
|
||||
for rows.Next() {
|
||||
var chatID, userID string
|
||||
if err := rows.Scan(&chatID, &userID); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
result[chatID] = append(result[chatID], userID)
|
||||
}
|
||||
return result, mapError(rows.Err())
|
||||
}
|
||||
|
||||
var _ domain.ChatRepository = (*ChatRepo)(nil)
|
||||
@@ -0,0 +1,46 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Взял за основу что в inmemery делал
|
||||
func encodeCursor(scope string, createdAt time.Time, id string) string {
|
||||
raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id
|
||||
return base64.RawURLEncoding.EncodeToString([]byte(raw))
|
||||
}
|
||||
|
||||
func decodeCursor(cursor, expectedScope string) (time.Time, string, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(cursor)
|
||||
if err != nil {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
parts := strings.SplitN(string(data), "|", 3)
|
||||
if len(parts) != 3 {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
if parts[0] != expectedScope {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
t, err := time.Parse(time.RFC3339Nano, parts[1])
|
||||
if err != nil {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
if _, err := uuid.Parse(parts[2]); err != nil {
|
||||
return time.Time{}, "", badCursor()
|
||||
}
|
||||
return t, parts[2], nil
|
||||
}
|
||||
|
||||
func badCursor() error {
|
||||
return &domain.ValidationError{
|
||||
Field: "cursor",
|
||||
Message: "некорректный формат",
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
|
||||
"high-loud-apps-labs/migrations"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
MaxConns int32
|
||||
MinConns int32
|
||||
MaxConnLifetime time.Duration
|
||||
MaxConnIdleTime time.Duration
|
||||
ConnectTimeout time.Duration
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, cfg Config) (*pgxpool.Pool, error) {
|
||||
if cfg.URL == "" {
|
||||
return nil, fmt.Errorf("DATABASE_URL пустой")
|
||||
}
|
||||
|
||||
poolCfg, err := pgxpool.ParseConfig(cfg.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("разбор DATABASE_URL: %w", err)
|
||||
}
|
||||
if cfg.MaxConns > 0 {
|
||||
poolCfg.MaxConns = cfg.MaxConns
|
||||
}
|
||||
if cfg.MinConns > 0 {
|
||||
poolCfg.MinConns = cfg.MinConns
|
||||
}
|
||||
if cfg.MaxConnLifetime > 0 {
|
||||
poolCfg.MaxConnLifetime = cfg.MaxConnLifetime
|
||||
}
|
||||
if cfg.MaxConnIdleTime > 0 {
|
||||
poolCfg.MaxConnIdleTime = cfg.MaxConnIdleTime
|
||||
}
|
||||
if cfg.ConnectTimeout > 0 {
|
||||
poolCfg.ConnConfig.ConnectTimeout = cfg.ConnectTimeout
|
||||
}
|
||||
poolCfg.HealthCheckPeriod = 10 * time.Second
|
||||
|
||||
tracer, err := newPoolTracer()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("метрики пула: %w", err)
|
||||
}
|
||||
poolCfg.ConnConfig.Tracer = tracer
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("создание пула: %w", err)
|
||||
}
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := pool.Ping(pingCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping БД: %w", err)
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error {
|
||||
db := stdlib.OpenDBFromPool(pool)
|
||||
|
||||
goose.SetBaseFS(migrations.FS)
|
||||
if err := goose.SetDialect("postgres"); err != nil {
|
||||
return fmt.Errorf("goose dialect: %w", err)
|
||||
}
|
||||
if err := goose.UpContext(ctx, db, "."); err != nil {
|
||||
return fmt.Errorf("применение миграций: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type coded interface {
|
||||
Code() domain.ErrorCode
|
||||
}
|
||||
|
||||
// mapError переводит ошибки драйвера в доменные.
|
||||
func mapError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var c coded
|
||||
if errors.As(err, &c) {
|
||||
return err
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return err
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return &domain.UnavailableError{Reason: "превышено время ожидания БД"}
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return &domain.UnavailableError{Reason: "запрос отменён"}
|
||||
}
|
||||
return &domain.UnavailableError{Reason: err.Error()}
|
||||
}
|
||||
|
||||
func notFound(err error, entity, id string) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return &domain.NotFoundError{Entity: entity, ID: id}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func uniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
return pgErr.Code == pgerrcode.UniqueViolation
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func foreignKeyViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
return pgErr.Code == pgerrcode.ForeignKeyViolation
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func checkViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
return pgErr.Code == pgerrcode.CheckViolation
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type FailedJobRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewFailedJobRepo(pool *pgxpool.Pool) *FailedJobRepo {
|
||||
return &FailedJobRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *FailedJobRepo) Record(ctx context.Context, f *domain.FailedJob) error {
|
||||
if f.ID == "" {
|
||||
f.ID = uuid.NewString()
|
||||
}
|
||||
if f.LastErrorAt.IsZero() {
|
||||
f.LastErrorAt = time.Now().UTC()
|
||||
}
|
||||
const q = `
|
||||
INSERT INTO failed_jobs (id, attachment_id, reason_code, attempts, last_error_at, created_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3, $4, $5, $5)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, f.ID, f.AttachmentID, f.ReasonCode, f.Attempts, f.LastErrorAt)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *FailedJobRepo) Resolve(ctx context.Context, attachmentID string, resolution string) error {
|
||||
const q = `UPDATE failed_jobs SET resolved_at = now(), resolution = $2 WHERE attachment_id = $1::uuid AND resolved_at IS NULL`
|
||||
_, err := r.pool.Exec(ctx, q, attachmentID, resolution)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *FailedJobRepo) ResolveInTx(ctx context.Context, tx domain.Tx, id string) error {
|
||||
const q = `
|
||||
UPDATE failed_jobs
|
||||
SET resolved_at = now(), resolution = 'manual_replay'
|
||||
WHERE attachment_id = $1::uuid AND resolved_at IS NULL
|
||||
`
|
||||
return tx.Exec(ctx, q, id)
|
||||
}
|
||||
|
||||
var _ domain.FailedJobRepository = (*FailedJobRepo)(nil)
|
||||
@@ -0,0 +1,321 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type MessageRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewMessageRepo(pool *pgxpool.Pool) *MessageRepo {
|
||||
return &MessageRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *MessageRepo) Create(ctx context.Context, m *domain.Message) error {
|
||||
const q = `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt)
|
||||
if err != nil {
|
||||
if foreignKeyViolation(err) {
|
||||
return &domain.NotFoundError{Entity: "chat", ID: m.ChatID}
|
||||
}
|
||||
if checkViolation(err) {
|
||||
return &domain.ValidationError{Field: "text", Message: "нарушено ограничение"}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) CreateIdempotent(
|
||||
ctx context.Context,
|
||||
scope domain.IdempotencyScope,
|
||||
requestHash []byte,
|
||||
fresh *domain.Message,
|
||||
freshResponse []byte,
|
||||
ttl time.Duration,
|
||||
) (*domain.CreateIdempotentResult, error) {
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
// 1) Убираем просроченную запись, чтобы ON CONFLICT сработал
|
||||
// только на живой, а не на истёкшей строке.
|
||||
if _, err := tx.Exec(ctx, `
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4
|
||||
AND expires_at <= now()
|
||||
`, scope.UserID, scope.Method, scope.Path, scope.Key); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
|
||||
// 2) Резервируем слот. Если параллельная реплика уже вставила —
|
||||
// Postgres заблокирует конфликтный INSERT и мы увидим RowsAffected=0
|
||||
// только после того, как конкурент закоммитит или откатится.
|
||||
expiresAt := time.Now().UTC().Add(ttl)
|
||||
tag, err := tx.Exec(ctx, `
|
||||
INSERT INTO idempotency_keys
|
||||
(user_id, method, path, key, request_hash, response_status, response_body, expires_at)
|
||||
VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (user_id, method, path, key) DO NOTHING
|
||||
`, scope.UserID, scope.Method, scope.Path, scope.Key,
|
||||
requestHash, http.StatusCreated, freshResponse, expiresAt)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
|
||||
// 3) Слот занят — читаем сохранённый ответ в этой же транзакции.
|
||||
if tag.RowsAffected() == 0 {
|
||||
var (
|
||||
storedHash []byte
|
||||
storedStatus int
|
||||
storedBody []byte
|
||||
)
|
||||
err := tx.QueryRow(ctx, `
|
||||
SELECT request_hash, response_status, response_body
|
||||
FROM idempotency_keys
|
||||
WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4
|
||||
`, scope.UserID, scope.Method, scope.Path, scope.Key).
|
||||
Scan(&storedHash, &storedStatus, &storedBody)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(storedHash, requestHash) {
|
||||
return nil, &domain.IdempotencyConflictError{Key: scope.Key}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
return &domain.CreateIdempotentResult{
|
||||
Status: storedStatus,
|
||||
Body: storedBody,
|
||||
Replayed: true,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// 4) Слот наш — валидируем вложения, вставляем сообщение, линкуем
|
||||
// вложения. Всё в одной транзакции: при ошибке на любом шаге
|
||||
// откатывается и idempotency-ключ, и сообщение, и связи.
|
||||
if err := r.validateAttachments(ctx, tx, fresh.ChatID, fresh.AttachmentIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := tx.Exec(ctx, `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5)
|
||||
`, fresh.ID, fresh.ChatID, fresh.SenderID, fresh.Text, fresh.CreatedAt); err != nil {
|
||||
if foreignKeyViolation(err) {
|
||||
return nil, &domain.NotFoundError{Entity: "chat", ID: fresh.ChatID}
|
||||
}
|
||||
if checkViolation(err) {
|
||||
return nil, &domain.ValidationError{Field: "text", Message: "нарушено ограничение"}
|
||||
}
|
||||
return nil, mapError(err)
|
||||
}
|
||||
|
||||
if err := r.linkAttachments(ctx, tx, fresh.ID, fresh.AttachmentIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
return &domain.CreateIdempotentResult{
|
||||
Status: http.StatusCreated,
|
||||
Body: freshResponse,
|
||||
Replayed: false,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) {
|
||||
scope := "messages:" + chatID
|
||||
|
||||
var (
|
||||
afterTime time.Time
|
||||
afterID string
|
||||
hasCursor bool
|
||||
)
|
||||
if cursor != nil {
|
||||
t, id, err := decodeCursor(*cursor, scope)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
afterTime = t
|
||||
afterID = id
|
||||
hasCursor = true
|
||||
}
|
||||
|
||||
var (
|
||||
rows pgx.Rows
|
||||
err error
|
||||
)
|
||||
if hasCursor {
|
||||
const q = `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1::uuid
|
||||
AND (created_at, id) > ($2, $3::uuid)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4
|
||||
`
|
||||
rows, err = r.pool.Query(ctx, q, chatID, afterTime, afterID, limit+1)
|
||||
} else {
|
||||
const q = `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1::uuid
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2
|
||||
`
|
||||
rows, err = r.pool.Query(ctx, q, chatID, limit+1)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
page := make([]domain.Message, 0, limit)
|
||||
for rows.Next() {
|
||||
var m domain.Message
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
page = append(page, m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var next *string
|
||||
if len(page) > limit {
|
||||
page = page[:limit]
|
||||
last := page[len(page)-1]
|
||||
c := encodeCursor(scope, last.CreatedAt, last.ID)
|
||||
next = &c
|
||||
}
|
||||
|
||||
if len(page) > 0 {
|
||||
msgIDs := make([]string, len(page))
|
||||
for i := range page {
|
||||
msgIDs[i] = page[i].ID
|
||||
}
|
||||
links, err := r.loadAttachmentIDs(ctx, msgIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for i := range page {
|
||||
if ids, ok := links[page[i].ID]; ok {
|
||||
page[i].AttachmentIDs = ids
|
||||
} else {
|
||||
page[i].AttachmentIDs = []string{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return page, next, nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID string, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
const q = `SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])`
|
||||
rows, err := tx.Query(ctx, q, ids)
|
||||
if err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type info struct {
|
||||
chatID string
|
||||
status domain.AttachmentStatus
|
||||
}
|
||||
found := make(map[string]info, len(ids))
|
||||
for rows.Next() {
|
||||
var id, chID, st string
|
||||
if err := rows.Scan(&id, &chID, &st); err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
found[id] = info{chatID: chID, status: domain.AttachmentStatus(st)}
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
for _, id := range ids {
|
||||
a, ok := found[id]
|
||||
if !ok || a.chatID != chatID {
|
||||
return &domain.NotFoundError{Entity: "attachment", ID: id}
|
||||
}
|
||||
if a.status == domain.AttachmentStatusFailed {
|
||||
return &domain.ConflictError{Reason: "вложение " + id + " в состоянии failed"}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID string, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
const q = `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)`
|
||||
for i, id := range ids {
|
||||
if _, err := tx.Exec(ctx, q, messageID, id, int16(i)); err != nil {
|
||||
if foreignKeyViolation(err) {
|
||||
return &domain.NotFoundError{Entity: "attachment", ID: id}
|
||||
}
|
||||
if uniqueViolation(err) {
|
||||
return &domain.ValidationError{Field: "attachment_ids", Message: "дубликаты запрещены"}
|
||||
}
|
||||
if checkViolation(err) {
|
||||
return &domain.ValidationError{Field: "attachment_ids", Message: "нарушено ограничение"}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MessageRepo) loadAttachmentIDs(ctx context.Context, messageIDs []string) (map[string][]string, error) {
|
||||
if len(messageIDs) == 0 {
|
||||
return map[string][]string{}, nil
|
||||
}
|
||||
const q = `
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY($1::uuid[])
|
||||
ORDER BY message_id, position
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, messageIDs)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
result := make(map[string][]string, len(messageIDs))
|
||||
for rows.Next() {
|
||||
var msgID, attID string
|
||||
if err := rows.Scan(&msgID, &attID); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
result[msgID] = append(result[msgID], attID)
|
||||
}
|
||||
return result, mapError(rows.Err())
|
||||
}
|
||||
|
||||
var _ domain.MessageRepository = (*MessageRepo)(nil)
|
||||
@@ -0,0 +1,123 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type OutboxRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewOutboxRepo(pool *pgxpool.Pool) *OutboxRepo {
|
||||
return &OutboxRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) Enqueue(ctx context.Context, tx domain.Tx, e *domain.OutboxEntry) error {
|
||||
if e.AggregateType == "" || e.AggregateID == "" || e.EventType == "" {
|
||||
return &domain.ValidationError{
|
||||
Field: "outbox",
|
||||
Message: "aggregate_type, aggregate_id и event_type обязательны",
|
||||
}
|
||||
}
|
||||
if len(e.Payload) == 0 {
|
||||
return &domain.ValidationError{
|
||||
Field: "outbox.payload",
|
||||
Message: "не может быть пустым",
|
||||
}
|
||||
}
|
||||
|
||||
if e.ID == "" {
|
||||
e.ID = uuid.NewString()
|
||||
}
|
||||
if e.CreatedAt.IsZero() {
|
||||
e.CreatedAt = time.Now().UTC()
|
||||
}
|
||||
|
||||
if e.Headers == nil {
|
||||
e.Headers = telemetry.TraceHeaders(ctx)
|
||||
}
|
||||
const q = `
|
||||
INSERT INTO outbox
|
||||
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at, headers)
|
||||
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8, $9::jsonb)
|
||||
`
|
||||
if err := tx.Exec(ctx, q,
|
||||
e.ID, e.AggregateType, e.AggregateID, e.EventType,
|
||||
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, e.Headers,
|
||||
); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) ClaimBatch(ctx context.Context, limit int) ([]domain.OutboxEntry, error) {
|
||||
const q = `
|
||||
WITH claimed AS (
|
||||
SELECT id FROM outbox
|
||||
WHERE published_at IS NULL AND created_at <= now()
|
||||
ORDER BY created_at
|
||||
LIMIT $1
|
||||
FOR UPDATE SKIP LOCKED
|
||||
)
|
||||
UPDATE outbox o
|
||||
SET attempts = attempts + 1
|
||||
FROM claimed c
|
||||
WHERE o.id = c.id
|
||||
RETURNING o.id, o.aggregate_type, o.aggregate_id, o.event_type, o.payload, o.attempts, o.created_at, o.published_at, o.headers
|
||||
`
|
||||
rows, err := r.pool.Query(ctx, q, limit)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var entries []domain.OutboxEntry
|
||||
for rows.Next() {
|
||||
var e domain.OutboxEntry
|
||||
if err := rows.Scan(
|
||||
&e.ID, &e.AggregateType, &e.AggregateID, &e.EventType,
|
||||
&e.Payload, &e.Attempts, &e.CreatedAt, &e.PublishedAt, &e.Headers,
|
||||
); err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
entries = append(entries, e)
|
||||
}
|
||||
return entries, mapError(rows.Err())
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) EnqueueDirect(ctx context.Context, e *domain.OutboxEntry) error {
|
||||
if e.ID == "" {
|
||||
e.ID = uuid.NewString()
|
||||
}
|
||||
if e.Headers == nil {
|
||||
e.Headers = telemetry.TraceHeaders(ctx)
|
||||
}
|
||||
const q = `
|
||||
INSERT INTO outbox
|
||||
(id, aggregate_type, aggregate_id, event_type, payload, attempts, created_at, published_at, headers)
|
||||
VALUES ($1::uuid, $2, $3::uuid, $4, $5::jsonb, $6, $7, $8, $9::jsonb)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q,
|
||||
e.ID, e.AggregateType, e.AggregateID, e.EventType,
|
||||
string(e.Payload), e.Attempts, e.CreatedAt, e.PublishedAt, e.Headers,
|
||||
)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (r *OutboxRepo) MarkPublished(ctx context.Context, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
const q = `UPDATE outbox SET published_at = now() WHERE id = ANY($1::uuid[])`
|
||||
_, err := r.pool.Exec(ctx, q, ids)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
var _ domain.OutboxRepository = (*OutboxRepo)(nil)
|
||||
@@ -0,0 +1,59 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type SessionRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewSessionRepo(pool *pgxpool.Pool) *SessionRepo {
|
||||
return &SessionRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error {
|
||||
const q = `
|
||||
INSERT INTO sessions (token_hash, user_id, expires_at, created_at)
|
||||
VALUES ($1, $2::uuid, $3, $4)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q, s.TokenHash, s.UserID, s.ExpiresAt, s.CreatedAt)
|
||||
if err != nil {
|
||||
if uniqueViolation(err) {
|
||||
return &domain.AlreadyExistsError{
|
||||
Entity: "session",
|
||||
Field: "token_hash",
|
||||
Value: "",
|
||||
}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) {
|
||||
const q = `
|
||||
SELECT token_hash, user_id, expires_at, created_at
|
||||
FROM sessions
|
||||
WHERE token_hash = $1
|
||||
`
|
||||
s := &domain.Session{}
|
||||
err := r.pool.QueryRow(ctx, q, tokenHash).
|
||||
Scan(&s.TokenHash, &s.UserID, &s.ExpiresAt, &s.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err, "session", "")
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error {
|
||||
const q = `DELETE FROM sessions WHERE token_hash = $1`
|
||||
_, err := r.pool.Exec(ctx, q, tokenHash)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
var _ domain.SessionRepository = (*SessionRepo)(nil)
|
||||
@@ -0,0 +1,22 @@
|
||||
package postgres
|
||||
|
||||
import "github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
type Storage struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool) *Storage {
|
||||
return &Storage{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Storage) Pool() *pgxpool.Pool { return s.pool }
|
||||
|
||||
func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} }
|
||||
func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} }
|
||||
func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} }
|
||||
func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} }
|
||||
func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} }
|
||||
func (s *Storage) Outbox() *OutboxRepo { return &OutboxRepo{pool: s.pool} }
|
||||
func (s *Storage) Variants() *AttachmentVariantRepo { return &AttachmentVariantRepo{pool: s.pool} }
|
||||
func (s *Storage) FailedJobs() *FailedJobRepo { return &FailedJobRepo{pool: s.pool} }
|
||||
@@ -0,0 +1,99 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type poolTracer struct {
|
||||
tracer trace.Tracer
|
||||
duration metric.Float64Histogram
|
||||
waiting metric.Int64UpDownCounter
|
||||
}
|
||||
|
||||
func newPoolTracer() (*poolTracer, error) {
|
||||
meter := telemetry.Meter("storage.postgres")
|
||||
duration, err := meter.Float64Histogram("db_pool_acquire_duration_seconds",
|
||||
metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(0.0001, 0.001, 0.005, 0.01, 0.05, 0.1, 0.5, 1, 5),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
waiting, err := meter.Int64UpDownCounter("db_pool_waiting",
|
||||
metric.WithDescription("Число незавершённых вызовов Acquire"))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &poolTracer{tracer: telemetry.Tracer("storage.postgres"), duration: duration, waiting: waiting}, nil
|
||||
}
|
||||
|
||||
type acquireStartKey struct{}
|
||||
|
||||
func (t *poolTracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
||||
ctx, _ = t.tracer.Start(ctx, "postgresql.acquire")
|
||||
t.waiting.Add(ctx, 1)
|
||||
return context.WithValue(ctx, acquireStartKey{}, time.Now())
|
||||
}
|
||||
|
||||
func (t *poolTracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, data pgxpool.TraceAcquireEndData) {
|
||||
t.waiting.Add(ctx, -1)
|
||||
t.duration.Record(ctx, time.Since(ctx.Value(acquireStartKey{}).(time.Time)).Seconds())
|
||||
endSpan(ctx, data.Err)
|
||||
}
|
||||
|
||||
func (t *poolTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
|
||||
// Передаём только известный SQL-глагол, без текста запроса и параметров.
|
||||
op := "QUERY"
|
||||
sql := strings.TrimSpace(data.SQL)
|
||||
if i := strings.IndexAny(sql, " \r\n\t;("); i >= 0 {
|
||||
sql = sql[:i]
|
||||
}
|
||||
switch verb := strings.ToUpper(sql); verb {
|
||||
case "SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK", "WITH":
|
||||
op = verb
|
||||
}
|
||||
ctx, _ = t.tracer.Start(ctx, "postgresql."+op, trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system.name", "postgresql"), attribute.String("db.operation.name", op)))
|
||||
return ctx
|
||||
}
|
||||
|
||||
func (t *poolTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
||||
endSpan(ctx, data.Err)
|
||||
}
|
||||
|
||||
func endSpan(ctx context.Context, err error) {
|
||||
span := trace.SpanFromContext(ctx)
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "операция PostgreSQL не выполнена")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
|
||||
func ObservePool(pool *pgxpool.Pool) (metric.Registration, error) {
|
||||
meter := telemetry.Meter("storage.postgres")
|
||||
connections, err := meter.Int64ObservableGauge("db_pool_connections")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return meter.RegisterCallback(func(_ context.Context, observer metric.Observer) error {
|
||||
stat := pool.Stat()
|
||||
observe := func(state string, count int32) {
|
||||
observer.ObserveInt64(connections, int64(count), metric.WithAttributes(attribute.String("state", state)))
|
||||
}
|
||||
observe("used", stat.AcquiredConns())
|
||||
observe("idle", stat.IdleConns())
|
||||
observe("max", stat.MaxConns())
|
||||
return nil
|
||||
}, connections)
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type pgxTx struct {
|
||||
tx pgx.Tx
|
||||
}
|
||||
|
||||
func (t *pgxTx) Exec(ctx context.Context, sql string, args ...any) error {
|
||||
_, err := t.tx.Exec(ctx, sql, args...)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
// WithTx выполняет fn в одной транзакции:
|
||||
// - commit, если fn вернул nil;
|
||||
// - rollback, если fn вернул ошибку.
|
||||
|
||||
func (s *Storage) WithTx(ctx context.Context, fn func(tx domain.Tx) error) error {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return mapError(err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
if err := fn(&pgxTx{tx: tx}); err != nil {
|
||||
return err
|
||||
}
|
||||
return mapError(tx.Commit(ctx))
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type UserRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserRepo(pool *pgxpool.Pool) *UserRepo {
|
||||
return &UserRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *UserRepo) Create(ctx context.Context, user *domain.User) error {
|
||||
const q = `
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ($1::uuid, $2, $3, $4, $5)
|
||||
`
|
||||
_, err := r.pool.Exec(ctx, q,
|
||||
user.ID, user.Username, user.DisplayName, user.PasswordHash, user.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
if uniqueViolation(err) {
|
||||
return &domain.AlreadyExistsError{
|
||||
Entity: "user",
|
||||
Field: "username",
|
||||
Value: user.Username,
|
||||
}
|
||||
}
|
||||
if checkViolation(err) {
|
||||
return &domain.ValidationError{
|
||||
Field: "user",
|
||||
Message: "нарушено ограничение целостности",
|
||||
}
|
||||
}
|
||||
return mapError(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) {
|
||||
const q = `
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
u := &domain.User{}
|
||||
err := r.pool.QueryRow(ctx, q, id).
|
||||
Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err, "user", id)
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) {
|
||||
const q = `
|
||||
SELECT id, username, display_name, password_hash, created_at
|
||||
FROM users
|
||||
WHERE username = $1
|
||||
`
|
||||
u := &domain.User{}
|
||||
err := r.pool.QueryRow(ctx, q, username).
|
||||
Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, notFound(err, "user", username)
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
var _ domain.UserRepository = (*UserRepo)(nil)
|
||||
@@ -0,0 +1,36 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
func NewClient(url string) (*redis.Client, error) {
|
||||
opt, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("разбор URL: %w", err)
|
||||
}
|
||||
|
||||
opt.DialTimeout = 700 * time.Millisecond
|
||||
opt.ReadTimeout = 700 * time.Millisecond
|
||||
opt.WriteTimeout = 700 * time.Millisecond
|
||||
opt.MaxRetries = 1
|
||||
opt.PoolSize = 10
|
||||
opt.ConnMaxIdleTime = time.Minute
|
||||
|
||||
client := redis.NewClient(opt)
|
||||
client.AddHook(tracingHook{tracer: telemetry.Tracer("storage.redis")})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
_ = client.Close()
|
||||
return nil, fmt.Errorf("ping: %w", err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type coded interface {
|
||||
Code() domain.ErrorCode
|
||||
}
|
||||
|
||||
// mapError переводит ошибки драйвера в доменные.
|
||||
// Любая недоступность redis (dial/read/write/context) → UnavailableError (503).
|
||||
func mapError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var c coded
|
||||
if errors.As(err, &c) {
|
||||
return err
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return &domain.UnavailableError{Reason: "превышено время ожидания redis"}
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return &domain.UnavailableError{Reason: "запрос отменён"}
|
||||
}
|
||||
return &domain.UnavailableError{Reason: err.Error()}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
type tracingHook struct{ tracer trace.Tracer }
|
||||
|
||||
func (h tracingHook) DialHook(next redis.DialHook) redis.DialHook { return next }
|
||||
|
||||
func (h tracingHook) ProcessHook(next redis.ProcessHook) redis.ProcessHook {
|
||||
return func(ctx context.Context, cmd redis.Cmder) error {
|
||||
return h.run(ctx, cmd.Name(), func(ctx context.Context) error { return next(ctx, cmd) })
|
||||
}
|
||||
}
|
||||
|
||||
func (h tracingHook) ProcessPipelineHook(next redis.ProcessPipelineHook) redis.ProcessPipelineHook {
|
||||
return func(ctx context.Context, cmds []redis.Cmder) error {
|
||||
return h.run(ctx, "pipeline", func(ctx context.Context) error { return next(ctx, cmds) })
|
||||
}
|
||||
}
|
||||
|
||||
func (h tracingHook) run(ctx context.Context, op string, call func(context.Context) error) error {
|
||||
ctx, span := h.tracer.Start(ctx, "redis."+op, trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("db.system.name", "redis"), attribute.String("db.operation.name", op)))
|
||||
defer span.End()
|
||||
err := call(ctx)
|
||||
if err != nil && !errors.Is(err, redis.Nil) {
|
||||
span.SetStatus(codes.Error, "операция Redis не выполнена")
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
const sessionKeyPrefix = "session:"
|
||||
|
||||
type SessionRepo struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
func NewSessionRepo(client *redis.Client) *SessionRepo {
|
||||
return &SessionRepo{client: client}
|
||||
}
|
||||
|
||||
type sessionValue struct {
|
||||
UserID string `json:"user_id"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (r *SessionRepo) key(tokenHash []byte) string {
|
||||
return sessionKeyPrefix + hex.EncodeToString(tokenHash)
|
||||
}
|
||||
|
||||
func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error {
|
||||
ttl := time.Until(s.ExpiresAt)
|
||||
if ttl <= 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := json.Marshal(sessionValue{
|
||||
UserID: s.UserID,
|
||||
ExpiresAt: s.ExpiresAt.UTC(),
|
||||
CreatedAt: s.CreatedAt.UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("сериализация сессии: %w", err)
|
||||
}
|
||||
|
||||
return mapError(r.client.Set(ctx, r.key(s.TokenHash), data, ttl).Err())
|
||||
}
|
||||
|
||||
func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) {
|
||||
data, err := r.client.Get(ctx, r.key(tokenHash)).Bytes()
|
||||
if err != nil {
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return nil, &domain.NotFoundError{Entity: "session"}
|
||||
}
|
||||
return nil, mapError(err)
|
||||
}
|
||||
|
||||
var sv sessionValue
|
||||
if err := json.Unmarshal(data, &sv); err != nil {
|
||||
return nil, fmt.Errorf("разбор сессии: %w", err)
|
||||
}
|
||||
|
||||
return &domain.Session{
|
||||
TokenHash: tokenHash,
|
||||
UserID: sv.UserID,
|
||||
ExpiresAt: sv.ExpiresAt,
|
||||
CreatedAt: sv.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error {
|
||||
return mapError(r.client.Del(ctx, r.key(tokenHash)).Err())
|
||||
}
|
||||
|
||||
var _ domain.SessionRepository = (*SessionRepo)(nil)
|
||||
@@ -0,0 +1,115 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Endpoint string
|
||||
Bucket string
|
||||
AccessKey string
|
||||
SecretKey string
|
||||
UseSSL bool
|
||||
Region string
|
||||
}
|
||||
|
||||
type MinioStorage struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg Config) (*MinioStorage, error) {
|
||||
base, err := minio.DefaultTransport(cfg.UseSSL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Оборачиваем базовый транспорт в otelhttp.
|
||||
// Опция WithSpanNameFormatter задаёт красивое имя спана (s3.PUT, s3.GET),
|
||||
// чтобы в Grafana не смотреть на дефолтное "HTTP PUT".
|
||||
tracedTransport := otelhttp.NewTransport(base,
|
||||
otelhttp.WithSpanNameFormatter(func(operation string, r *http.Request) string {
|
||||
return "s3." + r.Method
|
||||
}),
|
||||
)
|
||||
|
||||
client, err := minio.New(cfg.Endpoint, &minio.Options{
|
||||
Transport: tracedTransport,
|
||||
Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""),
|
||||
Secure: cfg.UseSSL,
|
||||
Region: cfg.Region,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
exists, err := client.BucketExists(ctx, cfg.Bucket)
|
||||
if err != nil {
|
||||
return nil, mapError(err)
|
||||
}
|
||||
if !exists {
|
||||
return nil, &domain.UnavailableError{Reason: "s3 bucket отсутствует"}
|
||||
}
|
||||
|
||||
return &MinioStorage{client: client, bucket: cfg.Bucket}, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Ping(ctx context.Context) error {
|
||||
_, err := s.client.BucketExists(ctx, s.bucket)
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error) {
|
||||
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, 0, "", mapError(err)
|
||||
}
|
||||
|
||||
stat, err := obj.Stat()
|
||||
if err != nil {
|
||||
_ = obj.Close()
|
||||
return nil, 0, "", mapError(err)
|
||||
}
|
||||
|
||||
return obj, stat.Size, stat.ContentType, nil
|
||||
}
|
||||
|
||||
func (s *MinioStorage) Delete(ctx context.Context, key string) error {
|
||||
err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||
return mapError(err)
|
||||
}
|
||||
|
||||
func (s *MinioStorage) List(ctx context.Context, prefix string) ([]domain.ObjectInfo, error) {
|
||||
out := make([]domain.ObjectInfo, 0)
|
||||
for obj := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{
|
||||
Prefix: prefix,
|
||||
Recursive: true,
|
||||
}) {
|
||||
if obj.Err != nil {
|
||||
return nil, mapError(obj.Err)
|
||||
}
|
||||
out = append(out, domain.ObjectInfo{
|
||||
Key: obj.Key,
|
||||
LastModified: obj.LastModified,
|
||||
Size: obj.Size,
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
var _ domain.ObjectStorage = (*MinioStorage)(nil)
|
||||
@@ -0,0 +1,42 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
)
|
||||
|
||||
type coded interface {
|
||||
Code() domain.ErrorCode
|
||||
}
|
||||
|
||||
func mapError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var c coded
|
||||
if errors.As(err, &c) {
|
||||
return err
|
||||
}
|
||||
var resp minio.ErrorResponse
|
||||
if errors.As(err, &resp) {
|
||||
switch resp.Code {
|
||||
case "NoSuchKey", "NotFound":
|
||||
return &domain.NotFoundError{Entity: "object"}
|
||||
case "NoSuchBucket":
|
||||
return &domain.UnavailableError{Reason: "s3 bucket отсутствует"}
|
||||
case "AccessDenied", "InvalidAccessKeyId", "SignatureDoesNotMatch":
|
||||
return &domain.UnavailableError{Reason: "s3 доступ запрещён"}
|
||||
}
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return &domain.UnavailableError{Reason: "превышено время ожидания s3"}
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return &domain.UnavailableError{Reason: "запрос отменён"}
|
||||
}
|
||||
return &domain.UnavailableError{Reason: err.Error()}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Один span на HTTP-попытку MinIO до получения заголовков ответа.
|
||||
type tracingTransport struct {
|
||||
next http.RoundTripper
|
||||
tracer trace.Tracer
|
||||
}
|
||||
|
||||
func (t tracingTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
ctx, span := t.tracer.Start(req.Context(), "s3."+req.Method, trace.WithSpanKind(trace.SpanKindClient),
|
||||
trace.WithAttributes(attribute.String("http.request.method", req.Method)))
|
||||
defer span.End()
|
||||
|
||||
resp, err := t.next.RoundTrip(req.WithContext(ctx))
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "ошибка соединения с S3")
|
||||
} else {
|
||||
span.SetAttributes(attribute.Int("http.response.status_code", resp.StatusCode))
|
||||
if resp.StatusCode >= 400 {
|
||||
span.SetStatus(codes.Error, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// multiHandler — slog.Handler, который дублирует записи во все вложенные handler'ы.
|
||||
//
|
||||
// Используется, чтобы одна и та же лог-запись попала:
|
||||
// - в stdout как JSON (для `docker logs` и отладки);
|
||||
// - в OTLP как структурированный лог с trace_id/span_id (для Collector → OpenSearch).
|
||||
//
|
||||
// Уровень (Enabled) считается включённым, если его принимает хотя бы один
|
||||
// вложенный handler. Это позволяет держать в stdout более подробный уровень,
|
||||
// чем в OTLP, если понадобится.
|
||||
type multiHandler struct {
|
||||
handlers []slog.Handler
|
||||
}
|
||||
|
||||
func newMultiHandler(h ...slog.Handler) slog.Handler {
|
||||
return &multiHandler{handlers: h}
|
||||
}
|
||||
|
||||
func (m *multiHandler) Enabled(ctx context.Context, level slog.Level) bool {
|
||||
for _, h := range m.handlers {
|
||||
if h.Enabled(ctx, level) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (m *multiHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
sc := trace.SpanContextFromContext(ctx)
|
||||
var traceID, spanID string
|
||||
if sc.IsValid() {
|
||||
traceID, spanID = sc.TraceID().String(), sc.SpanID().String()
|
||||
}
|
||||
r.AddAttrs(slog.String("trace_id", traceID), slog.String("span_id", spanID))
|
||||
for _, h := range m.handlers {
|
||||
if !h.Enabled(ctx, r.Level) {
|
||||
continue
|
||||
}
|
||||
// Clone нужен, потому что некоторые handler'ы могут менять Record,
|
||||
// а мы передаём его в несколько мест последовательно.
|
||||
if err := h.Handle(ctx, r.Clone()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *multiHandler) WithAttrs(attrs []slog.Attr) slog.Handler {
|
||||
nh := make([]slog.Handler, len(m.handlers))
|
||||
for i, h := range m.handlers {
|
||||
nh[i] = h.WithAttrs(attrs)
|
||||
}
|
||||
return &multiHandler{handlers: nh}
|
||||
}
|
||||
|
||||
func (m *multiHandler) WithGroup(name string) slog.Handler {
|
||||
nh := make([]slog.Handler, len(m.handlers))
|
||||
for i, h := range m.handlers {
|
||||
nh[i] = h.WithGroup(name)
|
||||
}
|
||||
return &multiHandler{handlers: nh}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
// Tracer возвращает tracer из глобального провайдера.
|
||||
func Tracer(name string) trace.Tracer {
|
||||
return otel.Tracer(name)
|
||||
}
|
||||
|
||||
// Meter возвращает meter из глобального провайдера.
|
||||
func Meter(name string) metric.Meter {
|
||||
return otel.Meter(name)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
)
|
||||
|
||||
// TraceHeaders переносит только W3C Trace Context через границу задания.
|
||||
func TraceHeaders(ctx context.Context) map[string]string {
|
||||
headers := propagation.MapCarrier{}
|
||||
propagation.TraceContext{}.Inject(ctx, headers)
|
||||
return headers
|
||||
}
|
||||
|
||||
func ExtractTrace(ctx context.Context, headers map[string]string) context.Context {
|
||||
return propagation.TraceContext{}.Extract(ctx, propagation.MapCarrier(headers))
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
)
|
||||
|
||||
func TestOutboxTraceHeaders(t *testing.T) {
|
||||
input := map[string]string{
|
||||
"traceparent": "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-01",
|
||||
"tracestate": "lab=node1",
|
||||
"baggage": "private=example",
|
||||
}
|
||||
ctx := ExtractTrace(context.Background(), input)
|
||||
got := TraceHeaders(ctx)
|
||||
if len(got) != 2 || got["traceparent"] != input["traceparent"] || got["tracestate"] != input["tracestate"] {
|
||||
t.Fatalf("неожиданные заголовки outbox: %v", got)
|
||||
}
|
||||
for _, headers := range []map[string]string{nil, {"traceparent": "broken"}} {
|
||||
if trace.SpanContextFromContext(ExtractTrace(context.Background(), headers)).IsValid() {
|
||||
t.Fatal("отсутствующий или повреждённый контекст дал валидного родителя")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/contrib/bridges/otelslog"
|
||||
"go.opentelemetry.io/otel"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp"
|
||||
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp"
|
||||
"go.opentelemetry.io/otel/propagation"
|
||||
otellog "go.opentelemetry.io/otel/sdk/log"
|
||||
sdkmetric "go.opentelemetry.io/otel/sdk/metric"
|
||||
"go.opentelemetry.io/otel/sdk/resource"
|
||||
sdktrace "go.opentelemetry.io/otel/sdk/trace"
|
||||
)
|
||||
|
||||
// SetupConfig - параметры инициализации SDK.
|
||||
type SetupConfig struct {
|
||||
ServiceName string
|
||||
ServiceVersion string
|
||||
InstanceID string
|
||||
Environment string
|
||||
}
|
||||
|
||||
// Providers - набор провайдеров OTel, которые нужно завершать при остановке.
|
||||
type Providers struct {
|
||||
Tracer *sdktrace.TracerProvider
|
||||
Meter *sdkmetric.MeterProvider
|
||||
Logs *otellog.LoggerProvider
|
||||
Logger *slog.Logger
|
||||
}
|
||||
|
||||
// Setup инициализирует OTel SDK: resource, OTLP/HTTP экспортёры и провайдеры.
|
||||
//
|
||||
// При OTEL_SDK_DISABLED=true, будут пустые Providers.
|
||||
func Setup(ctx context.Context, cfg SetupConfig) (*Providers, error) {
|
||||
if disabled() {
|
||||
logger := slog.New(newMultiHandler(slog.NewJSONHandler(os.Stdout, nil))).With("service.name", cfg.ServiceName)
|
||||
return &Providers{Logger: logger}, nil
|
||||
}
|
||||
|
||||
res, err := resource.New(ctx,
|
||||
resource.WithTelemetrySDK(),
|
||||
resource.WithHost(),
|
||||
resource.WithFromEnv(),
|
||||
resource.WithAttributes(
|
||||
attribute.String("service.name", cfg.ServiceName),
|
||||
attribute.String("service.version", cfg.ServiceVersion),
|
||||
attribute.String("service.instance.id", cfg.InstanceID),
|
||||
attribute.String("deployment.environment.name", cfg.Environment),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ресурс телеметрии: %w", err)
|
||||
}
|
||||
|
||||
endpoint := baseEndpoint()
|
||||
|
||||
// Трассы
|
||||
traceExp, err := otlptracehttp.New(ctx,
|
||||
otlptracehttp.WithEndpointURL(endpoint+"/v1/traces"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("экспортёр трассировок: %w", err)
|
||||
}
|
||||
tp := sdktrace.NewTracerProvider(
|
||||
sdktrace.WithBatcher(traceExp),
|
||||
sdktrace.WithResource(res),
|
||||
)
|
||||
|
||||
// Метрики
|
||||
metricExp, err := otlpmetrichttp.New(ctx,
|
||||
otlpmetrichttp.WithEndpointURL(endpoint+"/v1/metrics"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("экспортёр метрик: %w", err)
|
||||
}
|
||||
mp := sdkmetric.NewMeterProvider(
|
||||
sdkmetric.WithReader(sdkmetric.NewPeriodicReader(
|
||||
metricExp,
|
||||
sdkmetric.WithInterval(metricInterval()),
|
||||
)),
|
||||
sdkmetric.WithResource(res),
|
||||
)
|
||||
|
||||
// Логи
|
||||
logExp, err := otlploghttp.New(ctx,
|
||||
otlploghttp.WithEndpointURL(endpoint+"/v1/logs"),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("экспортёр логов: %w", err)
|
||||
}
|
||||
lp := otellog.NewLoggerProvider(
|
||||
otellog.WithProcessor(otellog.NewBatchProcessor(logExp)),
|
||||
otellog.WithResource(res),
|
||||
)
|
||||
|
||||
otel.SetTracerProvider(tp)
|
||||
otel.SetMeterProvider(mp)
|
||||
|
||||
// Пропагация контекста через W3C Trace Context и Baggage
|
||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||
propagation.TraceContext{}, propagation.Baggage{},
|
||||
))
|
||||
|
||||
logger := slog.New(newMultiHandler(
|
||||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo}),
|
||||
otelslog.NewHandler(cfg.ServiceName, otelslog.WithLoggerProvider(lp)),
|
||||
)).With("service.name", cfg.ServiceName)
|
||||
|
||||
return &Providers{Tracer: tp, Meter: mp, Logs: lp, Logger: logger}, nil
|
||||
}
|
||||
|
||||
// Shutdown корректно завершает провайдеры, отправляя сохран. данные.
|
||||
func (p *Providers) Shutdown(ctx context.Context) error {
|
||||
if p == nil {
|
||||
return nil
|
||||
}
|
||||
var errs []error
|
||||
if p.Logs != nil {
|
||||
errs = append(errs, p.Logs.Shutdown(ctx))
|
||||
}
|
||||
if p.Meter != nil {
|
||||
errs = append(errs, p.Meter.Shutdown(ctx))
|
||||
}
|
||||
if p.Tracer != nil {
|
||||
errs = append(errs, p.Tracer.Shutdown(ctx))
|
||||
}
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func disabled() bool {
|
||||
return strings.EqualFold(os.Getenv("OTEL_SDK_DISABLED"), "true")
|
||||
}
|
||||
|
||||
func baseEndpoint() string {
|
||||
v := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT")
|
||||
if v == "" {
|
||||
v = "http://otel-collector:4318"
|
||||
}
|
||||
return strings.TrimRight(v, "/")
|
||||
}
|
||||
|
||||
func metricInterval() time.Duration {
|
||||
v := os.Getenv("OTEL_METRIC_EXPORT_INTERVAL")
|
||||
if v == "" {
|
||||
return 15 * time.Second
|
||||
}
|
||||
ms, err := strconv.Atoi(v)
|
||||
if err != nil || ms <= 0 {
|
||||
return 15 * time.Second
|
||||
}
|
||||
return time.Duration(ms) * time.Millisecond
|
||||
}
|
||||
|
||||
func samplerFromEnv() sdktrace.Sampler {
|
||||
name := strings.ToLower(strings.TrimSpace(os.Getenv("OTEL_TRACES_SAMPLER")))
|
||||
switch name {
|
||||
case "always_off":
|
||||
return sdktrace.NeverSample()
|
||||
case "always_on":
|
||||
return sdktrace.AlwaysSample()
|
||||
case "traceidratio":
|
||||
return sdktrace.TraceIDRatioBased(ratioArg(0.1))
|
||||
case "parentbased_traceidratio":
|
||||
return sdktrace.ParentBased(sdktrace.TraceIDRatioBased(ratioArg(0.1)))
|
||||
default:
|
||||
return sdktrace.ParentBased(sdktrace.AlwaysSample())
|
||||
}
|
||||
}
|
||||
|
||||
func ratioArg(def float64) float64 {
|
||||
v := os.Getenv("OTEL_TRACES_SAMPLER_ARG")
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
f, err := strconv.ParseFloat(v, 64)
|
||||
if err != nil || f < 0 || f > 1 {
|
||||
return def
|
||||
}
|
||||
return f
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/metric"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type jobMetrics struct {
|
||||
processed metric.Int64Counter
|
||||
duration metric.Float64Histogram
|
||||
ready metric.Float64Histogram
|
||||
}
|
||||
|
||||
func newJobMetrics() jobMetrics {
|
||||
meter := telemetry.Meter("worker.pipeline")
|
||||
processed, err := meter.Int64Counter("jobs_processed_total")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
unit := metric.WithUnit("s")
|
||||
duration, err := meter.Float64Histogram("job_processing_duration_seconds", unit,
|
||||
metric.WithExplicitBucketBoundaries(.01, .05, .1, .25, .5, 1, 2, 5, 10, 30, 60))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ready, err := meter.Float64Histogram("end_to_end_ready_seconds", unit,
|
||||
metric.WithExplicitBucketBoundaries(.1, .5, 1, 2, 5, 10, 30, 60, 120, 300, 900, 3600))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return jobMetrics{processed: processed, duration: duration, ready: ready}
|
||||
}
|
||||
|
||||
func (m jobMetrics) recordAttempt(ctx context.Context, start time.Time, outcome string) {
|
||||
attrs := metric.WithAttributes(attribute.String("job.type", domain.OutboxEventProcess), attribute.String("outcome", outcome))
|
||||
m.duration.Record(ctx, time.Since(start).Seconds(), attrs)
|
||||
m.processed.Add(ctx, 1, attrs)
|
||||
|
||||
span := trace.SpanFromContext(ctx)
|
||||
span.SetAttributes(attribute.String("job.outcome", outcome))
|
||||
level := slog.LevelInfo
|
||||
switch outcome {
|
||||
case "failed", "error", "retry":
|
||||
level = slog.LevelWarn
|
||||
span.SetStatus(codes.Error, outcome)
|
||||
}
|
||||
slog.Log(ctx, level, "Попытка обработки завершена", "event", "job.processed", "outcome", outcome)
|
||||
}
|
||||
|
||||
func (m jobMetrics) recordReady(ctx context.Context, createdAt time.Time) {
|
||||
if createdAt.IsZero() {
|
||||
return
|
||||
}
|
||||
m.ready.Record(ctx, max(0, time.Since(createdAt).Seconds()),
|
||||
metric.WithAttributes(attribute.String("job.type", domain.OutboxEventProcess)))
|
||||
}
|
||||
@@ -0,0 +1,228 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/imaging"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type Pipeline struct {
|
||||
jobs domain.AttachmentJobRepository
|
||||
store domain.ObjectStorage
|
||||
variants domain.AttachmentVariantRepository
|
||||
failedJobs domain.FailedJobRepository
|
||||
outbox domain.OutboxRepository
|
||||
lease time.Duration
|
||||
maxRetries int
|
||||
metrics jobMetrics
|
||||
}
|
||||
|
||||
func NewPipeline(
|
||||
jobs domain.AttachmentJobRepository,
|
||||
store domain.ObjectStorage,
|
||||
variants domain.AttachmentVariantRepository,
|
||||
failedJobs domain.FailedJobRepository,
|
||||
outbox domain.OutboxRepository,
|
||||
) *Pipeline {
|
||||
return &Pipeline{
|
||||
jobs: jobs,
|
||||
store: store,
|
||||
variants: variants,
|
||||
failedJobs: failedJobs,
|
||||
outbox: outbox,
|
||||
lease: 5 * time.Minute,
|
||||
maxRetries: 5,
|
||||
metrics: newJobMetrics(),
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Pipeline) Process(ctx context.Context, attachmentID string, pipelineVersion int) error {
|
||||
start := time.Now()
|
||||
outcome := "error"
|
||||
defer func() { p.metrics.recordAttempt(ctx, start, outcome) }()
|
||||
|
||||
log.Printf("worker: начало обработки %s (версия пайплайна %d)", attachmentID, pipelineVersion)
|
||||
|
||||
jobCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
tracer := telemetry.Tracer("worker.pipeline")
|
||||
|
||||
// 1. Забираем задачу с проверкой версии
|
||||
claimCtx, claimSpan := tracer.Start(jobCtx, "job.claim")
|
||||
a, err := p.jobs.ClaimForProcessing(claimCtx, attachmentID, pipelineVersion, p.lease)
|
||||
endPipelineSpan(claimSpan, err)
|
||||
if err != nil {
|
||||
return fmt.Errorf("захват задачи: %w", err)
|
||||
}
|
||||
if a == nil {
|
||||
outcome = "skipped"
|
||||
log.Printf("worker: задача %s уже взята, выполнена или версия устарела", attachmentID)
|
||||
return nil // no-op
|
||||
}
|
||||
|
||||
fail := func(err error, permanent bool) error {
|
||||
outcome = p.handleFailure(a, err, permanent)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 2. Скачиваем оригинал из staging
|
||||
getCtx, getSpan := tracer.Start(jobCtx, "job.s3.get")
|
||||
rc, _, _, err := p.store.Get(getCtx, a.StagingKey)
|
||||
if err != nil {
|
||||
endPipelineSpan(getSpan, err)
|
||||
return fail(fmt.Errorf("скачивание staging: %w", err), false)
|
||||
}
|
||||
defer rc.Close()
|
||||
|
||||
buf, err := io.ReadAll(rc)
|
||||
endPipelineSpan(getSpan, err)
|
||||
if err != nil {
|
||||
return fail(fmt.Errorf("чтение staging: %w", err), false)
|
||||
}
|
||||
|
||||
// 3. Генерируем все варианты (Thumbnail, Crop, Optimized)
|
||||
log.Printf("worker: генерация вариантов для %s", a.ID)
|
||||
_, imageSpan := tracer.Start(jobCtx, "job.image.process")
|
||||
thumbMeta, thumbData, err := imaging.Thumbnail(a.ID, buf)
|
||||
if err != nil {
|
||||
endPipelineSpan(imageSpan, err)
|
||||
return fail(fmt.Errorf("генерация thumbnail: %w", err), true)
|
||||
}
|
||||
|
||||
cropMeta, cropData, err := imaging.Crop(a.ID, buf)
|
||||
if err != nil {
|
||||
endPipelineSpan(imageSpan, err)
|
||||
return fail(fmt.Errorf("генерация crop: %w", err), true)
|
||||
}
|
||||
|
||||
webpMeta, webpData, err := imaging.Optimized(a.ID, buf)
|
||||
endPipelineSpan(imageSpan, err)
|
||||
if err != nil {
|
||||
return fail(fmt.Errorf("генерация webp: %w", err), true)
|
||||
}
|
||||
|
||||
// 4. Сохраняем оригинал в финальный prefix (chats/...)
|
||||
putCtx, putSpan := tracer.Start(jobCtx, "job.s3.put")
|
||||
if err := p.store.Put(putCtx, a.ObjectKey, bytes.NewReader(buf), int64(len(buf)), a.ContentType); err != nil {
|
||||
endPipelineSpan(putSpan, err)
|
||||
return fail(fmt.Errorf("загрузка оригинала: %w", err), false)
|
||||
}
|
||||
|
||||
// 5. Загружаем все сгенерированные варианты в S3
|
||||
variants := []domain.AttachmentVariant{*thumbMeta, *cropMeta, *webpMeta}
|
||||
|
||||
for i := range variants {
|
||||
v := &variants[i]
|
||||
key := fmt.Sprintf("%s/%s", a.ObjectKey, v.Name)
|
||||
|
||||
var data []byte
|
||||
switch v.Name {
|
||||
case domain.VariantThumbnail:
|
||||
data = thumbData
|
||||
case domain.VariantCrop:
|
||||
data = cropData
|
||||
case domain.VariantOptimized:
|
||||
data = webpData
|
||||
}
|
||||
|
||||
if err := p.store.Put(putCtx, key, bytes.NewReader(data), v.SizeBytes, v.ContentType); err != nil {
|
||||
endPipelineSpan(putSpan, err)
|
||||
return fail(fmt.Errorf("загрузка варианта %s: %w", v.Name, err), false)
|
||||
}
|
||||
v.ObjectKey = key
|
||||
}
|
||||
|
||||
putSpan.End()
|
||||
|
||||
// 6. Фиксируем результат в БД (переводим в ready)
|
||||
completeCtx, completeSpan := tracer.Start(jobCtx, "job.db.complete")
|
||||
ok, err := p.jobs.CompleteJob(completeCtx, a.ID, a.PipelineVersion, variants)
|
||||
endPipelineSpan(completeSpan, err)
|
||||
if err != nil {
|
||||
return fail(fmt.Errorf("фиксация в БД: %w", err), false)
|
||||
}
|
||||
if !ok {
|
||||
outcome = "skipped"
|
||||
log.Printf("worker: версия устарела или статус изменился, пропускаем %s", a.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
outcome = "ready"
|
||||
p.metrics.recordReady(ctx, a.CreatedAt)
|
||||
log.Printf("worker: успешно завершено %s", a.ID)
|
||||
return nil
|
||||
}
|
||||
|
||||
func endPipelineSpan(span trace.Span, err error) {
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, "операция завершилась ошибкой")
|
||||
}
|
||||
span.End()
|
||||
}
|
||||
|
||||
func (p *Pipeline) handleFailure(a *domain.Attachment, err error, isPermanent bool) string {
|
||||
// Если исчерпаны попытки или ошибка постоянная (например, битый файл)
|
||||
if isPermanent || a.Attempts >= p.maxRetries {
|
||||
reason := domain.JobErrorInternal
|
||||
if a.Attempts >= p.maxRetries {
|
||||
reason = domain.JobErrorRetryExhausted
|
||||
} else if isPermanent {
|
||||
reason = domain.JobErrorDecodeFailed
|
||||
}
|
||||
|
||||
log.Printf("worker: финальная ошибка для %s (попытка %d): %v", a.ID, a.Attempts, err)
|
||||
|
||||
// Используем context.Background(), чтобы запись в БД прошла даже при истекшем jobCtx
|
||||
updateErr := p.jobs.FailJob(context.Background(), a.ID, reason)
|
||||
recordErr := p.failedJobs.Record(context.Background(), &domain.FailedJob{
|
||||
AttachmentID: a.ID,
|
||||
ReasonCode: reason,
|
||||
Attempts: a.Attempts,
|
||||
})
|
||||
if updateErr != nil || recordErr != nil {
|
||||
return "error"
|
||||
}
|
||||
return "failed"
|
||||
}
|
||||
|
||||
// Временная ошибка - делаем Retry с backoff
|
||||
log.Printf("worker: временная ошибка для %s, попытка %d: %v", a.ID, a.Attempts, err)
|
||||
|
||||
// Экспоненциальный backoff: 2^attempts секунд + случайный jitter (0-1000 мс)
|
||||
backoff := time.Duration(1<<uint(a.Attempts)) * time.Second
|
||||
jitter := time.Duration(rand.Intn(1000)) * time.Millisecond
|
||||
nextAt := time.Now().UTC().Add(backoff + jitter)
|
||||
|
||||
scheduleErr := p.jobs.ScheduleRetry(context.Background(), a.ID, nextAt, "")
|
||||
|
||||
// Кладем в Outbox отложенное сообщение
|
||||
payload, _ := json.Marshal(domain.JobMessage{
|
||||
AttachmentID: a.ID,
|
||||
PipelineVersion: a.PipelineVersion,
|
||||
Attempt: a.Attempts + 1,
|
||||
})
|
||||
enqueueErr := p.outbox.EnqueueDirect(context.Background(), &domain.OutboxEntry{
|
||||
AggregateType: domain.OutboxAggregateAttachment,
|
||||
AggregateID: a.ID,
|
||||
EventType: domain.OutboxEventProcess,
|
||||
Payload: payload,
|
||||
CreatedAt: nextAt,
|
||||
})
|
||||
|
||||
if scheduleErr != nil || enqueueErr != nil {
|
||||
return "error"
|
||||
}
|
||||
return "retry"
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT users_username_format
|
||||
CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_not_blank
|
||||
CHECK (display_name !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT users_display_name_len
|
||||
CHECK (char_length(display_name) <= 100),
|
||||
CONSTRAINT users_password_hash_not_blank
|
||||
CHECK (length(password_hash) > 0)
|
||||
);
|
||||
CREATE UNIQUE INDEX users_username_uniq ON users (username);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT chats_title_not_blank CHECK (title !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT chats_title_len CHECK (char_length(title) <= 100)
|
||||
);
|
||||
CREATE INDEX chats_created_id_idx ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
joined_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'),
|
||||
CONSTRAINT messages_text_len CHECK (char_length(text) <= 4000)
|
||||
);
|
||||
CREATE INDEX messages_chat_created_id_idx
|
||||
ON messages (chat_id, created_at, id);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash BYTEA PRIMARY KEY,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32)
|
||||
);
|
||||
CREATE INDEX sessions_user_idx ON sessions (user_id);
|
||||
CREATE INDEX sessions_expires_idx ON sessions (expires_at);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,35 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method TEXT NOT NULL,
|
||||
path TEXT NOT NULL,
|
||||
key TEXT NOT NULL,
|
||||
request_hash BYTEA NOT NULL,
|
||||
response_status INT NOT NULL,
|
||||
response_body BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
expires_at TIMESTAMPTZ NOT NULL,
|
||||
|
||||
CONSTRAINT idempotency_keys_pkey
|
||||
PRIMARY KEY (user_id, method, path, key),
|
||||
CONSTRAINT idempotency_keys_key_len
|
||||
CHECK (char_length(key) BETWEEN 1 AND 128),
|
||||
CONSTRAINT idempotency_keys_method_len
|
||||
CHECK (char_length(method) BETWEEN 1 AND 16),
|
||||
CONSTRAINT idempotency_keys_path_len
|
||||
CHECK (char_length(path) BETWEEN 1 AND 2048),
|
||||
CONSTRAINT idempotency_keys_hash_len
|
||||
CHECK (octet_length(request_hash) = 32),
|
||||
CONSTRAINT idempotency_keys_status_range
|
||||
CHECK (response_status BETWEEN 100 AND 599)
|
||||
);
|
||||
|
||||
CREATE INDEX idempotency_keys_expires_idx
|
||||
ON idempotency_keys (expires_at);
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS idempotency_keys;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,73 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE attachments (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT,
|
||||
filename TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
sha256 BYTEA NOT NULL,
|
||||
object_key TEXT NOT NULL,
|
||||
status TEXT NOT NULL,
|
||||
error_code TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachments_filename_len
|
||||
CHECK (char_length(filename) BETWEEN 1 AND 255),
|
||||
CONSTRAINT attachments_filename_no_slash
|
||||
CHECK (filename !~ '[/\\]'),
|
||||
CONSTRAINT attachments_filename_no_ctrl
|
||||
CHECK (filename !~ '[[:cntrl:]]'),
|
||||
CONSTRAINT attachments_content_type_chk
|
||||
CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
CONSTRAINT attachments_size_chk
|
||||
CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
CONSTRAINT attachments_sha256_len
|
||||
CHECK (octet_length(sha256) = 32),
|
||||
CONSTRAINT attachments_object_key_len
|
||||
CHECK (char_length(object_key) BETWEEN 1 AND 1024),
|
||||
CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('ready', 'pending', 'failed')),
|
||||
CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('ready', 'pending') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX attachments_object_key_uniq
|
||||
ON attachments (object_key);
|
||||
|
||||
CREATE INDEX attachments_chat_idx
|
||||
ON attachments (chat_id, created_at, id);
|
||||
|
||||
CREATE INDEX attachments_pending_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT,
|
||||
position SMALLINT NOT NULL,
|
||||
|
||||
CONSTRAINT message_attachments_pkey
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
CONSTRAINT message_attachments_position_range
|
||||
CHECK (position >= 0 AND position < 10),
|
||||
CONSTRAINT message_attachments_position_uniq
|
||||
UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX message_attachments_attachment_idx
|
||||
ON message_attachments (attachment_id);
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS message_attachments;
|
||||
DROP TABLE IF EXISTS attachments;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,13 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank
|
||||
CHECK (text !~ '^[[:space:]]+$');
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank
|
||||
CHECK (text !~ '^[[:space:]]*$');
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,77 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('pending', 'queued', 'processing', 'ready', 'failed'));
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('pending', 'queued', 'processing', 'ready') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
);
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN attempts INT NOT NULL DEFAULT 0;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_attempts_nonneg
|
||||
CHECK (attempts >= 0);
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN next_attempt_at TIMESTAMPTZ;
|
||||
ALTER TABLE attachments ADD COLUMN locked_until TIMESTAMPTZ;
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN pipeline_version INT NOT NULL DEFAULT 1;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_pipeline_version_pos
|
||||
CHECK (pipeline_version >= 1);
|
||||
|
||||
ALTER TABLE attachments ADD COLUMN staging_key TEXT;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_staging_key_len
|
||||
CHECK (staging_key IS NULL OR char_length(staging_key) BETWEEN 1 AND 1024);
|
||||
|
||||
|
||||
DROP INDEX IF EXISTS attachments_pending_idx;
|
||||
|
||||
CREATE INDEX attachments_active_idx
|
||||
ON attachments (status, next_attempt_at)
|
||||
WHERE status IN ('queued', 'processing');
|
||||
|
||||
CREATE INDEX attachments_locked_idx
|
||||
ON attachments (locked_until)
|
||||
WHERE status = 'processing';
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
|
||||
DROP INDEX IF EXISTS attachments_locked_idx;
|
||||
DROP INDEX IF EXISTS attachments_active_idx;
|
||||
|
||||
CREATE INDEX attachments_pending_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_staging_key_len;
|
||||
ALTER TABLE attachments DROP COLUMN IF EXISTS staging_key;
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_pipeline_version_pos;
|
||||
ALTER TABLE attachments DROP COLUMN IF EXISTS pipeline_version;
|
||||
|
||||
ALTER TABLE attachments DROP COLUMN IF EXISTS locked_until;
|
||||
ALTER TABLE attachments DROP COLUMN IF EXISTS next_attempt_at;
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_attempts_nonneg;
|
||||
ALTER TABLE attachments DROP COLUMN IF EXISTS attempts;
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('ready', 'pending') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
);
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('ready', 'pending', 'failed'));
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,32 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE outbox (
|
||||
id UUID PRIMARY KEY,
|
||||
aggregate_type TEXT NOT NULL,
|
||||
aggregate_id UUID NOT NULL,
|
||||
event_type TEXT NOT NULL,
|
||||
payload JSONB NOT NULL,
|
||||
attempts INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
published_at TIMESTAMPTZ,
|
||||
|
||||
CONSTRAINT outbox_aggregate_type_len
|
||||
CHECK (char_length(aggregate_type) BETWEEN 1 AND 64),
|
||||
CONSTRAINT outbox_event_type_len
|
||||
CHECK (char_length(event_type) BETWEEN 1 AND 64),
|
||||
CONSTRAINT outbox_attempts_nonneg
|
||||
CHECK (attempts >= 0)
|
||||
);
|
||||
|
||||
CREATE INDEX outbox_pending_idx
|
||||
ON outbox (created_at)
|
||||
WHERE published_at IS NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS outbox;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,43 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('queued', 'processing', 'ready', 'failed'));
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('queued', 'processing', 'ready') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
);
|
||||
|
||||
DROP INDEX IF EXISTS attachments_pending_idx;
|
||||
DROP INDEX IF EXISTS attachments_active_idx;
|
||||
CREATE INDEX attachments_active_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status IN ('queued', 'processing');
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
|
||||
DROP INDEX IF EXISTS attachments_active_idx;
|
||||
CREATE INDEX attachments_pending_idx
|
||||
ON attachments (created_at)
|
||||
WHERE status = 'pending';
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_error_code_consistency;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_error_code_consistency
|
||||
CHECK (
|
||||
(status IN ('ready', 'pending') AND error_code IS NULL) OR
|
||||
(status = 'failed' AND error_code IS NOT NULL)
|
||||
);
|
||||
|
||||
ALTER TABLE attachments DROP CONSTRAINT IF EXISTS attachments_status_chk;
|
||||
ALTER TABLE attachments ADD CONSTRAINT attachments_status_chk
|
||||
CHECK (status IN ('ready', 'pending', 'failed'));
|
||||
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,26 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
name TEXT NOT NULL,
|
||||
object_key TEXT NOT NULL,
|
||||
content_type TEXT NOT NULL,
|
||||
size_bytes BIGINT NOT NULL,
|
||||
width INT NOT NULL,
|
||||
height INT NOT NULL,
|
||||
sha256 BYTEA NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT attachment_variants_pkey PRIMARY KEY (attachment_id, name),
|
||||
CONSTRAINT attachment_variants_name_chk CHECK (name IN ('thumbnail', 'crop', 'optimized')),
|
||||
CONSTRAINT attachment_variants_size_chk CHECK (size_bytes > 0),
|
||||
CONSTRAINT attachment_variants_sha256_len CHECK (octet_length(sha256) = 32)
|
||||
);
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS attachment_variants;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,25 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
CREATE TABLE failed_jobs (
|
||||
id UUID PRIMARY KEY,
|
||||
attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
|
||||
reason_code TEXT NOT NULL,
|
||||
attempts INT NOT NULL,
|
||||
last_error_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
resolved_at TIMESTAMPTZ,
|
||||
resolution TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
|
||||
CONSTRAINT failed_jobs_reason_len CHECK (char_length(reason_code) BETWEEN 1 AND 64),
|
||||
CONSTRAINT failed_jobs_attempts_pos CHECK (attempts > 0)
|
||||
);
|
||||
|
||||
CREATE INDEX failed_jobs_unresolved_idx ON failed_jobs (created_at) WHERE resolved_at IS NULL;
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
DROP TABLE IF EXISTS failed_jobs;
|
||||
-- +goose StatementEnd
|
||||
@@ -0,0 +1,5 @@
|
||||
-- +goose Up
|
||||
ALTER TABLE outbox ADD COLUMN headers JSONB NOT NULL DEFAULT '{}';
|
||||
|
||||
-- +goose Down
|
||||
ALTER TABLE outbox DROP COLUMN headers;
|
||||
@@ -0,0 +1,6 @@
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.sql
|
||||
var FS embed.FS
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user