env_config.cr
EnvConfig
Allows easy definition of application configs based on env vars. Supports string values and all types that have a constructor new(value : String)
. Allows defining converters for more complex types (see usage). Inspired by JSON::Serializable
.
Installation
Add this to your application's shard.yml
:
dependencies:
env_config:
github: jreinert/env_config.cr
Usage
require "env_config"
require "uri"
module MyApp
def self.config
@@config ||= Config.new(ENV, prefix: "MY_APP")
end
class Config
include EnvConfig
getter server : ServerConfig
@[EnvConfig::Setting(key: "db")]
getter database : DatabaseConfig
class ServerConfig
include EnvConfig
getter host : String = "localhost"
getter port : Int32 = 3000
getter reuse_port : Bool
end
class DatabaseConfig
include EnvConfig
getter host : String = "localhost"
getter port : Int32 = 6543
getter name : String
getter username : String?
getter password : String?
def uri
URI.parse("postgres://#{host}:#{port}/#{name}").tap do |uri|
username.try(&uri.username=)
password.try(&uri.password=)
end
end
end
end
end
$> MY_APP_SERVER_HOST=0.0.0.0 MY_APP_SERVER_REUSE_PORT=1 MY_APP_DB_NAME=my_app bin/my_app
Development
If you find bugs or type conversions missing please let me know or fix/add support for them yourself. Pull requests welcome!
Contributing
- Fork it (https://github.com/jreinert/env_config.cr/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
Contributors
- jreinert Joakim Reinert - creator, maintainer
Repository
env_config.cr
Owner
Statistic
- 3
- 0
- 0
- 1
- 0
- about 6 years ago
- July 25, 2018
License
MIT License
Links
Synced at
Wed, 06 Nov 2024 11:12:25 GMT
Languages