Safe Haskell | None |
---|
IHP.EnvVar
Synopsis
- env :: forall result monad. (MonadIO monad, EnvVarReader result) => ByteString -> monad result
- envOrDefault :: (MonadIO monad, EnvVarReader result) => ByteString -> result -> monad result
- envOrNothing :: (MonadIO monad, EnvVarReader result) => ByteString -> monad (Maybe result)
- hasEnvVar :: MonadIO monad => ByteString -> monad Bool
- class EnvVarReader valueType where
- envStringToValue :: ByteString -> Either Text valueType
Documentation
env :: forall result monad. (MonadIO monad, EnvVarReader result) => ByteString -> monad result Source #
Returns a env variable. The raw string value is parsed before returning it. So the return value type depends on what you expect (e.g. can be Text, Int some custom type).
When the parameter is missing or cannot be parsed, an error is raised and
the app startup is aborted. Use envOrDefault
when you want to get a
default value instead of an error, or paramOrNothing
to get Nothing
when the env variable is missing.
You can define a custom env variable parser by defining a EnvVarReader
instance.
Example: Accessing a env var PORT.
Let's say an env var PORT is set to 1337
export PORT=1337
We can read PORT
like this:
port <- env @Int "PORT"
Example: Missing env vars
Let's say the PORT
env var is not defined. In that case we'll get an
error when trying to access it:
>>>
port <- env @Int "PORT"
"Env var 'PORT' not set, but it's required for the app to run"
envOrDefault :: (MonadIO monad, EnvVarReader result) => ByteString -> result -> monad result Source #
envOrNothing :: (MonadIO monad, EnvVarReader result) => ByteString -> monad (Maybe result) Source #
class EnvVarReader valueType where Source #
Methods
envStringToValue :: ByteString -> Either Text valueType Source #