| 1 |
{-| |
| 2 |
Complete the definition of the web app begun in App.hs. |
| 3 |
This is always done in two files for (TH?) reasons. |
| 4 |
-} |
| 5 |
|
| 6 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 7 |
{-# LANGUAGE OverloadedStrings #-} |
| 8 |
{-# LANGUAGE TemplateHaskell #-} |
| 9 |
{-# LANGUAGE ViewPatterns #-} |
| 10 |
|
| 11 |
module Hledger.Web.Application |
| 12 |
( makeApplication |
| 13 |
, makeApp |
| 14 |
, makeAppWith |
| 15 |
) where |
| 16 |
|
| 17 |
import Data.IORef (newIORef, writeIORef) |
| 18 |
import Network.Wai.Middleware.RequestLogger (logStdoutDev, logStdout) |
| 19 |
import Network.HTTP.Client (defaultManagerSettings) |
| 20 |
import Network.HTTP.Conduit (newManager) |
| 21 |
import Yesod.Default.Config |
| 22 |
|
| 23 |
import Hledger.Data (Journal, nulljournal) |
| 24 |
|
| 25 |
import Hledger.Web.Handler.AddR |
| 26 |
import Hledger.Web.Handler.MiscR |
| 27 |
import Hledger.Web.Handler.EditR |
| 28 |
import Hledger.Web.Handler.UploadR |
| 29 |
import Hledger.Web.Handler.JournalR |
| 30 |
import Hledger.Web.Handler.RegisterR |
| 31 |
import Hledger.Web.Import |
| 32 |
import Hledger.Web.WebOptions (ServerMode(..), WebOpts(server_mode_), corsPolicy) |
| 33 |
|
| 34 |
-- mkYesodDispatch creates our YesodDispatch instance. |
| 35 |
-- It complements the mkYesodData call in App.hs, |
| 36 |
-- but must be in a separate file for (TH?) reasons. |
| 37 |
mkYesodDispatch "App" resourcesApp |
| 38 |
|
| 39 |
-- This function allocates resources (such as a database connection pool), |
| 40 |
-- performs initialization and creates a WAI application. This is also the |
| 41 |
-- place to put your migrate statements to have automatic database |
| 42 |
-- migrations handled by Yesod. |
| 43 |
makeApplication :: WebOpts -> Journal -> AppConfig DefaultEnv Extra -> IO Application |
| 44 |
makeApplication opts' j' conf' = do |
| 45 |
app <- makeApp conf' opts' |
| 46 |
writeIORef (appJournal app) j' |
| 47 |
(logWare . (corsPolicy opts')) <$> toWaiApp app |
| 48 |
where |
| 49 |
logWare | development = logStdoutDev |
| 50 |
| server_mode_ opts' `elem` [Serve, ServeJson] = logStdout |
| 51 |
| otherwise = id |
| 52 |
|
| 53 |
makeApp :: AppConfig DefaultEnv Extra -> WebOpts -> IO App |
| 54 |
makeApp = makeAppWith nulljournal |
| 55 |
|
| 56 |
-- Make an "App" (defined in App.hs), |
| 57 |
-- with the given Journal as its state |
| 58 |
-- and the given "AppConfig" and "WebOpts" as its configuration. |
| 59 |
makeAppWith :: Journal -> AppConfig DefaultEnv Extra -> WebOpts -> IO App |
| 60 |
makeAppWith j' aconf wopts = do |
| 61 |
s <- staticSite |
| 62 |
m <- newManager defaultManagerSettings |
| 63 |
jref <- newIORef j' |
| 64 |
return App{ |
| 65 |
settings = aconf |
| 66 |
, getStatic = s |
| 67 |
, httpManager = m |
| 68 |
, appOpts = wopts |
| 69 |
, appJournal = jref |
| 70 |
} |