Less inversion of control

This commit is contained in:
Michael Snoyman 2012-01-12 09:52:06 +02:00
parent 352f577de6
commit bd6671b264
10 changed files with 64 additions and 56 deletions

3
.gitmodules vendored
View File

@ -4,3 +4,6 @@
[submodule "authenticate"] [submodule "authenticate"]
path = authenticate path = authenticate
url = https://github.com/yesodweb/authenticate.git url = https://github.com/yesodweb/authenticate.git
[submodule "http-conduit"]
path = http-conduit
url = https://github.com/snoyberg/http-conduit.git

1
http-conduit Submodule

@ -0,0 +1 @@
Subproject commit 7d67c7d78b2ba29639554d9d69d6b00dd98367fc

View File

@ -7,6 +7,7 @@ pkgs=( ./yesod-routes
./yesod-persistent ./yesod-persistent
./yesod-newsfeed ./yesod-newsfeed
./yesod-form ./yesod-form
./http-conduit
./authenticate ./authenticate
./yesod-auth ./yesod-auth
./yesod-sitemap ./yesod-sitemap

View File

@ -4,10 +4,8 @@ module Yesod.Default.Main
( defaultMain ( defaultMain
, defaultRunner , defaultRunner
, defaultDevelApp , defaultDevelApp
, defaultDevelAppWith
) where ) where
import Yesod.Core
import Yesod.Default.Config import Yesod.Default.Config
import Yesod.Logger (Logger, makeDefaultLogger, logString, flushLogger) import Yesod.Logger (Logger, makeDefaultLogger, logString, flushLogger)
import Network.Wai (Application) import Network.Wai (Application)
@ -41,15 +39,16 @@ import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
-- --
defaultMain :: (Show env, Read env) defaultMain :: (Show env, Read env)
=> IO (AppConfig env extra) => IO (AppConfig env extra)
-> (AppConfig env extra -> Logger -> (Application -> IO ()) -> IO ()) -> (AppConfig env extra -> Logger -> IO Application)
-> IO () -> IO ()
defaultMain load withSite = do defaultMain load getApp = do
config <- load config <- load
logger <- makeDefaultLogger logger <- makeDefaultLogger
withSite config logger $ runSettings defaultSettings app <- getApp config logger
runSettings defaultSettings
{ settingsHost = "0.0.0.0" { settingsHost = "0.0.0.0"
, settingsPort = appPort config , settingsPort = appPort config
} } app
-- | Run your application continously, listening for SIGINT and exiting -- | Run your application continously, listening for SIGINT and exiting
-- when recieved -- when recieved
@ -59,18 +58,15 @@ defaultMain load withSite = do
-- > Settings.withConnectionPool conf $ \p -> do -- > Settings.withConnectionPool conf $ \p -> do
-- > runConnectionPool (runMigration yourMigration) p -- > runConnectionPool (runMigration yourMigration) p
-- > defaultRunner f $ YourSite conf logger p -- > defaultRunner f $ YourSite conf logger p
defaultRunner :: (YesodDispatch y y, Yesod y) defaultRunner :: (Application -> IO ()) -> Application -> IO ()
=> (Application -> IO a) defaultRunner f app = do
-> y -- ^ your foundation type
-> IO ()
defaultRunner f h = do
-- clear the .static-cache so we don't have stale content -- clear the .static-cache so we don't have stale content
exists <- doesDirectoryExist staticCache exists <- doesDirectoryExist staticCache
when exists $ removeDirectoryRecursive staticCache when exists $ removeDirectoryRecursive staticCache
#ifdef WINDOWS #ifdef WINDOWS
toWaiAppPlain h >>= f . middlewares >> return () f (middlewares app)
#else #else
tid <- forkIO $ toWaiAppPlain h >>= f . middlewares >> return () tid <- forkIO $ f (middlewares app) >> return ()
flag <- newEmptyMVar flag <- newEmptyMVar
_ <- Signal.installHandler Signal.sigINT (Signal.CatchOnce $ do _ <- Signal.installHandler Signal.sigINT (Signal.CatchOnce $ do
putStrLn "Caught an interrupt" putStrLn "Caught an interrupt"
@ -84,30 +80,22 @@ defaultRunner f h = do
gset = def { gzipFiles = GzipCacheFolder staticCache } gset = def { gzipFiles = GzipCacheFolder staticCache }
staticCache = ".static-cache" staticCache = ".static-cache"
-- | Run your development app using the provided @'DefaultEnv'@ type
--
-- > withDevelAppPort :: Dynamic
-- > withDevelAppPort = toDyn $ defaultDevelApp withMySite
--
defaultDevelApp :: (AppConfig DefaultEnv () -> Logger -> (Application -> IO ()) -> IO ())
-> ((Int, Application) -> IO ())
-> IO ()
defaultDevelApp = defaultDevelAppWith loadDevelopmentConfig
-- | Run your development app using a custom environment type and loader -- | Run your development app using a custom environment type and loader
-- function -- function
-- --
-- > withDevelAppPort :: Dynamic -- > withDevelAppPort :: Dynamic
-- > withDevelAppPort = toDyn $ (defaultDevelAppWith customLoadAppConfig) withMySite -- > withDevelAppPort = toDyn $ defaultDevelApp customLoadAppConfig withMySite
-- --
defaultDevelAppWith :: (Show env, Read env) defaultDevelApp
=> IO (AppConfig env extra) -- ^ A means to load your development @'AppConfig'@ :: (Show env, Read env)
-> (AppConfig env extra -> Logger -> (Application -> IO ()) -> IO ()) -- ^ Your @withMySite@ function => IO (AppConfig env extra) -- ^ A means to load your development @'AppConfig'@
-> ((Int, Application) -> IO ()) -> IO () -> (AppConfig env extra -> Logger -> IO Application) -- ^ Get your @Application@
defaultDevelAppWith load withSite f = do -> ((Int, Application) -> IO ()) -> IO ()
conf <- load defaultDevelApp load getApp f = do
logger <- makeDefaultLogger conf <- load
let p = appPort conf logger <- makeDefaultLogger
logString logger $ "Devel application launched, listening on port " ++ show p let p = appPort conf
withSite conf logger $ \app -> f (p, app) logString logger $ "Devel application launched, listening on port " ++ show p
flushLogger logger app <- getApp conf logger
f (p, app)
flushLogger logger

View File

@ -84,7 +84,7 @@ scaffold = do
let runMigration = let runMigration =
case backend of case backend of
MongoDB -> "" MongoDB -> ""
_ -> "\n Database.Persist.Store.runPool dbconf (runMigration migrateAll) p" _ -> "\n Database.Persist.Store.runPool dbconf (runMigration migrateAll) p"
let importMigration = let importMigration =
case backend of case backend of

View File

@ -1,6 +1,6 @@
{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fno-warn-orphans #-}
module Application module Application
( with~sitearg~ ( getApplication
, withDevelAppPort , withDevelAppPort
) where ) where
@ -20,7 +20,7 @@ import Yesod.Logger (Logger)
import Network.Wai.Middleware.RequestLogger (logStdout) import Network.Wai.Middleware.RequestLogger (logStdout)
#endif #endif
import qualified Database.Persist.Store~importMigration~ import qualified Database.Persist.Store~importMigration~
import Network.HTTP.Conduit (withManager) import Network.HTTP.Conduit (newManagerIO)
-- Import all relevant handler modules here. -- Import all relevant handler modules here.
import Handler.Root import Handler.Root
@ -34,14 +34,16 @@ mkYesodDispatch "~sitearg~" resources~sitearg~
-- performs initialization and creates a WAI application. This is also the -- performs initialization and creates a WAI application. This is also the
-- place to put your migrate statements to have automatic database -- place to put your migrate statements to have automatic database
-- migrations handled by Yesod. -- migrations handled by Yesod.
with~sitearg~ :: AppConfig DefaultEnv Extra -> Logger -> (Application -> IO ()) -> IO () getApplication :: AppConfig DefaultEnv Extra -> Logger -> IO Application
with~sitearg~ conf logger f = withManager $ \manager -> lift $ do getApplication conf logger = do
manager <- newManagerIO 10
s <- staticSite s <- staticSite
dbconf <- withYamlEnvironment "config/~dbConfigFile~.yml" (appEnv conf) dbconf <- withYamlEnvironment "config/~dbConfigFile~.yml" (appEnv conf)
Database.Persist.Store.loadConfig Database.Persist.Store.loadConfig
Database.Persist.Store.withPool (dbconf :: Settings.PersistConfig) $ \p -> do~runMigration~ p <- Database.Persist.Store.createPoolConfig (dbconf :: Settings.PersistConfig)~runMigration~
let h = ~sitearg~ conf logger s p manager let foundation = ~sitearg~ conf logger s p manager
defaultRunner (f . logWare) h app <- toWaiAppPlain foundation
return $ logWare app
where where
#ifdef DEVELOPMENT #ifdef DEVELOPMENT
logWare = logHandleDev (\msg -> logBS logger msg >> flushLogger logger) logWare = logHandleDev (\msg -> logBS logger msg >> flushLogger logger)
@ -52,7 +54,7 @@ with~sitearg~ conf logger f = withManager $ \manager -> lift $ do
-- for yesod devel -- for yesod devel
withDevelAppPort :: Dynamic withDevelAppPort :: Dynamic
withDevelAppPort = withDevelAppPort =
toDyn $ defaultDevelAppWith loader with~sitearg~ toDyn $ defaultDevelApp loader getApplication
where where
loader = loadConfig (configSettings Development) loader = loadConfig (configSettings Development)
{ csParseExtra = parseExtra { csParseExtra = parseExtra

View File

@ -2,7 +2,7 @@ import Prelude (IO)
import Yesod.Default.Config (fromArgsExtra) import Yesod.Default.Config (fromArgsExtra)
import Yesod.Default.Main (defaultMain) import Yesod.Default.Main (defaultMain)
import Settings (parseExtra) import Settings (parseExtra)
import Application (with~sitearg~) import Application (getApplication)
main :: IO () main :: IO ()
main = defaultMain (fromArgsExtra parseExtra) with~sitearg~ main = defaultMain (fromArgsExtra parseExtra) getApplication

View File

@ -1,6 +1,6 @@
{-# OPTIONS_GHC -fno-warn-orphans #-} {-# OPTIONS_GHC -fno-warn-orphans #-}
module Application module Application
( with~sitearg~ ( getApplication
, withDevelAppPort , withDevelAppPort
) where ) where
@ -8,9 +8,15 @@ import Import
import Settings (parseExtra) import Settings (parseExtra)
import Settings.StaticFiles (staticSite) import Settings.StaticFiles (staticSite)
import Yesod.Default.Config import Yesod.Default.Config
import Yesod.Default.Main (defaultDevelAppWith, defaultRunner) import Yesod.Default.Main (defaultDevelApp)
import Yesod.Default.Handlers (getFaviconR, getRobotsR) import Yesod.Default.Handlers (getFaviconR, getRobotsR)
#if DEVELOPMENT
import Yesod.Logger (Logger, logBS, flushLogger)
import Network.Wai.Middleware.RequestLogger (logHandleDev)
#else
import Yesod.Logger (Logger) import Yesod.Logger (Logger)
import Network.Wai.Middleware.RequestLogger (logStdout)
#endif
import Network.Wai (Application) import Network.Wai (Application)
import Data.Dynamic (Dynamic, toDyn) import Data.Dynamic (Dynamic, toDyn)
@ -26,16 +32,23 @@ mkYesodDispatch "~sitearg~" resources~sitearg~
-- performs initialization and creates a WAI application. This is also the -- performs initialization and creates a WAI application. This is also the
-- place to put your migrate statements to have automatic database -- place to put your migrate statements to have automatic database
-- migrations handled by Yesod. -- migrations handled by Yesod.
with~sitearg~ :: AppConfig DefaultEnv Extra -> Logger -> (Application -> IO ()) -> IO () getApplication :: AppConfig DefaultEnv Extra -> Logger -> IO Application
with~sitearg~ conf logger f = do getApplication conf logger = do
s <- staticSite s <- staticSite
let h = ~sitearg~ conf logger s let foundation = ~sitearg~ conf logger s
defaultRunner f h app <- toWaiAppPlain foundation
return $ logWare app
where
#ifdef DEVELOPMENT
logWare = logHandleDev (\msg -> logBS logger msg >> flushLogger logger)
#else
logWare = logStdout
#endif
-- for yesod devel -- for yesod devel
withDevelAppPort :: Dynamic withDevelAppPort :: Dynamic
withDevelAppPort = withDevelAppPort =
toDyn $ defaultDevelAppWith loader with~sitearg~ toDyn $ defaultDevelApp loader getApplication
where where
loader = loadConfig (configSettings Development) loader = loadConfig (configSettings Development)
{ csParseExtra = parseExtra { csParseExtra = parseExtra

View File

@ -1,13 +1,12 @@
module Foundation module Foundation
( ~sitearg~ (..) ( ~sitearg~ (..)
, ~sitearg~Route (..) , Route (..)
, ~sitearg~Message (..) , ~sitearg~Message (..)
, resources~sitearg~ , resources~sitearg~
, Handler , Handler
, Widget , Widget
, module Yesod.Core , module Yesod.Core
, module Settings , module Settings
, StaticRoute (..)
, liftIO , liftIO
) where ) where
@ -15,7 +14,7 @@ import Prelude
import Yesod.Core import Yesod.Core
import Yesod.Default.Config import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal) import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Static (Static, base64md5, StaticRoute(..)) import Yesod.Static
import Settings.StaticFiles import Settings.StaticFiles
import Yesod.Logger (Logger, logMsg, formatLogText) import Yesod.Logger (Logger, logMsg, formatLogText)
import qualified Settings import qualified Settings

View File

@ -76,6 +76,7 @@ executable ~project~
, hamlet >= 0.10 && < 0.11 , hamlet >= 0.10 && < 0.11
, shakespeare-text >= 0.10 && < 0.11 , shakespeare-text >= 0.10 && < 0.11
, wai >= 1.0 && < 1.1 , wai >= 1.0 && < 1.1
, wai-extra >= 1.0 && < 1.1
, transformers >= 0.2 && < 0.3 , transformers >= 0.2 && < 0.3
, monad-control >= 0.3 && < 0.4 , monad-control >= 0.3 && < 0.4
, yaml >= 0.5 && < 0.6 , yaml >= 0.5 && < 0.6