Skip to content

Commit bc4864d

Browse files
committed
update config
1 parent 806374d commit bc4864d

File tree

1 file changed

+72
-13
lines changed

1 file changed

+72
-13
lines changed

README.md

+72-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,67 @@
33
EnvConfig is a library that makes it easier to write `runtime.exs` files that take in parameters
44
from the environment.
55

6-
The idea is that EnvConfig allows you to define which environment variables an application needs,
6+
The idea is that `EnvConfig` allows you to define which environment variables an application needs,
77
what their type is expected to be, and whether they are optional for the application to start.
88

9+
The api ensures that the values are read from the environment and are cast into their proper Elixir types.
10+
11+
**Before**
12+
```elixir
13+
import Config
14+
15+
if config_env() == :prod do
16+
secret_key_base =
17+
System.get_env("SECRET_KEY_BASE") ||
18+
raise """
19+
environment variable SECRET_KEY_BASE is missing.
20+
You can generate one by calling: mix phx.gen.secret
21+
"""
22+
23+
if System.get_env("PHX_SERVER") do
24+
config :my_app, MyAppWeb.Endpoint, server: true
25+
end
26+
end
27+
```
28+
29+
**After**
30+
31+
```elixir
32+
import Config
33+
require EnvConfig.Macros
34+
import EnvConfig.Macros
35+
36+
if config_env() == :prod do
37+
secret_key_base = required("SECRET_KEY_BASE", :string, min_length: 64)
38+
39+
phx_server = optional("PHX_SERVER", :boolean, false)
40+
config :my_app, MyAppWeb.Endpoint, server: phx_server
41+
end
42+
```
43+
44+
If the environment variable is not defined, you will see the same error.
45+
46+
```text
47+
** (RuntimeError) Environment variable SECRET_KEY_BASE is not set
48+
/Users/christophe/Documents/Code/loomy/data-api/config/runtime.exs:5: (file)
49+
(stdlib 6.2) erl_eval.erl:919: :erl_eval.do_apply/7
50+
(stdlib 6.2) erl_eval.erl:663: :erl_eval.expr/6
51+
(stdlib 6.2) erl_eval.erl:271: :erl_eval.exprs/6
52+
(elixir 1.18.1) lib/code.ex:572: Code.validated_eval_string/3
53+
```
54+
55+
Or, when the type or constraints are not met, an error explaining whats wrong.
56+
57+
```text
58+
** (RuntimeError) Environment variable SECRET_KEY_BASE does not meet constraints. string is shorter than 64 characters
59+
/Users/christophe/Documents/Code/loomy/data-api/config/runtime.exs:5: (file)
60+
(stdlib 6.2) erl_eval.erl:919: :erl_eval.do_apply/7
61+
(stdlib 6.2) erl_eval.erl:663: :erl_eval.expr/6
62+
(stdlib 6.2) erl_eval.erl:271: :erl_eval.exprs/6
63+
(elixir 1.18.1) lib/code.ex:572: Code.validated_eval_string/3
64+
```
65+
66+
967
## Installation
1068

1169
The package can be installed
@@ -21,15 +79,16 @@ end
2179

2280
## Types
2381

24-
- `:boolean`
25-
- `:integer`
26-
- Constraints
27-
- `{:min, integer}`: minimal required value
28-
- `{:max, integer}`: maximal required value
29-
- `:string`
30-
- Constraints
31-
- `{:allow_empty?, boolean}`: allow the (trimmed) string to be empty
32-
- `{:min_length, integer}`: minimal length of the string
33-
- `{:max_length, integer}`: maximum length of the string
34-
35-
## Example
82+
EnvConfig supports basic types out of the box, and lists of these types.
83+
84+
| Type | Values | Elixir Type |
85+
|---------------------|------------------------------------------------------------------------------|--------------|
86+
| `:boolean` | `true`, `TRUE`, `1`, `false`, `FALSE`, `0` | `boolean()` |
87+
| `:string` | any value | `String.t()` |
88+
| `:atom` | any value | `atom()` |
89+
| `charlist` | any value | `charlist()` |
90+
| `integer` | any integral value that's parsable as an integer using `&Integer.parse/1` | `integer()` |
91+
| `:float` | any value that's parsable as a float using `&Float.parse/1` | `float()` |
92+
| `{:list, type}` | a comma-separated list of values for any given type in the above table. | `[type]` |
93+
| `{:enum, [binary]}` | any value that is a member of the values given in the list of the enum type. | `String.t()` |
94+
|---------------------|------------------------------------------------------------------------------|--------------|

0 commit comments

Comments
 (0)