diff --git a/Guide/controller.markdown b/Guide/controller.markdown index 0ac9c1412..bbb57a32f 100644 --- a/Guide/controller.markdown +++ b/Guide/controller.markdown @@ -159,7 +159,12 @@ This will render `Hello World, Unnamed!` when the `ExampleAction` is called with ### Accessing the FrameworkConfig inside Controllers and Views. -The config defined in `Config/Config.hs` is available through the implicit parameter `context`, a [`ConfigProvider`](https://ihp.digitallyinduced.com/api-docs/IHP-FrameworkConfig.html#t:ConfigProvider) that is available in controllers. +The config defined in `Config/Config.hs` is available through the implicit parameter `?context` that is available in controllers: + +```haskell +action MyAction = do + let config = ?context.frameworkConfig +``` There are helpers that use this implicit parameter, e.g. [`isDevelopment`](https://ihp.digitallyinduced.com/api-docs/IHP-FrameworkConfig.html#v:isDevelopment)/[`isProduction`](https://ihp.digitallyinduced.com/api-docs/IHP-FrameworkConfig.html#v:isProduction): @@ -168,8 +173,6 @@ action MyAction = do when isDevelopment (putStrLn "Running in dev mode") ``` -or you can use the function [`getFrameworkConfig`](https://ihp.digitallyinduced.com/api-docs/IHP-FrameworkConfig.html#v:getFrameworkConfig) if you need to access the config yourself. - ### Advanced: Working with Custom Types Rarely you might want to work with a custom scalar value which is not yet supported with [`param`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Param.html#v:param). Define a custom [`ParamReader`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Param.html#t:ParamReader) instance to be able to use the [`param`](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Param.html#v:param) functions with your custom value type. [For that, take a look at the existing instances of `ParamReader`.](https://ihp.digitallyinduced.com/api-docs/IHP-Controller-Param.html#t:ParamReader) diff --git a/IHP/Assets/ViewFunctions.hs b/IHP/Assets/ViewFunctions.hs index 99a6a60c9..6ab29dc46 100644 --- a/IHP/Assets/ViewFunctions.hs +++ b/IHP/Assets/ViewFunctions.hs @@ -32,7 +32,5 @@ assetPath assetPath = assetPath <> "?v=" <> assetVersion -- The asset version can be configured using the -- @IHP_ASSET_VERSION@ environment variable. assetVersion :: (?context :: ControllerContext) => Text -assetVersion = ?context - |> Config.getFrameworkConfig - |> get #assetVersion +assetVersion = ?context.frameworkConfig.assetVersion {-# INLINABLE assetVersion #-} \ No newline at end of file diff --git a/IHP/Controller/Context.hs b/IHP/Controller/Context.hs index 8712f1944..61a8cd3bc 100644 --- a/IHP/Controller/Context.hs +++ b/IHP/Controller/Context.hs @@ -133,9 +133,9 @@ putContext value = do newtype ActionType = ActionType Typeable.TypeRep -instance ConfigProvider ControllerContext where - getFrameworkConfig context = getFrameworkConfig (get #requestContext context) - {-# INLINABLE getFrameworkConfig #-} +instance HasField "frameworkConfig" ControllerContext FrameworkConfig where + getField controllerContext = controllerContext.requestContext.frameworkConfig + {-# INLINABLE getField #-} -instance LoggingProvider ControllerContext where - getLogger = getLogger . getFrameworkConfig +instance HasField "logger" ControllerContext Logger where + getField controllerContext = controllerContext.frameworkConfig.logger diff --git a/IHP/Controller/Redirect.hs b/IHP/Controller/Redirect.hs index 8d8754f45..28d213856 100644 --- a/IHP/Controller/Redirect.hs +++ b/IHP/Controller/Redirect.hs @@ -43,7 +43,7 @@ redirectTo action = redirectToPath (pathTo action) -- -- Use 'redirectTo' if you want to redirect to a controller action. redirectToPath :: (?context :: ControllerContext) => Text -> IO () -redirectToPath path = redirectToUrl (fromConfig baseUrl <> path) +redirectToPath path = redirectToUrl (?context.frameworkConfig.baseUrl <> path) {-# INLINABLE redirectToPath #-} -- | Redirects to a url (given as a string) diff --git a/IHP/Controller/RequestContext.hs b/IHP/Controller/RequestContext.hs index f78338304..bf6a46ddf 100644 --- a/IHP/Controller/RequestContext.hs +++ b/IHP/Controller/RequestContext.hs @@ -1,7 +1,6 @@ module IHP.Controller.RequestContext ( RequestContext (..) , Respond -, getConfig , RequestBody (..) ) where @@ -28,6 +27,3 @@ data RequestContext = RequestContext , vault :: (Vault.Key (Session IO ByteString ByteString)) , frameworkConfig :: FrameworkConfig } - -instance ConfigProvider RequestContext where - getFrameworkConfig = frameworkConfig diff --git a/IHP/ControllerSupport.hs b/IHP/ControllerSupport.hs index aa4a40f95..99a0dbc3c 100644 --- a/IHP/ControllerSupport.hs +++ b/IHP/ControllerSupport.hs @@ -342,9 +342,7 @@ respondAndExit response = do -- > putStrLn ("Stripe public key: " <> stripePublicKey) -- getAppConfig :: forall configParameter context. (?context :: context, ConfigProvider context, Typeable configParameter) => configParameter -getAppConfig = ?context - |> getFrameworkConfig - |> get #appConfig +getAppConfig = ?context.frameworkConfig.appConfig |> TypeMap.lookup @configParameter |> fromMaybe (error ("Could not find " <> (show (Typeable.typeRep (Typeable.Proxy @configParameter))) <>" in config")) {-# INLINE getAppConfig #-} diff --git a/IHP/DataSync/Role.hs b/IHP/DataSync/Role.hs index fcfc4ce27..5856a3044 100644 --- a/IHP/DataSync/Role.hs +++ b/IHP/DataSync/Role.hs @@ -70,6 +70,4 @@ grantPermissions role = do pure () authenticatedRole :: (?context :: context, ConfigProvider context) => Text -authenticatedRole = ?context - |> getFrameworkConfig - |> get #rlsAuthenticatedRole \ No newline at end of file +authenticatedRole = ?context.frameworkConfig.rlsAuthenticatedRole \ No newline at end of file diff --git a/IHP/ErrorController.hs b/IHP/ErrorController.hs index 0daabd896..d8b08b582 100644 --- a/IHP/ErrorController.hs +++ b/IHP/ErrorController.hs @@ -161,7 +161,7 @@ displayException exception action additionalInfo = do [ recordNotFoundExceptionHandlerProd ] - let allHandlers = if fromConfig environment == Environment.Development + let allHandlers = if ?context.frameworkConfig.environment == Environment.Development then devHandlers else prodHandlers @@ -199,7 +199,7 @@ genericHandler exception controller additionalInfo = do let prodErrorMessage = [hsx|An exception was raised while running the action|] let prodTitle = [hsx|An error happened|] - let (errorMessage, errorTitle) = if fromConfig environment == Environment.Development + let (errorMessage, errorTitle) = if ?context.frameworkConfig.environment == Environment.Development then (devErrorMessage, devTitle) else (prodErrorMessage, prodTitle) let RequestContext { respond } = get #requestContext ?context @@ -211,9 +211,7 @@ postgresHandler exception controller additionalInfo = do let handlePostgresOutdatedError :: Show exception => exception -> H.Html -> IO ResponseReceived handlePostgresOutdatedError exception errorText = do - let ihpIdeBaseUrl = ?context - |> getFrameworkConfig - |> get #ideBaseUrl + let ihpIdeBaseUrl = ?context.frameworkConfig.ideBaseUrl let title = [hsx|Database looks outdated. {errorText}|] let errorMessage = [hsx|