56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
type S3 struct {
|
|
Endpoint, Region, Bucket string
|
|
AccessKey, SecretKey string
|
|
MaxBytes, MaxPixels int64
|
|
}
|
|
|
|
func LoadS3(lookup Lookup) (S3, error) {
|
|
cfg := S3{Endpoint: "http://s3:8333", Region: "us-east-1", Bucket: "messenger-attachments", MaxBytes: 10485760, MaxPixels: 40000000}
|
|
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, 10485760}, {"MAX_IMAGE_PIXELS", &cfg.MaxPixels, 40000000}} {
|
|
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
|
|
}
|
|
}
|
|
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)
|
|
}
|
|
}
|