feat: lab4 - S3 attachments, image validation, failover

This commit is contained in:
2026-11-11 21:42:08 +03:00
parent 312131c344
commit 9fc37b0d8a
18 changed files with 1050 additions and 59 deletions
+51
View File
@@ -0,0 +1,51 @@
// Package store is the only layer that talks to PostgreSQL.
package store
import (
"context"
"database/sql"
"time"
_ "github.com/lib/pq"
)
const (
maxOpenConns = 20
maxIdleConns = 10
connMaxLifetime = 5 * time.Minute
pingTimeout = 2 * time.Second
)
// Store wraps the shared PostgreSQL connection pool.
type Store struct {
db *sql.DB
}
// Open creates the pool; it does not require the database to be reachable yet.
func Open(databaseURL string) (*Store, error) {
db, err := sql.Open("postgres", databaseURL)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(maxOpenConns)
db.SetMaxIdleConns(maxIdleConns)
db.SetConnMaxLifetime(connMaxLifetime)
return &Store{db: db}, nil
}
// Close releases the pool.
func (s *Store) Close() error {
return s.db.Close()
}
// Ping reports whether the database answers within a short budget; readiness uses it.
func (s *Store) Ping(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, pingTimeout)
defer cancel()
return s.db.PingContext(ctx)
}
// scanTime normalises timestamps returned by the driver.
func utc(t time.Time) time.Time {
return t.UTC()
}