feat: attachments over S3 (grade 3)

This commit is contained in:
2026-11-20 03:55:39 +03:00
parent 083d0d80cc
commit 89e9e7b4d4
44 changed files with 1468 additions and 67 deletions
+61 -12
View File
@@ -10,12 +10,19 @@ import (
)
type Config struct {
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
}
func Load() (Config, error) {
@@ -23,6 +30,14 @@ func Load() (Config, error) {
if err != nil || ttl <= 0 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
}
maxUploadBytes, err := positiveInt("MAX_UPLOAD_BYTES", "10485760")
if err != nil {
return Config{}, err
}
maxImagePixels, err := positiveInt("MAX_IMAGE_PIXELS", "40000000")
if err != nil {
return Config{}, err
}
databaseURL, err := readSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return Config{}, err
@@ -31,18 +46,52 @@ func Load() (Config, error) {
if err != nil {
return Config{}, err
}
accessKeyID, err := readSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return Config{}, err
}
secretAccessKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return Config{}, err
}
if accessKeyID == "" || secretAccessKey == "" {
return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID_FILE and S3_SECRET_ACCESS_KEY_FILE must not be empty")
}
bucket := getenv("S3_BUCKET", "messenger-attachments")
if strings.TrimSpace(bucket) == "" {
return Config{}, fmt.Errorf("S3_BUCKET must not be empty")
}
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
if strings.TrimSpace(endpoint) == "" {
return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty")
}
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
}, nil
}
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
func positiveInt(key, def string) (int64, error) {
value, err := strconv.ParseInt(getenv(key, def), 10, 64)
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
}
return value, nil
}
func readSecret(key, def string) (string, error) {
path := getenv(key, def)
b, err := os.ReadFile(path)