forked from mtusi2026/mtusi2026lab4
refactor(s3): read S3 secrets through app config
This commit is contained in:
@@ -20,6 +20,7 @@ type Config struct {
|
||||
Logging LoggingConfig
|
||||
Database DatabaseConfig
|
||||
Redis RedisConfig
|
||||
S3 S3Config
|
||||
SessionStore string
|
||||
}
|
||||
|
||||
@@ -56,6 +57,34 @@ func (c RedisConfig) URL() (string, error) {
|
||||
return readSecretFile(c.URLFile)
|
||||
}
|
||||
|
||||
type S3Config struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
APIAccessKeyFile string
|
||||
APISecretKeyFile string
|
||||
BootstrapAccessKeyFile string
|
||||
BootstrapSecretKeyFile string
|
||||
MaxUploadBytes int
|
||||
MaxImagePixels int
|
||||
}
|
||||
|
||||
func (c S3Config) APIAccessKey() (string, error) {
|
||||
return readSecretFile(c.APIAccessKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) APISecretKey() (string, error) {
|
||||
return readSecretFile(c.APISecretKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) BootstrapAccessKey() (string, error) {
|
||||
return readSecretFile(c.BootstrapAccessKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) BootstrapSecretKey() (string, error) {
|
||||
return readSecretFile(c.BootstrapSecretKeyFile)
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Server: ServerConfig{
|
||||
@@ -79,10 +108,26 @@ func Load() Config {
|
||||
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
|
||||
},
|
||||
|
||||
S3: LoadS3(),
|
||||
|
||||
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
|
||||
}
|
||||
}
|
||||
|
||||
func LoadS3() S3Config {
|
||||
return S3Config{
|
||||
Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"),
|
||||
Region: getEnv("S3_REGION", "us-east-1"),
|
||||
Bucket: getEnv("S3_BUCKET", "messenger-attachments"),
|
||||
APIAccessKeyFile: getEnv("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"),
|
||||
APISecretKeyFile: getEnv("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"),
|
||||
BootstrapAccessKeyFile: getEnv("S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", "/run/secrets/s3_bootstrap_access_key_id"),
|
||||
BootstrapSecretKeyFile: getEnv("S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_bootstrap_secret_access_key"),
|
||||
MaxUploadBytes: getEnvPositiveInt("MAX_UPLOAD_BYTES", 10485760),
|
||||
MaxImagePixels: getEnvPositiveInt("MAX_IMAGE_PIXELS", 40000000),
|
||||
}
|
||||
}
|
||||
|
||||
func getEnv(key, def string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
|
||||
Reference in New Issue
Block a user