Перенеси реализацию мессенджера из ЛР 6
Источник — проверенное рабочее дерево ЛР 6 поверх 18878d6, включая изменения после повторного ревью. SHA-256 списка исходных файлов: e45a3d85b41feff1da78dce3f20fd1f4af1c5ee629e6ed703218bc8e21ad58fe.
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/internal/imaging"
|
||||
)
|
||||
|
||||
type S3 struct {
|
||||
Endpoint, Region, Bucket string
|
||||
AccessKey, SecretKey string
|
||||
MaxBytes, MaxPixels int64
|
||||
StagingRetention time.Duration
|
||||
}
|
||||
|
||||
func LoadS3(lookup Lookup) (S3, error) {
|
||||
cfg := S3{
|
||||
Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments",
|
||||
MaxBytes: imaging.MaxUploadBytes, MaxPixels: imaging.MaxImagePixels, StagingRetention: 24 * time.Hour,
|
||||
}
|
||||
for key, dest := range map[string]*string{"S3_ENDPOINT": &cfg.Endpoint, "S3_REGION": &cfg.Region, "S3_BUCKET": &cfg.Bucket} {
|
||||
if v, ok := lookup(key); ok {
|
||||
*dest = v
|
||||
}
|
||||
}
|
||||
var err error
|
||||
if cfg.AccessKey, err = secret(lookup, "S3_ACCESS_KEY_ID_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
if cfg.SecretKey, err = secret(lookup, "S3_SECRET_ACCESS_KEY_FILE"); err != nil {
|
||||
return S3{}, err
|
||||
}
|
||||
for _, p := range []struct {
|
||||
key string
|
||||
dest *int64
|
||||
max int64
|
||||
}{{"MAX_UPLOAD_BYTES", &cfg.MaxBytes, imaging.MaxUploadBytes}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, imaging.MaxImagePixels}} {
|
||||
if v, ok := lookup(p.key); ok {
|
||||
n, e := strconv.ParseInt(v, 10, 64)
|
||||
if e != nil || n < 1 || n > p.max {
|
||||
return S3{}, fmt.Errorf("%s must be between 1 and %d", p.key, p.max)
|
||||
}
|
||||
*p.dest = n
|
||||
}
|
||||
}
|
||||
if raw, ok := lookup("STAGING_RETENTION_SECONDS"); ok {
|
||||
const maxSeconds = math.MaxInt64 / int64(time.Second)
|
||||
n, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || n < 1 || n > maxSeconds {
|
||||
return S3{}, fmt.Errorf("STAGING_RETENTION_SECONDS must be between 1 and %d", maxSeconds)
|
||||
}
|
||||
cfg.StagingRetention = time.Duration(n) * time.Second
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// ReconcileS3 selects the separate listing identity used by background scans.
|
||||
func ReconcileS3(lookup Lookup) Lookup {
|
||||
return func(key string) (string, bool) {
|
||||
if key == "S3_ACCESS_KEY_ID_FILE" {
|
||||
return lookup("S3_RECONCILE_ACCESS_KEY_ID_FILE")
|
||||
}
|
||||
if key == "S3_SECRET_ACCESS_KEY_FILE" {
|
||||
return lookup("S3_RECONCILE_SECRET_ACCESS_KEY_FILE")
|
||||
}
|
||||
return lookup(key)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user