Mercurial > gemma
comparison config/config.go @ 220:11d1a488b08f
Depend command line parsing on Viper/Cobra. Configuration should be persistent now.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 24 Jul 2018 17:45:17 +0200 |
parents | 3457a60fb12d |
children | 696c19abe869 |
comparison
equal
deleted
inserted
replaced
219:ddc78991edbc | 220:11d1a488b08f |
---|---|
1 package config | 1 package config |
2 | 2 |
3 import ( | 3 var Config Configuration |
4 "log" | |
5 "os" | |
6 "strconv" | |
7 ) | |
8 | |
9 var Config = NewConfiguration() | |
10 | 4 |
11 type Configuration struct { | 5 type Configuration struct { |
6 File string | |
12 DBHost string | 7 DBHost string |
13 DBPort uint | 8 DBPort uint |
14 DBName string | 9 DBName string |
15 DBSSLMode string | 10 DBSSLMode string |
16 | 11 |
17 SessionStore string | 12 SessionStore string |
13 | |
14 Web string | |
15 WebHost string | |
16 WebPort uint | |
18 } | 17 } |
19 | |
20 func NewConfiguration() *Configuration { | |
21 // TODO: Load from file. | |
22 return &Configuration{ | |
23 DBHost: envString("GEMMA_DB_HOST", "localhost"), | |
24 DBPort: envUint("GEMMA_DB_PORT", 5432), | |
25 DBName: envString("GEMMA_DB_NAME", "gemma"), | |
26 DBSSLMode: envString("GEMMA_DB_SSL_MODE", "require"), | |
27 SessionStore: envString("GEMMA_SESSION_STORE", ""), | |
28 } | |
29 } | |
30 | |
31 func envString(key, def string) string { | |
32 if v, ok := os.LookupEnv(key); ok { | |
33 return v | |
34 } | |
35 return def | |
36 } | |
37 | |
38 func envUint(key string, def uint) uint { | |
39 if v, ok := os.LookupEnv(key); ok { | |
40 x, err := strconv.ParseUint(v, 10, 64) | |
41 if err != nil { | |
42 log.Printf("warn: invalid uint env %s: %v\n", key, err) | |
43 return def | |
44 } | |
45 return uint(x) | |
46 } | |
47 return def | |
48 } |