github.com/annwntech/go-micro/v2@v2.9.5/config/source/env/README.md (about) 1 # Env Source 2 3 The env source reads config from environment variables 4 5 ## Format 6 7 We expect environment variables to be in the standard format of FOO=bar 8 9 Keys are converted to lowercase and split on underscore. 10 11 12 ### Example 13 14 ``` 15 DATABASE_ADDRESS=127.0.0.1 16 DATABASE_PORT=3306 17 ``` 18 19 Becomes 20 21 ```json 22 { 23 "database": { 24 "address": "127.0.0.1", 25 "port": 3306 26 } 27 } 28 ``` 29 30 ## Prefixes 31 32 Environment variables can be namespaced so we only have access to a subset. Two options are available: 33 34 ``` 35 WithPrefix(p ...string) 36 WithStrippedPrefix(p ...string) 37 ``` 38 39 The former will preserve the prefix and make it a top level key in the config. The latter eliminates the prefix, reducing the nesting by one. 40 41 #### Example: 42 43 Given ENVs of: 44 45 ``` 46 APP_DATABASE_ADDRESS=127.0.0.1 47 APP_DATABASE_PORT=3306 48 VAULT_ADDR=vault:1337 49 ``` 50 51 and a source initialized as follows: 52 53 ``` 54 src := env.NewSource( 55 env.WithPrefix("VAULT"), 56 env.WithStrippedPrefix("APP"), 57 ) 58 ``` 59 60 The resulting config will be: 61 62 ``` 63 { 64 "database": { 65 "address": "127.0.0.1", 66 "port": 3306 67 }, 68 "vault": { 69 "addr": "vault:1337" 70 } 71 } 72 ``` 73 74 75 ## New Source 76 77 Specify source with data 78 79 ```go 80 src := env.NewSource( 81 // optionally specify prefix 82 env.WithPrefix("MICRO"), 83 ) 84 ``` 85 86 ## Load Source 87 88 Load the source into config 89 90 ```go 91 // Create new config 92 conf := config.NewConfig() 93 94 // Load env source 95 conf.Load(src) 96 ```