{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE NoImplicitPrelude #-}
module BtcLsp.Yesod.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 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 -> 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
}
instance FromJSON AppSettings where
parseJSON :: Value -> Parser AppSettings
parseJSON = String
-> (Object -> Parser AppSettings) -> Value -> Parser AppSettings
forall a. String -> (Object -> Parser a) -> Value -> Parser a
withObject String
"AppSettings" ((Object -> Parser AppSettings) -> Value -> Parser AppSettings)
-> (Object -> Parser AppSettings) -> Value -> Parser AppSettings
forall a b. (a -> b) -> a -> b
$ \Object
o -> do
let defaultDev :: Bool
defaultDev = Bool
False
String
appStaticDir <- Object
o Object -> Text -> Parser String
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"static-dir"
Maybe Text
appRoot <- Object
o Object -> Text -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"approot"
HostPreference
appHost <- String -> HostPreference
forall a. IsString a => String -> a
fromString (String -> HostPreference)
-> Parser String -> Parser HostPreference
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Object
o Object -> Text -> Parser String
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"host"
Int
appPort <- Object
o Object -> Text -> Parser Int
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"port"
Bool
appIpFromHeader <- Object
o Object -> Text -> Parser Bool
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"ip-from-header"
Bool
dev <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"development" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
defaultDev
Bool
appDetailedRequestLogging <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"detailed-logging" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appShouldLogAll <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"should-log-all" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appReloadTemplates <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"reload-templates" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appMutableStatic <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"mutable-static" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Bool
appSkipCombining <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"skip-combining" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
Text
appCopyright <- Object
o Object -> Text -> Parser Text
forall a. FromJSON a => Object -> Text -> Parser a
.: Text
"copyright"
Maybe Text
appAnalytics <- Object
o Object -> Text -> Parser (Maybe Text)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"analytics"
Bool
appAuthDummyLogin <- Object
o Object -> Text -> Parser (Maybe Bool)
forall a. FromJSON a => Object -> Text -> Parser (Maybe a)
.:? Text
"auth-dummy-login" Parser (Maybe Bool) -> Bool -> Parser Bool
forall a. Parser (Maybe a) -> a -> Parser a
.!= Bool
dev
AppSettings -> Parser AppSettings
forall (m :: * -> *) a. Monad m => a -> m a
return AppSettings :: String
-> Maybe Text
-> HostPreference
-> Int
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Bool
-> Text
-> Maybe Text
-> Bool
-> AppSettings
AppSettings {Bool
Int
String
Maybe Text
Text
HostPreference
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
appStaticDir :: String
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
appStaticDir :: String
..}
widgetFileSettings :: WidgetFileSettings
widgetFileSettings :: WidgetFileSettings
widgetFileSettings = WidgetFileSettings
forall a. Default a => a
def
combineSettings :: 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 =
(ParseException -> Value)
-> (Value -> Value) -> Either ParseException Value -> Value
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either ParseException -> Value
forall a e. Exception e => e -> a
Exception.throw Value -> Value
forall {k} (cat :: k -> k -> *) (a :: k). Category cat => cat a a
id (Either ParseException Value -> Value)
-> Either ParseException Value -> Value
forall a b. (a -> b) -> a -> b
$
ByteString -> Either ParseException Value
forall a. FromJSON a => ByteString -> Either ParseException a
decodeEither' ByteString
configSettingsYmlBS
compileTimeAppSettings :: AppSettings
compileTimeAppSettings :: AppSettings
compileTimeAppSettings =
case Value -> Result AppSettings
forall a. FromJSON a => Value -> Result a
fromJSON (Value -> Result AppSettings) -> Value -> Result AppSettings
forall a b. (a -> b) -> a -> b
$ Bool -> KeyMap Text -> Value -> Value
applyEnvValue Bool
False KeyMap Text
forall a. Monoid a => a
mempty Value
configSettingsYmlValue of
Error String
e -> String -> AppSettings
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