module Settings where
import ClassyPrelude.Yesod
import qualified Control.Exception as Exception
import Data.Aeson (Result (..), fromJSON, withObject, (.!=),
(.:?))
import Data.FileEmbed (embedFile)
import Data.Yaml (decodeEither')
import Database.Persist.Sqlite (SqliteConf)
import Language.Haskell.TH.Syntax (Exp, Name, Q)
import Network.Wai.Handler.Warp (HostPreference)
import Yesod.Default.Config2 (applyEnvValue, configSettingsYml)
import Yesod.Default.Util (WidgetFileSettings, widgetFileNoReload,
widgetFileReload)
data AppSettings = AppSettings
{ AppSettings -> String
appStaticDir :: String
, AppSettings -> SqliteConf
appDatabaseConf :: SqliteConf
, AppSettings -> Maybe Text
appRoot :: Maybe Text
, AppSettings -> HostPreference
appHost :: HostPreference
, AppSettings -> Int
appPort :: Int
, :: Bool
, AppSettings -> Bool
appDetailedRequestLogging :: Bool
, AppSettings -> Bool
appShouldLogAll :: Bool
, AppSettings -> Bool
appReloadTemplates :: Bool
, AppSettings -> Bool
appMutableStatic :: Bool
, AppSettings -> Bool
appSkipCombining :: Bool
, AppSettings -> Text
appCopyright :: Text
, AppSettings -> Maybe Text
appAnalytics :: Maybe Text
, AppSettings -> Bool
appAuthDummyLogin :: Bool
, AppSettings -> Maybe Text
appArchiveSocksProxyHost :: Maybe Text
, AppSettings -> Maybe Int
appArchiveSocksProxyPort :: Maybe Int
, AppSettings -> Maybe Text
appSourceCodeUri :: Maybe Text
, AppSettings -> Bool
appSSLOnly :: Bool
, AppSettings -> Bool
appAllowNonHttpUrlSchemes :: Bool
}
instance FromJSON AppSettings where
parseJSON :: Value -> Parser AppSettings
parseJSON = forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"AppSettings" \Object
o -> do
let defaultDev :: Bool
defaultDev =
#ifdef DEVELOPMENT
True
#else
Bool
False
#endif
String
appStaticDir <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"static-dir"
SqliteConf
appDatabaseConf <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"database"
Maybe Text
appRoot <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"approot"
HostPreference
appHost <- forall a. IsString a => String -> a
fromString forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"host"
Int
appPort <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"port"
Bool
appIpFromHeader <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"ip-from-header"
Bool
dev <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"development" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
defaultDev
Bool
appDetailedRequestLogging <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"detailed-logging" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appShouldLogAll <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"should-log-all" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appReloadTemplates <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"reload-templates" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appMutableStatic <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"mutable-static" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appSkipCombining <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"skip-combining" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Text
appCopyright <- Object
o forall a. FromJSON a => Object -> Key -> Parser a
.: Key
"copyright"
Maybe Text
appAnalytics <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"analytics"
Bool
appAuthDummyLogin <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"auth-dummy-login" forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Maybe Text
appArchiveSocksProxyHost <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"archive-socks-proxy-host"
Maybe Int
appArchiveSocksProxyPort <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"archive-socks-proxy-port"
Maybe Text
appSourceCodeUri <- Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"source-code-uri"
Bool
appSSLOnly <- forall a. a -> Maybe a -> a
fromMaybe Bool
False forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"ssl-only"
Bool
appAllowNonHttpUrlSchemes <- forall a. a -> Maybe a -> a
fromMaybe Bool
False forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
.:? Key
"allow-non-http-url-schemes"
forall (m :: * -> *) a. Monad m => a -> m a
return AppSettings {Bool
Int
String
Maybe Int
Maybe Text
Text
HostPreference
SqliteConf
appAllowNonHttpUrlSchemes :: Bool
appSSLOnly :: Bool
appSourceCodeUri :: Maybe Text
appArchiveSocksProxyPort :: Maybe Int
appArchiveSocksProxyHost :: Maybe Text
appAuthDummyLogin :: Bool
appAnalytics :: Maybe Text
appCopyright :: Text
appSkipCombining :: Bool
appMutableStatic :: Bool
appReloadTemplates :: Bool
appShouldLogAll :: Bool
appDetailedRequestLogging :: Bool
appIpFromHeader :: Bool
appPort :: Int
appHost :: HostPreference
appRoot :: Maybe Text
appDatabaseConf :: SqliteConf
appStaticDir :: String
appAllowNonHttpUrlSchemes :: Bool
appSSLOnly :: Bool
appSourceCodeUri :: Maybe Text
appArchiveSocksProxyPort :: Maybe Int
appArchiveSocksProxyHost :: Maybe Text
appAuthDummyLogin :: Bool
appAnalytics :: Maybe Text
appCopyright :: Text
appSkipCombining :: Bool
appMutableStatic :: Bool
appReloadTemplates :: Bool
appShouldLogAll :: Bool
appDetailedRequestLogging :: Bool
appIpFromHeader :: Bool
appPort :: Int
appHost :: HostPreference
appRoot :: Maybe Text
appDatabaseConf :: SqliteConf
appStaticDir :: String
..}
widgetFileSettings :: WidgetFileSettings
widgetFileSettings :: WidgetFileSettings
widgetFileSettings = forall a. Default a => a
def
combineSettings :: CombineSettings
combineSettings :: CombineSettings
combineSettings = forall a. Default a => a
def
widgetFile :: String -> Q Exp
widgetFile :: String -> Q Exp
widgetFile = (if AppSettings -> Bool
appReloadTemplates AppSettings
compileTimeAppSettings
then WidgetFileSettings -> String -> Q Exp
widgetFileReload
else WidgetFileSettings -> String -> Q Exp
widgetFileNoReload)
WidgetFileSettings
widgetFileSettings
configSettingsYmlBS :: ByteString
configSettingsYmlBS :: ByteString
configSettingsYmlBS = $(embedFile configSettingsYml)
configSettingsYmlValue :: Value
configSettingsYmlValue :: Value
configSettingsYmlValue = forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either forall a e. Exception e => e -> a
Exception.throw forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id
forall a b. (a -> b) -> a -> b
$ forall a. FromJSON a => ByteString -> Either ParseException a
decodeEither' ByteString
configSettingsYmlBS
compileTimeAppSettings :: AppSettings
compileTimeAppSettings :: AppSettings
compileTimeAppSettings =
case forall a. FromJSON a => Value -> Result a
fromJSON forall a b. (a -> b) -> a -> b
$ Bool -> KeyMap Text -> Value -> Value
applyEnvValue Bool
False forall a. Monoid a => a
mempty Value
configSettingsYmlValue of
Error String
e -> forall a. HasCallStack => String -> a
error String
e
Success AppSettings
settings -> AppSettings
settings
combineStylesheets :: Name -> [Route Static] -> Q Exp
combineStylesheets :: Name -> [Route Static] -> Q Exp
combineStylesheets = Bool -> CombineSettings -> Name -> [Route Static] -> Q Exp
combineStylesheets'
(AppSettings -> Bool
appSkipCombining AppSettings
compileTimeAppSettings)
CombineSettings
combineSettings
combineScripts :: Name -> [Route Static] -> Q Exp
combineScripts :: Name -> [Route Static] -> Q Exp
combineScripts = Bool -> CombineSettings -> Name -> [Route Static] -> Q Exp
combineScripts'
(AppSettings -> Bool
appSkipCombining AppSettings
compileTimeAppSettings)
CombineSettings
combineSettings