forked from mtusi2026/mtusi2026lab4
feat: add S3 attachments and reconciliation
This commit is contained in:
+93
-24
@@ -12,29 +12,39 @@ import (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Address string
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
RequestTimeout time.Duration
|
||||
HTTPReadHeaderTimeout time.Duration
|
||||
HTTPReadTimeout time.Duration
|
||||
HTTPWriteTimeout time.Duration
|
||||
HTTPIdleTimeout time.Duration
|
||||
HTTPShutdownTimeout time.Duration
|
||||
PasswordMemoryKiB uint32
|
||||
PasswordIterations uint32
|
||||
PasswordParallelism uint8
|
||||
PasswordSaltLength uint32
|
||||
PasswordKeyLength uint32
|
||||
SwaggerPort string
|
||||
Address string
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
RequestTimeout time.Duration
|
||||
HTTPReadHeaderTimeout time.Duration
|
||||
HTTPReadTimeout time.Duration
|
||||
HTTPWriteTimeout time.Duration
|
||||
HTTPIdleTimeout time.Duration
|
||||
HTTPShutdownTimeout time.Duration
|
||||
PasswordMemoryKiB uint32
|
||||
PasswordIterations uint32
|
||||
PasswordParallelism uint8
|
||||
PasswordSaltLength uint32
|
||||
PasswordKeyLength uint32
|
||||
SwaggerPort string
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKey string
|
||||
S3SecretKey string
|
||||
S3UseTLS bool
|
||||
S3Timeout time.Duration
|
||||
AttachmentMaxBytes int64
|
||||
AttachmentMaxPixels int64
|
||||
AttachmentDecodeMemory int64
|
||||
}
|
||||
|
||||
type MigrationConfig struct {
|
||||
@@ -142,6 +152,50 @@ func Load() (Config, error) {
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3Endpoint, err := requiredString(values, "S3_ENDPOINT")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3Region, err := requiredString(values, "S3_REGION")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3Bucket, err := requiredString(values, "S3_BUCKET")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3AccessKey, err := requiredString(values, "S3_ACCESS_KEY")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3SecretKey, err := requiredString(values, "S3_SECRET_KEY")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3UseTLS, err := boolean(values, "S3_USE_TLS")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3Timeout, err := seconds(values, "S3_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
attachmentMaxBytes, err := integer(values, "ATTACHMENT_MAX_BYTES", 1, 10*1024*1024)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
attachmentMaxPixels, err := integer(values, "ATTACHMENT_MAX_PIXELS", 1, 40_000_000)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
attachmentDecodeMemory, err := integer(values, "ATTACHMENT_DECODE_MEMORY_BYTES", 1, 1024*1024*1024)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
minimumDecodeMemory := int64(attachmentMaxBytes) + int64(attachmentMaxPixels)*8
|
||||
if int64(attachmentDecodeMemory) < minimumDecodeMemory {
|
||||
return Config{}, fmt.Errorf("ATTACHMENT_DECODE_MEMORY_BYTES: must be at least %d", minimumDecodeMemory)
|
||||
}
|
||||
|
||||
return Config{
|
||||
Address: net.JoinHostPort(host, port), DatabaseURL: database.url, CacheURL: cacheURL,
|
||||
@@ -155,7 +209,10 @@ func Load() (Config, error) {
|
||||
HTTPShutdownTimeout: shutdownTimeout, PasswordMemoryKiB: uint32(passwordMemory),
|
||||
PasswordIterations: uint32(passwordIterations), PasswordParallelism: uint8(passwordParallelism),
|
||||
PasswordSaltLength: uint32(passwordSaltLength), PasswordKeyLength: uint32(passwordKeyLength),
|
||||
SwaggerPort: swaggerPort,
|
||||
SwaggerPort: swaggerPort, S3Endpoint: s3Endpoint, S3Region: s3Region, S3Bucket: s3Bucket,
|
||||
S3AccessKey: s3AccessKey, S3SecretKey: s3SecretKey, S3UseTLS: s3UseTLS, S3Timeout: s3Timeout,
|
||||
AttachmentMaxBytes: int64(attachmentMaxBytes), AttachmentMaxPixels: int64(attachmentMaxPixels),
|
||||
AttachmentDecodeMemory: int64(attachmentDecodeMemory),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -260,3 +317,15 @@ func integerValue(value string, minimum, maximum int) (int, error) {
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func boolean(values *viper.Viper, name string) (bool, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
parsed, err := strconv.ParseBool(value)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s: must be true or false", name)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user