Parse environment into a Config struct
()
| 45 | |
| 46 | // Parse environment into a Config struct |
| 47 | func Parse() *Config { |
| 48 | var cfg Config |
| 49 | |
| 50 | // with config file loaded into env values, we can now parse env into our config struct |
| 51 | err := envconfig.Process("Fathom", &cfg) |
| 52 | if err != nil { |
| 53 | log.Fatalf("Error parsing configuration from environment: %s", err) |
| 54 | } |
| 55 | |
| 56 | if cfg.Database.URL != "" { |
| 57 | u, err := url.Parse(cfg.Database.URL) |
| 58 | if err != nil { |
| 59 | log.Fatalf("Error parsing DATABASE_URL from environment: %s", err) |
| 60 | } |
| 61 | if u.Scheme == "postgres" { |
| 62 | cfg.Database.Driver = "postgres" |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // alias sqlite to sqlite3 |
| 67 | if cfg.Database.Driver == "sqlite" { |
| 68 | cfg.Database.Driver = "sqlite3" |
| 69 | } |
| 70 | |
| 71 | // use absolute path to sqlite3 database |
| 72 | if cfg.Database.Driver == "sqlite3" { |
| 73 | cfg.Database.Name, _ = filepath.Abs(cfg.Database.Name) |
| 74 | } |
| 75 | |
| 76 | // if secret key is empty, use a randomly generated one |
| 77 | if cfg.Secret == "" { |
| 78 | cfg.Secret = randomString(40) |
| 79 | } |
| 80 | |
| 81 | return &cfg |
| 82 | } |
| 83 | |
| 84 | func randomString(len int) string { |
| 85 | bytes := make([]byte, len) |