Load settings only at run time, not using compile time settings at all

This commit is contained in:
fr33domlover 2019-06-15 08:56:20 +00:00
parent 4b20ed23b6
commit 057f57ff0d
4 changed files with 38 additions and 24 deletions

View file

@ -120,7 +120,7 @@ makeFoundation appSettings = do
appLogger <- newStdoutLoggerSet defaultBufSize >>= makeYesodLogger appLogger <- newStdoutLoggerSet defaultBufSize >>= makeYesodLogger
appStatic <- appStatic <-
(if appMutableStatic appSettings then staticDevel else static) (if appMutableStatic appSettings then staticDevel else static)
(appStaticDir appSettings) appStaticDir
appMailQueue <- appMailQueue <-
case appMail appSettings of case appMail appSettings of
@ -286,7 +286,8 @@ appMain = do
settings <- loadYamlSettingsArgs settings <- loadYamlSettingsArgs
-- Fall back to compile-time values, set to [] to require values at -- Fall back to compile-time values, set to [] to require values at
-- runtime -- runtime
[configSettingsYmlValue] --[configSettingsYmlValue]
[]
-- Allow environment variables to override -- Allow environment variables to override
useEnv useEnv

View file

@ -486,11 +486,10 @@ instance Yesod App where
-- users receiving stale content. -- users receiving stale content.
addStaticContent ext mime content = do addStaticContent ext mime content = do
master <- getYesod master <- getYesod
let staticDir = appStaticDir $ appSettings master
addStaticContentExternal addStaticContentExternal
discardm discardm
genFileName genFileName
staticDir appStaticDir
(StaticR . flip StaticRoute []) (StaticR . flip StaticRoute [])
ext ext
mime mime

View file

@ -50,14 +50,34 @@ import qualified Data.Text as T
import Yesod.Mail.Send (MailSettings) 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 -- | Runtime settings to configure this application. These settings can be
-- loaded from various sources: defaults, environment variables, config files, -- loaded from various sources: defaults, environment variables, config files,
-- theoretically even a database. -- theoretically even a database.
data AppSettings = AppSettings data AppSettings = AppSettings
{ -- | Directory from which to serve static files. { -- | Directory from which to serve static files.
appStaticDir :: String --appStaticDir :: String
-- | Configuration settings for accessing the database. -- | Configuration settings for accessing the database.
, appDatabaseConf :: PostgresConf appDatabaseConf :: PostgresConf
-- | Maximal number of remote instance-scope keys to cache in our local -- | Maximal number of remote instance-scope keys to cache in our local
-- database per instance. -- database per instance.
, appMaxInstanceKeys :: Maybe Int , appMaxInstanceKeys :: Maybe Int
@ -93,11 +113,11 @@ data AppSettings = AppSettings
-- | Should all log messages be displayed? -- | Should all log messages be displayed?
, appShouldLogAll :: Bool , appShouldLogAll :: Bool
-- | Use the reload version of templates -- | Use the reload version of templates
, appReloadTemplates :: Bool --, appReloadTemplates :: Bool
-- | Assume that files in the static dir may change after compilation -- | Assume that files in the static dir may change after compilation
, appMutableStatic :: Bool , appMutableStatic :: Bool
-- | Perform no stylesheet/script combining -- | Perform no stylesheet/script combining
, appSkipCombining :: Bool --, appSkipCombining :: Bool
-- | Load SVG font file from the data file path of the @SVGFonts@ -- | Load SVG font file from the data file path of the @SVGFonts@
-- library, instead of the app's production runtime data directory. -- library, instead of the app's production runtime data directory.
@ -166,18 +186,10 @@ data AppSettings = AppSettings
, appHighlightStyle :: Text , appHighlightStyle :: Text
} }
developmentMode :: Bool
developmentMode =
#if DEVELOPMENT
True
#else
False
#endif
instance FromJSON AppSettings where instance FromJSON AppSettings where
parseJSON = withObject "AppSettings" $ \ o -> do parseJSON = withObject "AppSettings" $ \ o -> do
let defaultDev = developmentMode let defaultDev = developmentMode
appStaticDir <- o .: "static-dir" --appStaticDir <- o .: "static-dir"
appDatabaseConf <- o .: "database" appDatabaseConf <- o .: "database"
appMaxInstanceKeys <- o .:? "max-instance-keys" appMaxInstanceKeys <- o .:? "max-instance-keys"
appMaxActorKeys <- o .:? "max-actor-keys" appMaxActorKeys <- o .:? "max-actor-keys"
@ -194,9 +206,9 @@ instance FromJSON AppSettings where
appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev
appShouldLogAll <- o .:? "should-log-all" .!= defaultDev appShouldLogAll <- o .:? "should-log-all" .!= defaultDev
appReloadTemplates <- o .:? "reload-templates" .!= defaultDev --appReloadTemplates <- o .:? "reload-templates" .!= defaultDev
appMutableStatic <- o .:? "mutable-static" .!= defaultDev appMutableStatic <- o .:? "mutable-static" .!= defaultDev
appSkipCombining <- o .:? "skip-combining" .!= defaultDev --appSkipCombining <- o .:? "skip-combining" .!= defaultDev
appLoadFontFromLibData <- o .:? "load-font-from-lib-data" .!= defaultDev appLoadFontFromLibData <- o .:? "load-font-from-lib-data" .!= defaultDev
@ -249,11 +261,12 @@ combineSettings = def
widgetFile :: String -> Q Exp widgetFile :: String -> Q Exp
widgetFile = widgetFile =
let wf = let wf =
if appReloadTemplates compileTimeAppSettings if appReloadTemplates
then widgetFileReload then widgetFileReload
else widgetFileNoReload else widgetFileNoReload
in wf widgetFileSettings in wf widgetFileSettings
{-
-- | Raw bytes at compile time of @config/settings.yml@ -- | Raw bytes at compile time of @config/settings.yml@
configSettingsYmlBS :: ByteString configSettingsYmlBS :: ByteString
configSettingsYmlBS = $(embedFile configSettingsYml) configSettingsYmlBS = $(embedFile configSettingsYml)
@ -269,6 +282,7 @@ compileTimeAppSettings =
case fromJSON $ applyEnvValue False mempty configSettingsYmlValue of case fromJSON $ applyEnvValue False mempty configSettingsYmlValue of
Error e -> error e Error e -> error e
Success settings -> settings Success settings -> settings
-}
-- The following two functions can be used to combine multiple CSS or JS files -- The following two functions can be used to combine multiple CSS or JS files
-- at compile time to decrease the number of http requests. -- at compile time to decrease the number of http requests.
@ -279,11 +293,11 @@ compileTimeAppSettings =
combineStylesheets :: Name -> [Route Static] -> Q Exp combineStylesheets :: Name -> [Route Static] -> Q Exp
combineStylesheets = combineStylesheets =
combineStylesheets' combineStylesheets'
(appSkipCombining compileTimeAppSettings) appSkipCombining
combineSettings combineSettings
combineScripts :: Name -> [Route Static] -> Q Exp combineScripts :: Name -> [Route Static] -> Q Exp
combineScripts = combineScripts =
combineScripts' combineScripts'
(appSkipCombining compileTimeAppSettings) appSkipCombining
combineSettings combineSettings

View file

@ -15,7 +15,7 @@
module Vervis.Settings.StaticFiles where module Vervis.Settings.StaticFiles where
import Vervis.Settings (appStaticDir, compileTimeAppSettings) import Vervis.Settings (appStaticDir)
import Yesod.Static (staticFiles) import Yesod.Static (staticFiles)
-- This generates easy references to files in the static directory at compile time, -- 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: -- If the identifier is not available, you may use:
-- --
-- StaticFile ["js", "script.js"] [] -- StaticFile ["js", "script.js"] []
staticFiles (appStaticDir compileTimeAppSettings) staticFiles appStaticDir