module

EnvConfig

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.

Constants

VERSION = "0.1.0"

Instance methods

format(name, value)
Source
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. If the default is nil, abort and request that the required key be set.

Source
header
Source
to_bool(value) : Bool

Convert string values to bools in a simplistic way.

Source

Macros

mapping(properties, prefix = "")

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
Source
mapping

This is a convenience method to allow invoking EnvVar.mapping with named arguments instead of with a hash/named-tuple literal.

Source