clipper/backend/config/config.go

130 lines
3.1 KiB
Go
Raw Permalink Normal View History

2021-11-22 18:26:51 +00:00
package config
import (
"errors"
"fmt"
"os"
)
type Environment int
const (
EnvDevelopment Environment = iota
EnvProduction
)
2021-12-08 19:58:13 +00:00
type FileStore int
const (
FileSystemStore = iota
S3Store
)
2021-11-22 18:26:51 +00:00
type Config struct {
Environment Environment
BindAddr string
TLSCertFile string
TLSKeyFile string
DatabaseURL string
FileStore FileStore
FileStoreHTTPRoot string
FileStoreHTTPBaseURL string
AWSAccessKeyID string
AWSSecretAccessKey string
AWSRegion string
S3Bucket string
AssetsHTTPRoot string
2021-11-22 18:26:51 +00:00
}
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:
2021-11-22 20:39:23 +00:00
return Config{}, fmt.Errorf("invalid ENV value: %s", envString)
2021-11-22 18:26:51 +00:00
}
bindAddr := os.Getenv("BIND_ADDR")
if bindAddr == "" {
bindAddr = "localhost:8888"
}
tlsCertFile := os.Getenv("TLS_CERT_FILE")
tlsKeyFile := os.Getenv("TLS_KEY_FILE")
if (tlsCertFile == "" && tlsKeyFile != "") || (tlsCertFile != "" && tlsKeyFile == "") {
2021-12-08 19:58:13 +00:00
return Config{}, errors.New("both TLS_CERT_FILE and TLS_KEY_FILE must be set")
}
2021-11-22 18:26:51 +00:00
databaseURL := os.Getenv("DATABASE_URL")
if databaseURL == "" {
return Config{}, errors.New("DATABASE_URL not set")
}
2021-12-08 19:58:13 +00:00
fileStoreString := os.Getenv("FILE_STORE")
var fileStore FileStore
switch os.Getenv("FILE_STORE") {
case "s3":
fileStore = S3Store
case "filesystem", "":
fileStore = FileSystemStore
default:
return Config{}, fmt.Errorf("invalid FILE_STORE value: %s", fileStoreString)
2021-11-22 18:26:51 +00:00
}
fileStoreHTTPBaseURL := os.Getenv("FILE_STORE_HTTP_BASE_URL")
if fileStoreHTTPBaseURL == "" {
fileStoreHTTPBaseURL = "/"
}
2021-12-09 02:38:38 +00:00
var awsAccessKeyID, awsSecretAccessKey, awsRegion, s3Bucket, fileStoreHTTPRoot string
2021-12-08 19:58:13 +00:00
if fileStore == S3Store {
awsAccessKeyID = os.Getenv("AWS_ACCESS_KEY_ID")
if awsAccessKeyID == "" {
return Config{}, errors.New("AWS_ACCESS_KEY_ID not set")
}
2021-11-22 18:26:51 +00:00
2021-12-08 19:58:13 +00:00
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")
}
2021-11-22 18:26:51 +00:00
2021-12-08 19:58:13 +00:00
s3Bucket = os.Getenv("S3_BUCKET")
if s3Bucket == "" {
return Config{}, errors.New("S3_BUCKET not set")
}
} else {
2021-12-09 02:38:38 +00:00
if fileStoreHTTPRoot = os.Getenv("FILE_STORE_HTTP_ROOT"); fileStoreHTTPRoot == "" {
return Config{}, errors.New("FILE_STORE_HTTP_ROOT not set")
2021-12-08 19:58:13 +00:00
}
2021-11-22 18:26:51 +00:00
}
2021-12-09 02:38:38 +00:00
assetsHTTPRoot := os.Getenv("ASSETS_HTTP_ROOT")
2021-11-22 18:26:51 +00:00
return Config{
Environment: env,
BindAddr: bindAddr,
TLSCertFile: tlsCertFile,
TLSKeyFile: tlsKeyFile,
DatabaseURL: databaseURL,
FileStore: fileStore,
AWSAccessKeyID: awsAccessKeyID,
AWSSecretAccessKey: awsSecretAccessKey,
AWSRegion: awsRegion,
S3Bucket: s3Bucket,
AssetsHTTPRoot: assetsHTTPRoot,
FileStoreHTTPRoot: fileStoreHTTPRoot,
FileStoreHTTPBaseURL: fileStoreHTTPBaseURL,
2021-11-22 18:26:51 +00:00
}, nil
}