clipper/backend/config/config.go

73 lines
1.5 KiB
Go

package config
import (
"errors"
"fmt"
"os"
)
type Environment int
const (
EnvDevelopment Environment = iota
EnvProduction
)
type Config struct {
Environment Environment
DatabaseURL string
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
S3Bucket string
}
func NewFromEnv() (Config, error) {
envString := os.Getenv("ENV")
var env Environment
switch envString {
case "production":
env = EnvProduction
case "development":
env = EnvDevelopment
case "":
return Config{}, errors.New("ENV not set")
default:
return Config{}, fmt.Errorf("invalid ENVIRONMENT value: %s", envString)
}
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
return Config{}, errors.New("DATABASE_URL not set")
}
awsAccessKeyID := os.Getenv("AWS_ACCESS_KEY_ID")
if awsAccessKeyID == "" {
return Config{}, errors.New("AWS_ACCESS_KEY_ID not set")
}
awsSecretAccessKey := os.Getenv("AWS_SECRET_ACCESS_KEY")
if awsSecretAccessKey == "" {
return Config{}, errors.New("AWS_SECRET_ACCESS_KEY not set")
}
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
return Config{}, errors.New("AWS_REGION not set")
}
s3Bucket := os.Getenv("S3_BUCKET")
if s3Bucket == "" {
return Config{}, errors.New("S3_BUCKET not set")
}
return Config{
Environment: env,
DatabaseURL: databaseURL,
AWSAccessKeyID: awsAccessKeyID,
AWSSecretAccessKey: awsSecretAccessKey,
AWSRegion: awsRegion,
S3Bucket: s3Bucket,
}, nil
}