@@ -19,29 +19,42 @@ type Config struct {
1919 Port string
2020}
2121
22- func Load () * Config {
23- return & Config {
24- Env : getenv ("ENV" , "production" ),
25- PostgresUser : getenv ("POSTGRES_USER" , "pyazo" ),
26- PostgresPassword : getenv ("POSTGRES_PASSWORD" , "" ),
27- PostgresDB : getenv ("POSTGRES_DB" , "pyazo" ),
28- PostgresHost : getenv ("POSTGRES_HOST" , "localhost" ),
29- JWTSecret : getenv ("JWT_SECRET" , "" ),
30- BlockRegister : strings .ToLower (getenv ("BLOCK_REGISTER" , "true" )) != "false" ,
31- ImagesPath : getenv ("IMAGES_PATH" , "/images" ),
32- CORSOrigin : getenv ("CORS_ORIGIN" , "https://app.pyazo.com" ),
33- Port : getenv ("PORT" , "8000" ),
22+ func Load () (* Config , error ) {
23+ var missing []string
24+ cfg := & Config {
25+ Env : optional ("ENV" , "production" ),
26+ PostgresUser : optional ("POSTGRES_USER" , "pyazo" ),
27+ PostgresPassword : required ("POSTGRES_PASSWORD" , & missing ),
28+ PostgresDB : optional ("POSTGRES_DB" , "pyazo" ),
29+ PostgresHost : optional ("POSTGRES_HOST" , "localhost" ),
30+ JWTSecret : required ("JWT_SECRET" , & missing ),
31+ BlockRegister : strings .ToLower (optional ("BLOCK_REGISTER" , "true" )) != "false" ,
32+ ImagesPath : optional ("IMAGES_PATH" , "/images" ),
33+ CORSOrigin : optional ("CORS_ORIGIN" , "https://app.pyazo.com" ),
34+ Port : optional ("PORT" , "8000" ),
3435 }
36+ if len (missing ) > 0 {
37+ return nil , fmt .Errorf ("missing required env vars: %s" , strings .Join (missing , ", " ))
38+ }
39+ return cfg , nil
3540}
3641
37- func (c * Config ) DatabaseURL () string {
38- return fmt .Sprintf ("postgres://%s:%s@%s:5432/%s?sslmode=disable" ,
39- c .PostgresUser , c .PostgresPassword , c .PostgresHost , c .PostgresDB )
42+ func required (key string , missing * []string ) string {
43+ v := os .Getenv (key )
44+ if v == "" {
45+ * missing = append (* missing , key )
46+ }
47+ return v
4048}
4149
42- func getenv (key , fallback string ) string {
50+ func optional (key , fallback string ) string {
4351 if v := os .Getenv (key ); v != "" {
4452 return v
4553 }
4654 return fallback
4755}
56+
57+ func (c * Config ) DatabaseURL () string {
58+ return fmt .Sprintf ("postgres://%s:%s@%s:5432/%s?sslmode=disable" ,
59+ c .PostgresUser , c .PostgresPassword , c .PostgresHost , c .PostgresDB )
60+ }
0 commit comments