Skip to content

Commit

Permalink
Make the full app config from Config.hs available through FrameworkCo…
Browse files Browse the repository at this point in the history
…nfig.appConfig
  • Loading branch information
mpscholten committed Apr 29, 2021
1 parent dc21239 commit c955eeb
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions IHP/FrameworkConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ buildFrameworkConfig appConfig = do
logger <- findOption @Logger
exceptionTracker <- findOption @ExceptionTracker

appConfig <- State.get

pure FrameworkConfig { .. }

(frameworkConfig, _) <- State.runStateT (appConfig >> ihpDefaultConfig >> resolve) TMap.empty
Expand Down Expand Up @@ -219,6 +221,37 @@ data FrameworkConfig = FrameworkConfig
, cssFramework :: !CSSFramework
, logger :: !Logger
, exceptionTracker :: !ExceptionTracker

-- | Custom 'option's from @Config.hs@ are stored here
--
-- To access a custom option here, first set it up inside @Config.hs@. This example
-- reads a string from a env variable on app startup and makes it available to the app
-- by saving it into the application context:
--
-- > -- Config.hs:
-- >
-- > newtype RedisUrl = RedisUrl String
-- >
-- > config :: ConfigBuilder
-- > config = do
-- > option Development
-- > option (AppHostname "localhost")
-- >
-- > redisUrl <- liftIO $ System.Environment.getEnv "REDIS_URL"
-- > option (RedisUrl redisUrl)
--
-- This redis url can be access from all IHP entrypoints using the ?applicationContext that is in scope:
--
-- > import qualified Data.TMap as TMap
-- > import Config -- For accessing the RedisUrl data type
-- >
-- > action MyAction = do
-- > let appConfig = ?applicationCotext |> get #frameworkConfig |> get #appConfig
-- > let (RedisUrl redisUrl) = appConfig
-- > |> TMap.lookup @RedisUrl
-- > |> fromMaybe (error "Could not find RedisUrl in config")
-- >
, appConfig :: !TMap.TMap
}

class ConfigProvider a where
Expand Down

0 comments on commit c955eeb

Please sign in to comment.