go-micro.dev/v5@v5.12.0/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  ### Format example
    12  
    13  ```bash
    14  DATABASE_ADDRESS=127.0.0.1
    15  DATABASE_PORT=3306
    16  ```
    17  
    18  Becomes
    19  
    20  ```json
    21  {
    22    "database": {
    23      "address": "127.0.0.1",
    24      "port": 3306
    25    }
    26  }
    27  ```
    28  
    29  ## Prefixes
    30  
    31  Environment variables can be namespaced so we only have access to a subset. Two options are available:
    32  
    33  ```go
    34  WithPrefix(p ...string)
    35  WithStrippedPrefix(p ...string)
    36  ```
    37  
    38  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.
    39  
    40  ### Prefixes example
    41  
    42  Given ENVs of:
    43  
    44  ```bash
    45  APP_DATABASE_ADDRESS=127.0.0.1
    46  APP_DATABASE_PORT=3306
    47  VAULT_ADDR=vault:1337
    48  ```
    49  
    50  and a source initialized as follows:
    51  
    52  ```go
    53  src := env.NewSource(
    54      env.WithPrefix("VAULT"),
    55      env.WithStrippedPrefix("APP"),
    56  )
    57  ```
    58  
    59  The resulting config will be:
    60  
    61  ```json
    62  {
    63    "database": {
    64      "address": "127.0.0.1",
    65      "port": 3306
    66    },
    67    "vault": {
    68      "addr": "vault:1337"
    69    }
    70  }
    71  ```
    72  
    73  ## New Source
    74  
    75  Specify source with data
    76  
    77  ```go
    78  src := env.NewSource(
    79    // optionally specify prefix
    80    env.WithPrefix("MICRO"),
    81  )
    82  ```
    83  
    84  ## Load Source
    85  
    86  Load the source into config
    87  
    88  ```go
    89  // Create new config
    90  conf := config.NewConfig()
    91  
    92  // Load env source
    93  conf.Load(src)
    94  ```