From 057f57ff0d96700cdfc883e4a07c76f7c1d15fc8 Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Sat, 15 Jun 2019 08:56:20 +0000 Subject: [PATCH] Load settings only at run time, not using compile time settings at all --- src/Vervis/Application.hs | 5 +-- src/Vervis/Foundation.hs | 3 +- src/Vervis/Settings.hs | 50 +++++++++++++++++++----------- src/Vervis/Settings/StaticFiles.hs | 4 +-- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Vervis/Application.hs b/src/Vervis/Application.hs index 35ecece..9422b09 100644 --- a/src/Vervis/Application.hs +++ b/src/Vervis/Application.hs @@ -120,7 +120,7 @@ makeFoundation appSettings = do appLogger <- newStdoutLoggerSet defaultBufSize >>= makeYesodLogger appStatic <- (if appMutableStatic appSettings then staticDevel else static) - (appStaticDir appSettings) + appStaticDir appMailQueue <- case appMail appSettings of @@ -286,7 +286,8 @@ appMain = do settings <- loadYamlSettingsArgs -- Fall back to compile-time values, set to [] to require values at -- runtime - [configSettingsYmlValue] + --[configSettingsYmlValue] + [] -- Allow environment variables to override useEnv diff --git a/src/Vervis/Foundation.hs b/src/Vervis/Foundation.hs index 93182a5..d1857cc 100644 --- a/src/Vervis/Foundation.hs +++ b/src/Vervis/Foundation.hs @@ -486,11 +486,10 @@ instance Yesod App where -- users receiving stale content. addStaticContent ext mime content = do master <- getYesod - let staticDir = appStaticDir $ appSettings master addStaticContentExternal discardm genFileName - staticDir + appStaticDir (StaticR . flip StaticRoute []) ext mime diff --git a/src/Vervis/Settings.hs b/src/Vervis/Settings.hs index 5193055..d4eafb1 100644 --- a/src/Vervis/Settings.hs +++ b/src/Vervis/Settings.hs @@ -50,14 +50,34 @@ import qualified Data.Text as T import Yesod.Mail.Send (MailSettings) +developmentMode :: Bool +developmentMode = +#if DEVELOPMENT + True +#else + False +#endif + +-- | Directory from which to serve static files. +appStaticDir :: String +appStaticDir = "static" + +-- | Use the reload version of templates +appReloadTemplates :: Bool +appReloadTemplates = developmentMode + +-- | Perform no stylesheet/script combining +appSkipCombining :: Bool +appSkipCombining = developmentMode + -- | Runtime settings to configure this application. These settings can be -- loaded from various sources: defaults, environment variables, config files, -- theoretically even a database. data AppSettings = AppSettings { -- | Directory from which to serve static files. - appStaticDir :: String + --appStaticDir :: String -- | Configuration settings for accessing the database. - , appDatabaseConf :: PostgresConf + appDatabaseConf :: PostgresConf -- | Maximal number of remote instance-scope keys to cache in our local -- database per instance. , appMaxInstanceKeys :: Maybe Int @@ -93,11 +113,11 @@ data AppSettings = AppSettings -- | Should all log messages be displayed? , appShouldLogAll :: Bool -- | Use the reload version of templates - , appReloadTemplates :: Bool + --, appReloadTemplates :: Bool -- | Assume that files in the static dir may change after compilation , appMutableStatic :: Bool -- | Perform no stylesheet/script combining - , appSkipCombining :: Bool + --, appSkipCombining :: Bool -- | Load SVG font file from the data file path of the @SVGFonts@ -- library, instead of the app's production runtime data directory. @@ -166,18 +186,10 @@ data AppSettings = AppSettings , appHighlightStyle :: Text } -developmentMode :: Bool -developmentMode = -#if DEVELOPMENT - True -#else - False -#endif - instance FromJSON AppSettings where parseJSON = withObject "AppSettings" $ \ o -> do let defaultDev = developmentMode - appStaticDir <- o .: "static-dir" + --appStaticDir <- o .: "static-dir" appDatabaseConf <- o .: "database" appMaxInstanceKeys <- o .:? "max-instance-keys" appMaxActorKeys <- o .:? "max-actor-keys" @@ -194,9 +206,9 @@ instance FromJSON AppSettings where appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev appShouldLogAll <- o .:? "should-log-all" .!= defaultDev - appReloadTemplates <- o .:? "reload-templates" .!= defaultDev + --appReloadTemplates <- o .:? "reload-templates" .!= defaultDev appMutableStatic <- o .:? "mutable-static" .!= defaultDev - appSkipCombining <- o .:? "skip-combining" .!= defaultDev + --appSkipCombining <- o .:? "skip-combining" .!= defaultDev appLoadFontFromLibData <- o .:? "load-font-from-lib-data" .!= defaultDev @@ -249,11 +261,12 @@ combineSettings = def widgetFile :: String -> Q Exp widgetFile = let wf = - if appReloadTemplates compileTimeAppSettings + if appReloadTemplates then widgetFileReload else widgetFileNoReload in wf widgetFileSettings +{- -- | Raw bytes at compile time of @config/settings.yml@ configSettingsYmlBS :: ByteString configSettingsYmlBS = $(embedFile configSettingsYml) @@ -269,6 +282,7 @@ compileTimeAppSettings = case fromJSON $ applyEnvValue False mempty configSettingsYmlValue of Error e -> error e Success settings -> settings +-} -- The following two functions can be used to combine multiple CSS or JS files -- at compile time to decrease the number of http requests. @@ -279,11 +293,11 @@ compileTimeAppSettings = combineStylesheets :: Name -> [Route Static] -> Q Exp combineStylesheets = combineStylesheets' - (appSkipCombining compileTimeAppSettings) + appSkipCombining combineSettings combineScripts :: Name -> [Route Static] -> Q Exp combineScripts = combineScripts' - (appSkipCombining compileTimeAppSettings) + appSkipCombining combineSettings diff --git a/src/Vervis/Settings/StaticFiles.hs b/src/Vervis/Settings/StaticFiles.hs index f9a73d3..9df69cb 100644 --- a/src/Vervis/Settings/StaticFiles.hs +++ b/src/Vervis/Settings/StaticFiles.hs @@ -15,7 +15,7 @@ module Vervis.Settings.StaticFiles where -import Vervis.Settings (appStaticDir, compileTimeAppSettings) +import Vervis.Settings (appStaticDir) import Yesod.Static (staticFiles) -- This generates easy references to files in the static directory at compile time, @@ -30,4 +30,4 @@ import Yesod.Static (staticFiles) -- If the identifier is not available, you may use: -- -- StaticFile ["js", "script.js"] [] -staticFiles (appStaticDir compileTimeAppSettings) +staticFiles appStaticDir