module EnvConfig
Overview
envconfig is an environment variable configuration mapper for Crystal. It uses syntax similar to JSON, YAML, and DB shards to map environment variables to a Crystal class. It additionally contains a configuration printer for application startup so that the config values that were interpolated can be displayed for debugging purposes.
Macros are used to handle most of the important operations, similary to JSON,
YAML, and DB mapping macros. This EnvConfig
module contains all the macros
and necessary functions to operate the library.
The normal entrypoint for code is the mapping
macro.
Defined in:
envconfig.crConstant Summary
-
VERSION =
"0.1.0"
Instance Method Summary
- #footer
- #format(name, value)
-
#get_env(key : String, default : String | Nil, nilable : Bool) : String | Nil
Fetch a key from the environment, or set a default if the key is missing.
- #header
-
#to_bool(value) : Bool
Convert string values to bools in a simplistic way.
Macro Summary
-
mapping(properties, prefix = "")
Mapping does most of the work figuring out how to configure and set the properties.
-
mapping(**properties)
This is a convenience method to allow invoking
EnvVar.mapping
with named arguments instead of with a hash/named-tuple literal.
Instance Method Detail
Fetch a key from the environment, or set a default if the key is missing. If the default is nil, abort and request that the required key be set.
Macro Detail
Mapping does most of the work figuring out how to configure and set the properties. It is to be called from inside a class you define in your code. The resultant properties are turned into properties on the class. Example Usage:
class Config
EnvConfig.mapping({
prefix: {type: String, default: "l/", nilable: false},
redis_host: {type: String, default: "localhost", nilable: false},
redis_port: {type: Int32, default: "6379", nilable: false},
redis_pool: {type: Int32, default: "200", nilable: false},
listen_port: {type: Int32, default: "8087", nilable: false},
default_url: {type: String, nilable: false},
ssl_urls: {type: Bool, default: "false", nilable: false},
}, "CHOP"
)
end
This is a convenience method to allow invoking EnvVar.mapping
with named arguments instead of with a hash/named-tuple literal.