summaryrefslogtreecommitdiff
path: root/Foundation.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 /Foundation.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 'Foundation.hs')
-rw-r--r--Foundation.hs141
1 files changed, 141 insertions, 0 deletions
diff --git a/Foundation.hs b/Foundation.hs
new file mode 100644
index 0000000..85512a3
--- /dev/null
+++ b/Foundation.hs
@@ -0,0 +1,141 @@
1module Foundation where
2
3import Import.NoFoundation
4import Database.Persist.Sql (ConnectionPool, runSqlPool)
5import Text.Hamlet (hamletFile)
6
7import Yesod.Core.Types (Logger)
8import qualified Yesod.Core.Unsafe as Unsafe
9
10-- | The foundation datatype for your application. This can be a good place to
11-- keep settings and values requiring initialization before your application
12-- starts running, such as database connections. Every handler will have
13-- access to the data present here.
14data App = App
15 { appSettings :: AppSettings
16 , appStatic :: EmbeddedStatic
17 , appConnPool :: ConnectionPool -- ^ Database connection pool.
18 , appHttpManager :: Manager
19 , appLogger :: Logger
20 }
21
22data MenuItem = MenuItem
23 { menuItemLabel :: Text
24 , menuItemRoute :: Route App
25 }
26
27-- This is where we define all of the routes in our application. For a full
28-- explanation of the syntax, please see:
29-- http://www.yesodweb.com/book/routing-and-handlers
30--
31-- Note that this is really half the story; in Application.hs, mkYesodDispatch
32-- generates the rest of the code. Please see the following documentation
33-- for an explanation for this split:
34-- http://www.yesodweb.com/book/scaffolding-and-the-site-template#scaffolding-and-the-site-template_foundation_and_application_modules
35--
36-- This function also generates the following type synonyms:
37-- type Handler = HandlerT App IO
38-- type Widget = WidgetT App IO ()
39mkYesodData "App" $(parseRoutesFile "config/routes")
40
41-- | A convenient synonym for creating forms.
42type Form x = Html -> MForm (HandlerT App IO) (FormResult x, Widget)
43
44-- Please see the documentation for the Yesod typeclass. There are a number
45-- of settings which can be configured by overriding methods here.
46instance Yesod App where
47 -- Controls the base of generated URLs. For more information on modifying,
48 -- see: https://github.com/yesodweb/yesod/wiki/Overriding-approot
49 approot = ApprootRequest $ \app req ->
50 case appRoot $ appSettings app of
51 Nothing -> getApprootText guessApproot app req
52 Just root -> root
53
54 -- Store session data on the client in encrypted cookies,
55 -- default session idle timeout is 120 minutes
56 makeSessionBackend _ = Just <$> defaultClientSessionBackend
57 120 -- timeout in minutes
58 "client_session_key.aes"
59
60 -- Yesod Middleware allows you to run code before and after each handler function.
61
62 -- The defaultYesodMiddleware adds the response header "Vary: Accept, Accept-Language" and performs authorization checks.
63 -- Some users may also want to add the defaultCsrfMiddleware, which:
64 -- a) Sets a cookie with a CSRF token in it.
65 -- b) Validates that incoming write requests include that token in either a header or POST parameter.
66 -- To add it, chain it together with the defaultMiddleware: yesodMiddleware = defaultYesodMiddleware . defaultCsrfMiddleware
67 -- For details, see the CSRF documentation in the Yesod.Core.Handler module of the yesod-core package.
68 yesodMiddleware = defaultYesodMiddleware
69
70 defaultLayout widget = do
71 msgs <- getMessages
72
73 mCurrentRoute <- getCurrentRoute
74
75 -- Define the menu items of the header.
76 let menuItems =
77 [ MenuItem "Inventory" InventoryListingR
78 ]
79
80 -- We break up the default layout into two components:
81 -- default-layout is the contents of the body tag, and
82 -- default-layout-wrapper is the entire page. Since the final
83 -- value passed to hamletToRepHtml cannot be a widget, this allows
84 -- you to use normal widget features in default-layout.
85
86 pc <- widgetToPageContent $ do
87 addScript $ StaticR jquery_js
88 addScript $ StaticR webshim_polyfiller_js
89 $(widgetFile "default-layout")
90 withUrlRenderer $(hamletFile "templates/default-layout-wrapper.hamlet")
91
92
93 -- This function creates static content files in the static folder
94 -- and names them based on a hash of their content. This allows
95 -- expiration dates to be set far in the future without worry of
96 -- users receiving stale content.
97 addStaticContent = embedStaticContent appStatic StaticR Right
98
99 -- What messages should be logged. The following includes all messages when
100 -- in development, and warnings and errors in production.
101 shouldLog app _source level =
102 appShouldLogAll (appSettings app)
103 || level == LevelWarn
104 || level == LevelError
105
106 makeLogger = return . appLogger
107
108 -- Provide proper Bootstrap styling for default displays, like
109 -- error pages
110 defaultMessageWidget title body = $(widgetFile "default-message-widget")
111
112-- How to run database actions.
113instance YesodPersist App where
114 type YesodPersistBackend App = SqlBackend
115 runDB action = do
116 master <- getYesod
117 runSqlPool action $ appConnPool master
118instance YesodPersistRunner App where
119 getDBRunner = defaultGetDBRunner appConnPool
120
121-- This instance is required to use forms. You can modify renderMessage to
122-- achieve customized and internationalized form validation messages.
123instance RenderMessage App FormMessage where
124 renderMessage _ _ = defaultFormMessage
125
126-- Useful when writing code that is re-usable outside of the Handler context.
127-- An example is background jobs that send email.
128-- This can also be useful for writing code that works across multiple Yesod applications.
129instance HasHttpManager App where
130 getHttpManager = appHttpManager
131
132unsafeHandler :: App -> Handler a -> IO a
133unsafeHandler = Unsafe.fakeHandlerGetLogger appLogger
134
135-- Note: Some functionality previously present in the scaffolding has been
136-- moved to documentation in the Wiki. Following are some hopefully helpful
137-- links:
138--
139-- https://github.com/yesodweb/yesod/wiki/Sending-email
140-- https://github.com/yesodweb/yesod/wiki/Serve-static-files-from-a-separate-domain
141-- https://github.com/yesodweb/yesod/wiki/i18n-messages-in-the-scaffolding