2021-11-22 18:26:51 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Environment int
|
|
|
|
|
|
|
|
const (
|
|
|
|
EnvDevelopment Environment = iota
|
|
|
|
EnvProduction
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Environment Environment
|
2021-11-26 16:22:25 +00:00
|
|
|
BindAddr string
|
|
|
|
TLSCertFile string
|
|
|
|
TLSKeyFile string
|
2021-11-22 18:26:51 +00:00
|
|
|
DatabaseURL string
|
|
|
|
AWSAccessKeyID string
|
|
|
|
AWSSecretAccessKey string
|
|
|
|
AWSRegion string
|
|
|
|
S3Bucket string
|
2021-11-26 04:11:49 +00:00
|
|
|
AssetsHTTPBasePath 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
|
|
|
}
|
|
|
|
|
2021-11-26 16:22:25 +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 == "") {
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2021-11-26 04:11:49 +00:00
|
|
|
assetsHTTPBasePath := os.Getenv("ASSETS_HTTP_BASE_PATH")
|
|
|
|
|
2021-11-22 18:26:51 +00:00
|
|
|
return Config{
|
|
|
|
Environment: env,
|
2021-11-26 16:22:25 +00:00
|
|
|
BindAddr: bindAddr,
|
|
|
|
TLSCertFile: tlsCertFile,
|
|
|
|
TLSKeyFile: tlsKeyFile,
|
2021-11-22 18:26:51 +00:00
|
|
|
DatabaseURL: databaseURL,
|
|
|
|
AWSAccessKeyID: awsAccessKeyID,
|
|
|
|
AWSSecretAccessKey: awsSecretAccessKey,
|
|
|
|
AWSRegion: awsRegion,
|
|
|
|
S3Bucket: s3Bucket,
|
2021-11-26 04:11:49 +00:00
|
|
|
AssetsHTTPBasePath: assetsHTTPBasePath,
|
2021-11-22 18:26:51 +00:00
|
|
|
}, nil
|
|
|
|
}
|