Debian Codegraph @

Hledger/Web/Settings.hs

raw
1 {-|
2 Web app settings are centralized, as much as possible, into this file.
3 This 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 in App.hs.
6 -}
7
8 {-# LANGUAGE CPP #-}
9 {-# LANGUAGE OverloadedStrings #-}
10
11 module Hledger.Web.Settings where
12
13 import Data.Default (def)
14 import Data.Maybe (fromMaybe)
15 import Data.Text (Text)
16 import Data.Text qualified as T
17 import Data.Yaml
18 import Language.Haskell.TH.Syntax (Q, Exp)
19 import Text.Hamlet
20 import Yesod.Default.Config
21 import Yesod.Default.Util
22
23 import Hledger.Cli.Version (packagemajorversion)
24
25 development :: Bool
26 development =
27 #if DEVELOPMENT
28 True
29 #else
30 False
31 #endif
32
33 production :: Bool
34 production = not development
35
36 hledgerorgurl :: Text
37 hledgerorgurl = "https://hledger.org"
38
39 manualurl :: Text
40 manualurl = hledgerorgurl <> "/" <> T.pack packagemajorversion <> "/hledger.html"
41
42 -- | The default IP address to listen on. May be overridden with --host.
43 defhost :: String
44 defhost = "127.0.0.1"
45
46 -- | The default TCP port to listen on. May be overridden with --port.
47 defport :: Int
48 defport = 5000
49
50 defbaseurl :: String -> Int -> String
51 defbaseurl host port =
52 if ':' `elem` host
53 then -- ipv6 address
54 "http://[" ++ host ++ "]" ++ if port /= 80 then ":" ++ show port else ""
55 else
56 "http://" ++ host ++ if port /= 80 then ":" ++ show port else ""
57
58 -- Static file settings. Changing these requires a recompile.
59
60 -- | The file path on your machine where static files can be found.
61 -- StaticFiles.hs uses this (must be separate for TH reasons).
62 staticDir :: FilePath
63 staticDir = "static"
64
65 -- | The base URL for static files. As you can see by the default
66 -- value, this can simply be "static" appended to your application root.
67 -- A powerful optimization can be serving static files from a separate
68 -- domain name. This allows you to use a web server optimized for static
69 -- files, more easily set expires and cache values, and avoid possibly
70 -- costly transference of cookies on static files.
71 --
72 -- If you change the resource pattern for StaticR in App.hs,
73 -- (or staticDir above), you will have to make a corresponding change here.
74 --
75 -- To see how this value is used, see urlRenderOverride in App.hs
76 --
77 -- XXX Does not respect --file-url #2139
78 staticRoot :: AppConfig DefaultEnv Extra -> Text
79 staticRoot conf = fromMaybe (appRoot conf <> "/static") . extraStaticRoot $ appExtra conf
80
81 -- | Settings for 'widgetFile', such as which template languages to support and
82 -- default Hamlet settings.
83 widgetFileSettings :: WidgetFileSettings
84 widgetFileSettings = def
85 { wfsHamletSettings = defaultHamletSettings
86 { hamletNewlines = AlwaysNewlines
87 }
88 }
89
90 -- The rest of this file contains settings which rarely need changing by a
91 -- user.
92
93 widgetFile :: String -> Q Exp
94 widgetFile = (if development then widgetFileReload
95 else widgetFileNoReload)
96 widgetFileSettings
97
98 data Extra = Extra
99 { extraCopyright :: Text
100 , extraAnalytics :: Maybe Text -- ^ Google Analytics
101 , extraStaticRoot :: Maybe Text
102 } deriving Show
103
104 parseExtra :: DefaultEnv -> Object -> Parser Extra
105 parseExtra _ o = Extra
106 <$> o .: "copyright"
107 <*> o .:? "analytics"
108 <*> o .:? "staticRoot"