summaryrefslogtreecommitdiff
path: root/Settings.hs
diff options
context:
space:
mode:
authorGregor Kleen <gkleen@yggdrasil.li>2017-03-14 01:06:28 +0100
committerGregor Kleen <gkleen@yggdrasil.li>2017-03-14 01:06:28 +0100
commitd84b462a711ce95593ff05a7581e722562c3835a (patch)
tree41e5af455fea925b2680b29718b24ba2876e803a /Settings.hs
downloadbar-d84b462a711ce95593ff05a7581e722562c3835a.tar
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.gz
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.bz2
bar-d84b462a711ce95593ff05a7581e722562c3835a.tar.xz
bar-d84b462a711ce95593ff05a7581e722562c3835a.zip
Implement old bar.hs
Diffstat (limited to 'Settings.hs')
-rw-r--r--Settings.hs107
1 files changed, 107 insertions, 0 deletions
diff --git a/Settings.hs b/Settings.hs
new file mode 100644
index 0000000..76aa2f3
--- /dev/null
+++ b/Settings.hs
@@ -0,0 +1,107 @@
1{-# Language CPP #-}
2-- | Settings are centralized, as much as possible, into this file. This
3-- includes database connection settings, static file locations, etc.
4-- In addition, you can configure a number of different aspects of Yesod
5-- by overriding methods in the Yesod typeclass. That instance is
6-- declared in the Foundation.hs file.
7module Settings where
8
9import ClassyPrelude.Yesod
10import qualified Control.Exception as Exception
11import Data.Aeson (Result (..), fromJSON, withObject, (.!=),
12 (.:?))
13import Data.FileEmbed (embedFile)
14import Data.Yaml (decodeEither')
15import Database.Persist.Postgresql (PostgresConf)
16import Language.Haskell.TH.Syntax (Exp, Q)
17import Network.Wai.Handler.Warp (HostPreference)
18import Yesod.Default.Config2 (applyEnvValue, configSettingsYml)
19import Yesod.Default.Util (WidgetFileSettings, widgetFileNoReload,
20 widgetFileReload)
21
22#ifdef DEVELOPMENT
23#define DEV_BOOL True
24#else
25#define DEV_BOOL False
26#endif
27
28-- | Runtime settings to configure this application. These settings can be
29-- loaded from various sources: defaults, environment variables, config files,
30-- theoretically even a database.
31data AppSettings = AppSettings
32 { appStaticDir :: String
33 -- ^ Directory from which to serve static files.
34 , appDatabaseConf :: PostgresConf
35 -- ^ Configuration settings for accessing the database.
36 , appRoot :: Maybe Text
37 -- ^ Base for all generated URLs. If @Nothing@, determined
38 -- from the request headers.
39 , appHost :: HostPreference
40 -- ^ Host/interface the server should bind to.
41 , appPort :: Int
42 -- ^ Port to listen on
43 , appIpFromHeader :: Bool
44 -- ^ Get the IP address from the header when logging. Useful when sitting
45 -- behind a reverse proxy.
46
47 , appDetailedRequestLogging :: Bool
48 -- ^ Use detailed request logging system
49 , appShouldLogAll :: Bool
50 -- ^ Should all log messages be displayed?
51 , appReloadTemplates :: Bool
52 -- ^ Use the reload version of templates
53 }
54
55instance FromJSON AppSettings where
56 parseJSON = withObject "AppSettings" $ \o -> do
57 let defaultDev = DEV_BOOL
58 appStaticDir <- o .: "static-dir"
59 appDatabaseConf <- o .: "database"
60 appRoot <- o .:? "approot"
61 appHost <- fromString <$> o .: "host"
62 appPort <- o .: "port"
63 appIpFromHeader <- o .: "ip-from-header"
64
65 appDetailedRequestLogging <- o .:? "detailed-logging" .!= defaultDev
66 appShouldLogAll <- o .:? "should-log-all" .!= defaultDev
67 appReloadTemplates <- o .:? "reload-templates" .!= defaultDev
68
69 return AppSettings {..}
70
71-- | Settings for 'widgetFile', such as which template languages to support and
72-- default Hamlet settings.
73--
74-- For more information on modifying behavior, see:
75--
76-- https://github.com/yesodweb/yesod/wiki/Overriding-widgetFile
77widgetFileSettings :: WidgetFileSettings
78widgetFileSettings = def
79
80-- | How static files should be combined.
81combineSettings :: CombineSettings
82combineSettings = def
83
84-- The rest of this file contains settings which rarely need changing by a
85-- user.
86
87widgetFile :: String -> Q Exp
88widgetFile = (if appReloadTemplates compileTimeAppSettings
89 then widgetFileReload
90 else widgetFileNoReload)
91 widgetFileSettings
92
93-- | Raw bytes at compile time of @config/settings.yml@
94configSettingsYmlBS :: ByteString
95configSettingsYmlBS = $(embedFile configSettingsYml)
96
97-- | @config/settings.yml@, parsed to a @Value@.
98configSettingsYmlValue :: Value
99configSettingsYmlValue = either Exception.throw id
100 $ decodeEither' configSettingsYmlBS
101
102-- | A version of @AppSettings@ parsed at compile time from @config/settings.yml@.
103compileTimeAppSettings :: AppSettings
104compileTimeAppSettings =
105 case fromJSON $ applyEnvValue False mempty configSettingsYmlValue of
106 Error e -> error e
107 Success settings -> settings