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)
|
2013-12-09 22:06:53 +04:00
|
|
|
import Network.HTTP.Conduit (newManager)
|
|
|
|
|
|
|
|
-- adapt to http-conduit 1.x or 2.x when cabal macros are available, otherwise assume 2.x
|
|
|
|
#ifdef MIN_VERSION_http_conduit
|
|
|
|
#if MIN_VERSION_http_conduit(2,0,0)
|
|
|
|
#define http_conduit_2
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#define http_conduit_2
|
|
|
|
#endif
|
|
|
|
#ifdef http_conduit_2
|
|
|
|
import Network.HTTP.Client (defaultManagerSettings)
|
|
|
|
#else
|
|
|
|
import Network.HTTP.Conduit (def)
|
|
|
|
#endif
|
2012-11-15 21:48:48 +04:00
|
|
|
|
|
|
|
-- 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.RegisterR
|
2014-06-13 03:14:41 +04:00
|
|
|
import Handler.SidebarR
|
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
|
2013-12-09 22:06:53 +04:00
|
|
|
manager <- newManager
|
|
|
|
#ifdef http_conduit_2
|
|
|
|
defaultManagerSettings
|
|
|
|
#else
|
|
|
|
def
|
|
|
|
#endif
|
2012-11-15 21:48:48 +04:00
|
|
|
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
|
2014-07-03 20:37:02 +04:00
|
|
|
j <- either error' id `fmap` readJournalFile Nothing Nothing True f
|
2013-05-01 18:23:59 +04:00
|
|
|
defaultDevelApp loader (makeApplication defwebopts j)
|
2012-11-15 21:48:48 +04:00
|
|
|
where
|
2013-05-15 02:36:44 +04:00
|
|
|
loader = Yesod.Default.Config.loadConfig (configSettings Development)
|
2012-11-15 21:48:48 +04:00
|
|
|
{ csParseExtra = parseExtra
|
|
|
|
}
|