2012-11-15 21:48:48 +04:00
|
|
|
{-# OPTIONS_GHC -fno-warn-orphans #-}
|
|
|
|
module Application
|
|
|
|
( makeApplication
|
|
|
|
, getApplicationDev
|
|
|
|
, makeFoundation
|
|
|
|
) where
|
|
|
|
|
2013-04-09 20:33:19 +04:00
|
|
|
import Data.IORef
|
2012-11-15 21:48:48 +04:00
|
|
|
import Import
|
|
|
|
import Yesod.Default.Config
|
|
|
|
import Yesod.Default.Main
|
|
|
|
import Yesod.Default.Handlers
|
2013-05-01 18:23:59 +04:00
|
|
|
import Network.Wai.Middleware.RequestLogger (logStdoutDev, logStdout)
|
2012-11-15 21:48:48 +04:00
|
|
|
import Network.HTTP.Conduit (newManager, def)
|
|
|
|
|
|
|
|
-- Import all relevant handler modules here.
|
|
|
|
-- Don't forget to add new modules to your cabal file!
|
2012-11-20 06:23:23 +04:00
|
|
|
import Handler.RootR
|
|
|
|
import Handler.JournalR
|
|
|
|
import Handler.JournalEditR
|
|
|
|
import Handler.JournalEntriesR
|
|
|
|
import Handler.RegisterR
|
2012-11-15 21:48:48 +04:00
|
|
|
|
2013-05-01 18:23:59 +04:00
|
|
|
import Hledger.Web.Options (WebOpts(..), defwebopts)
|
2013-04-09 20:33:19 +04:00
|
|
|
import Hledger.Data (Journal, nulljournal)
|
|
|
|
import Hledger.Read (readJournalFile)
|
|
|
|
import Hledger.Utils (error')
|
|
|
|
import Hledger.Cli.Options (defcliopts, journalFilePathFromOpts)
|
2012-11-15 21:48:48 +04:00
|
|
|
|
|
|
|
-- This line actually creates our YesodDispatch instance. It is the second half
|
|
|
|
-- of the call to mkYesodData which occurs in Foundation.hs. Please see the
|
|
|
|
-- comments there for more details.
|
|
|
|
mkYesodDispatch "App" resourcesApp
|
|
|
|
|
|
|
|
-- This function allocates resources (such as a database connection pool),
|
|
|
|
-- performs initialization and creates a WAI application. This is also the
|
|
|
|
-- place to put your migrate statements to have automatic database
|
|
|
|
-- migrations handled by Yesod.
|
2013-05-01 18:23:59 +04:00
|
|
|
makeApplication :: WebOpts -> Journal -> AppConfig DefaultEnv Extra -> IO Application
|
|
|
|
makeApplication opts j conf = do
|
2012-11-15 21:48:48 +04:00
|
|
|
foundation <- makeFoundation conf
|
2013-04-09 20:33:19 +04:00
|
|
|
writeIORef (appJournal foundation) j
|
2012-11-15 21:48:48 +04:00
|
|
|
app <- toWaiAppPlain foundation
|
|
|
|
return $ logWare app
|
|
|
|
where
|
2013-05-01 18:23:59 +04:00
|
|
|
logWare | development = logStdoutDev
|
|
|
|
| server_ opts = logStdout
|
|
|
|
| otherwise = id
|
2012-11-15 21:48:48 +04:00
|
|
|
|
|
|
|
makeFoundation :: AppConfig DefaultEnv Extra -> IO App
|
|
|
|
makeFoundation conf = do
|
|
|
|
manager <- newManager def
|
|
|
|
s <- staticSite
|
2013-04-09 20:33:19 +04:00
|
|
|
jref <- newIORef nulljournal
|
|
|
|
return $ App conf s manager defwebopts jref
|
2012-11-15 21:48:48 +04:00
|
|
|
|
|
|
|
-- for yesod devel
|
2013-04-09 20:33:19 +04:00
|
|
|
-- uses the journal specified by the LEDGER_FILE env var, or ~/.hledger.journal
|
2012-11-15 21:48:48 +04:00
|
|
|
getApplicationDev :: IO (Int, Application)
|
2013-04-09 20:33:19 +04:00
|
|
|
getApplicationDev = do
|
|
|
|
f <- journalFilePathFromOpts defcliopts
|
|
|
|
j <- either error' id `fmap` readJournalFile Nothing Nothing f
|
2013-05-01 18:23:59 +04:00
|
|
|
defaultDevelApp loader (makeApplication defwebopts j)
|
2012-11-15 21:48:48 +04:00
|
|
|
where
|
|
|
|
loader = loadConfig (configSettings Development)
|
|
|
|
{ csParseExtra = parseExtra
|
|
|
|
}
|