Skip to content

Commit

Permalink
Configure asset version using FrameworkConfig
Browse files Browse the repository at this point in the history
The previous approach evaluated two env vars on every web request which is not efficient
  • Loading branch information
mpscholten committed Oct 18, 2021
1 parent 85cd423 commit 7005dc6
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
34 changes: 0 additions & 34 deletions IHP/Assets/ControllerFunctions.hs

This file was deleted.

21 changes: 18 additions & 3 deletions IHP/Assets/ViewFunctions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,34 @@ Copyright: (c) digitally induced GmbH, 2021
-}
module IHP.Assets.ViewFunctions
( assetPath
, assetVersion
) where

import IHP.Prelude
import IHP.Assets.Types
import IHP.Controller.Context
import qualified IHP.FrameworkConfig as Config

-- | Adds a cache buster to a asset path
--
-- >>> assetPath "/keyhandlers.js"
-- "/keyhandlers.js?v=9be8995c-7055-43d9-a1b2-43e05c210271"
--
-- The asset version can be configured using the
-- @IHP_ASSET_VERSION@ environment variable.
assetPath :: (?context :: ControllerContext) => Text -> Text
assetPath assetPath = assetPath <> "?v=" <> assetVersion
where
(AssetVersion assetVersion) = fromFrozenContext @AssetVersion
{-# INLINABLE assetPath #-}
{-# INLINABLE assetPath #-}

-- | Returns the assetVersion
--
-- >>> assetVersion
-- "9be8995c-7055-43d9-a1b2-43e05c210271"
--
-- The asset version can be configured using the
-- @IHP_ASSET_VERSION@ environment variable.
assetVersion :: (?context :: ControllerContext) => Text
assetVersion = ?context
|> Config.getFrameworkConfig
|> get #assetVersion
{-# INLINABLE assetVersion #-}
2 changes: 0 additions & 2 deletions IHP/ControllerPrelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ module IHP.ControllerPrelude
, Only (..)
, module IHP.PageHead.ControllerFunctions
, module IHP.WebSocket
, module IHP.Assets.ControllerFunctions
, module IHP.FileStorage.Types
, module IHP.FileStorage.ControllerFunctions
, module IHP.FileStorage.Preprocessor.ImageMagick
Expand Down Expand Up @@ -75,7 +74,6 @@ import IHP.LoginSupport.Helper.Controller
import IHP.PageHead.ControllerFunctions

import IHP.WebSocket
import IHP.Assets.ControllerFunctions

import IHP.FileStorage.Types
import IHP.FileStorage.ControllerFunctions
Expand Down
2 changes: 0 additions & 2 deletions IHP/ControllerSupport.hs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ import qualified Data.Aeson as Aeson
import qualified Network.Wai.Handler.WebSockets as WebSockets
import qualified Network.WebSockets as WebSockets
import qualified IHP.WebSocket as WebSockets
import qualified IHP.Assets.ControllerFunctions as Assets

type Action' = IO ResponseReceived

Expand Down Expand Up @@ -90,7 +89,6 @@ runActionWithNewContext controller = do
let ?context = controllerContext
Context.putContext ?application
Context.putContext (Context.ActionType (Typeable.typeOf controller))
Assets.initAssetVersion

try (initContext @application) >>= \case
Left exception -> do
Expand Down
27 changes: 26 additions & 1 deletion IHP/FrameworkConfig.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ newtype IdeBaseUrl = IdeBaseUrl Text
-- | Postgres role to be used for making queries with Row level security enabled
newtype RLSAuthenticatedRole = RLSAuthenticatedRole Text

newtype AssetVersion = AssetVersion Text

-- | Puts an option into the current configuration
--
-- In case an option already exists with the same type, it will not be overriden:
Expand Down Expand Up @@ -154,9 +156,18 @@ ihpDefaultConfig = do
rlsAuthenticatedRole <- fromMaybe "ihp_authenticated" <$> liftIO (Environment.lookupEnv "IHP_RLS_AUTHENTICATED_ROLE")
option $ RLSAuthenticatedRole (cs rlsAuthenticatedRole)


{-# INLINABLE ihpDefaultConfig #-}

initAssetVersion :: ConfigBuilder
initAssetVersion = do
ihpCloudContainerId <- fmap cs <$> liftIO (Environment.lookupEnv "IHP_CLOUD_CONTAINER_ID")
ihpAssetVersion <- fmap cs <$> liftIO (Environment.lookupEnv "IHP_ASSET_VERSION")
let assetVersion = [ ihpCloudContainerId, ihpAssetVersion]
|> catMaybes
|> head
|> fromMaybe "dev"
option (AssetVersion assetVersion)

findOption :: forall option. Typeable option => State.StateT TMap.TMap IO option
findOption = fromMaybe (error optionNotFoundErrorMessage) <$> findOptionOrNothing @option
where
Expand Down Expand Up @@ -191,6 +202,7 @@ buildFrameworkConfig appConfig = do
parseRequestBodyOptions <- findOption @WaiParse.ParseRequestBodyOptions
(IdeBaseUrl ideBaseUrl) <- findOption @IdeBaseUrl
(RLSAuthenticatedRole rlsAuthenticatedRole) <- findOption @RLSAuthenticatedRole
(AssetVersion assetVersion) <- findOption @AssetVersion

appConfig <- State.get

Expand Down Expand Up @@ -337,6 +349,19 @@ data FrameworkConfig = FrameworkConfig

-- | See IHP.DataSync.Role
, rlsAuthenticatedRole :: Text

-- | The asset version is used for cache busting
--
-- On IHP Cloud IHP automatically uses the @IHP_CLOUD_CONTAINER_ID@ env variable
-- as the asset version. So when running there, you don't need to do anything.
--
-- If you deploy IHP on your own, you should provide the IHP_ASSET_VERSION
-- env variable with e.g. the git commit hash of the production build.
--
-- If IHP cannot figure out an asset version, it will fallback to the static
-- string @"dev"@.
--
, assetVersion :: Text
}

class ConfigProvider a where
Expand Down
3 changes: 0 additions & 3 deletions IHP/IDE/ToolServer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ import IHP.Controller.Layout
import qualified IHP.LibDir as LibDir
import qualified IHP.IDE.LiveReloadNotificationServer as LiveReloadNotificationServer
import qualified IHP.Version as Version
import qualified IHP.Assets.Types as Assets

startToolServer :: (?context :: Context) => IO ()
startToolServer = do
Expand Down Expand Up @@ -140,5 +139,3 @@ instance ControllerSupport.InitControllerContext ToolServerApplication where
putContext webControllers
putContext (AppUrl appUrl)
setLayout Layout.toolServerLayout

putContext (Assets.AssetVersion Version.ihpVersion)
1 change: 0 additions & 1 deletion ihp.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ library
, IHP.PageHead.Types
, IHP.PageHead.ViewFunctions
, IHP.Assets.Types
, IHP.Assets.ControllerFunctions
, IHP.Assets.ViewFunctions
-- Server Side Component Modules
, IHP.ServerSideComponent.Types
Expand Down

0 comments on commit 7005dc6

Please sign in to comment.