Remainder of Logger changes, scaffolded site works (#360)
This commit is contained in:
parent
ddd1059983
commit
985dd6c924
@ -7,7 +7,6 @@ module Yesod.Default.Main
|
|||||||
) where
|
) where
|
||||||
|
|
||||||
import Yesod.Default.Config
|
import Yesod.Default.Config
|
||||||
import Yesod.Logger (Logger, defaultDevelopmentLogger, logString)
|
|
||||||
import Network.Wai (Application)
|
import Network.Wai (Application)
|
||||||
import Network.Wai.Handler.Warp
|
import Network.Wai.Handler.Warp
|
||||||
(runSettings, defaultSettings, settingsPort, settingsHost)
|
(runSettings, defaultSettings, settingsPort, settingsHost)
|
||||||
@ -33,12 +32,11 @@ 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 -> IO Application)
|
-> (AppConfig env extra -> IO Application)
|
||||||
-> IO ()
|
-> IO ()
|
||||||
defaultMain load getApp = do
|
defaultMain load getApp = do
|
||||||
config <- load
|
config <- load
|
||||||
logger <- defaultDevelopmentLogger
|
app <- getApp config
|
||||||
app <- getApp config logger
|
|
||||||
print $ appHost config
|
print $ appHost config
|
||||||
runSettings defaultSettings
|
runSettings defaultSettings
|
||||||
{ settingsPort = appPort config
|
{ settingsPort = appPort config
|
||||||
@ -80,12 +78,11 @@ defaultRunner f app = do
|
|||||||
defaultDevelApp
|
defaultDevelApp
|
||||||
:: (Show env, Read env)
|
:: (Show env, Read env)
|
||||||
=> IO (AppConfig env extra) -- ^ A means to load your development @'AppConfig'@
|
=> IO (AppConfig env extra) -- ^ A means to load your development @'AppConfig'@
|
||||||
-> (AppConfig env extra -> Logger -> IO Application) -- ^ Get your @Application@
|
-> (AppConfig env extra -> IO Application) -- ^ Get your @Application@
|
||||||
-> IO (Int, Application)
|
-> IO (Int, Application)
|
||||||
defaultDevelApp load getApp = do
|
defaultDevelApp load getApp = do
|
||||||
conf <- load
|
conf <- load
|
||||||
logger <- defaultDevelopmentLogger
|
|
||||||
let p = appPort conf
|
let p = appPort conf
|
||||||
logString logger $ "Devel application launched: http://localhost:" ++ show p
|
putStrLn $ "Devel application launched: http://localhost:" ++ show p
|
||||||
app <- getApp conf logger
|
app <- getApp conf
|
||||||
return (p, app)
|
return (p, app)
|
||||||
|
|||||||
@ -138,10 +138,10 @@ instance RenderRoute Static where
|
|||||||
|
|
||||||
instance Yesod master => YesodDispatch Static master where
|
instance Yesod master => YesodDispatch Static master where
|
||||||
-- Need to append trailing slash to make relative links work
|
-- Need to append trailing slash to make relative links work
|
||||||
yesodDispatch _ _ _ _ _ _ [] _ req =
|
yesodDispatch _ _ _ _ _ _ _ [] _ req =
|
||||||
return $ responseLBS status301 [("Location", rawPathInfo req `S.append` "/")] ""
|
return $ responseLBS status301 [("Location", rawPathInfo req `S.append` "/")] ""
|
||||||
|
|
||||||
yesodDispatch _ (Static set) _ _ _ _ textPieces _ req =
|
yesodDispatch _ _ (Static set) _ _ _ _ textPieces _ req =
|
||||||
staticApp set req { pathInfo = textPieces }
|
staticApp set req { pathInfo = textPieces }
|
||||||
|
|
||||||
notHidden :: Prelude.FilePath -> Bool
|
notHidden :: Prelude.FilePath -> Bool
|
||||||
|
|||||||
@ -45,15 +45,13 @@ import Text.Julius
|
|||||||
import Yesod.Form
|
import Yesod.Form
|
||||||
import Yesod.Json
|
import Yesod.Json
|
||||||
import Yesod.Persist
|
import Yesod.Persist
|
||||||
import Network.HTTP.Types (status200)
|
|
||||||
import Control.Monad.IO.Class (liftIO, MonadIO(..))
|
import Control.Monad.IO.Class (liftIO, MonadIO(..))
|
||||||
import Control.Monad.Trans.Control (MonadBaseControl)
|
import Control.Monad.Trans.Control (MonadBaseControl)
|
||||||
|
|
||||||
import Network.Wai
|
import Network.Wai
|
||||||
import Network.Wai.Logger
|
import Network.Wai.Middleware.RequestLogger (logStdout)
|
||||||
import Network.Wai.Handler.Warp (run)
|
import Network.Wai.Handler.Warp (run)
|
||||||
import System.IO (stderr, stdout, hFlush, hPutStrLn)
|
import System.IO (stderr, hPutStrLn)
|
||||||
import System.Log.FastLogger
|
|
||||||
#if MIN_VERSION_blaze_html(0, 5, 0)
|
#if MIN_VERSION_blaze_html(0, 5, 0)
|
||||||
import Text.Blaze.Html (toHtml)
|
import Text.Blaze.Html (toHtml)
|
||||||
#else
|
#else
|
||||||
@ -80,23 +78,7 @@ warpDebug :: (Yesod a, YesodDispatch a a) => Int -> a -> IO ()
|
|||||||
warpDebug port app = do
|
warpDebug port app = do
|
||||||
hPutStrLn stderr $ "Application launched, listening on port " ++ show port
|
hPutStrLn stderr $ "Application launched, listening on port " ++ show port
|
||||||
waiApp <- toWaiApp app
|
waiApp <- toWaiApp app
|
||||||
dateRef <- dateInit
|
run port $ logStdout waiApp
|
||||||
run port $ (logStdout dateRef) waiApp
|
|
||||||
|
|
||||||
logStdout :: DateRef -> Middleware
|
|
||||||
logStdout dateRef waiApp =
|
|
||||||
\req -> do
|
|
||||||
logRequest dateRef req
|
|
||||||
waiApp req
|
|
||||||
|
|
||||||
logRequest :: Control.Monad.IO.Class.MonadIO m =>
|
|
||||||
DateRef -> Network.Wai.Request -> m ()
|
|
||||||
logRequest dateRef req = do
|
|
||||||
date <- liftIO $ getDate dateRef
|
|
||||||
let status = status200
|
|
||||||
len = 4
|
|
||||||
liftIO $ hPutLogStr stdout $ apacheFormat FromSocket date req status (Just len)
|
|
||||||
liftIO $ hFlush stdout
|
|
||||||
|
|
||||||
-- | Run a development server, where your code changes are automatically
|
-- | Run a development server, where your code changes are automatically
|
||||||
-- reloaded.
|
-- reloaded.
|
||||||
|
|||||||
@ -11,8 +11,7 @@ import Yesod.Auth
|
|||||||
import Yesod.Default.Config
|
import Yesod.Default.Config
|
||||||
import Yesod.Default.Main
|
import Yesod.Default.Main
|
||||||
import Yesod.Default.Handlers
|
import Yesod.Default.Handlers
|
||||||
import Yesod.Logger (Logger, logBS, toProduction)
|
import Network.Wai.Middleware.RequestLogger (logStdout, logStdoutDev)
|
||||||
import Network.Wai.Middleware.RequestLogger (logCallback, logCallbackDev)
|
|
||||||
import qualified Database.Persist.Store~importMigration~
|
import qualified Database.Persist.Store~importMigration~
|
||||||
import Network.HTTP.Conduit (newManager, def)
|
import Network.HTTP.Conduit (newManager, def)
|
||||||
|
|
||||||
@ -29,25 +28,24 @@ 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.
|
||||||
makeApplication :: AppConfig DefaultEnv Extra -> Logger -> IO Application
|
makeApplication :: AppConfig DefaultEnv Extra -> IO Application
|
||||||
makeApplication conf logger = do
|
makeApplication conf = do
|
||||||
foundation <- makeFoundation conf setLogger
|
foundation <- makeFoundation conf
|
||||||
app <- toWaiAppPlain foundation
|
app <- toWaiAppPlain foundation
|
||||||
return $ logWare app
|
return $ logWare app
|
||||||
where
|
where
|
||||||
setLogger = if development then logger else toProduction logger
|
logWare = if development then logStdoutDev
|
||||||
logWare = if development then logCallbackDev (logBS setLogger)
|
else logStdout
|
||||||
else logCallback (logBS setLogger)
|
|
||||||
|
|
||||||
makeFoundation :: AppConfig DefaultEnv Extra -> Logger -> IO ~sitearg~
|
makeFoundation :: AppConfig DefaultEnv Extra -> IO ~sitearg~
|
||||||
makeFoundation conf setLogger = do
|
makeFoundation conf = do
|
||||||
manager <- newManager def
|
manager <- newManager def
|
||||||
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.applyEnv
|
Database.Persist.Store.applyEnv
|
||||||
p <- Database.Persist.Store.createPoolConfig (dbconf :: Settings.PersistConfig)~runMigration~
|
p <- Database.Persist.Store.createPoolConfig (dbconf :: Settings.PersistConfig)~runMigration~
|
||||||
return $ ~sitearg~ conf setLogger s p manager dbconf
|
return $ ~sitearg~ conf s p manager dbconf
|
||||||
|
|
||||||
-- for yesod devel
|
-- for yesod devel
|
||||||
getApplicationDev :: IO (Int, Application)
|
getApplicationDev :: IO (Int, Application)
|
||||||
|
|||||||
@ -20,7 +20,6 @@ import Yesod.Auth.BrowserId
|
|||||||
import Yesod.Auth.GoogleEmail
|
import Yesod.Auth.GoogleEmail
|
||||||
import Yesod.Default.Config
|
import Yesod.Default.Config
|
||||||
import Yesod.Default.Util (addStaticContentExternal)
|
import Yesod.Default.Util (addStaticContentExternal)
|
||||||
import Yesod.Logger (Logger, logMsg, formatLogText)
|
|
||||||
import Network.HTTP.Conduit (Manager)
|
import Network.HTTP.Conduit (Manager)
|
||||||
import qualified Settings
|
import qualified Settings
|
||||||
import qualified Database.Persist.Store
|
import qualified Database.Persist.Store
|
||||||
@ -38,7 +37,6 @@ import Text.Hamlet (hamletFile)
|
|||||||
-- access to the data present here.
|
-- access to the data present here.
|
||||||
data ~sitearg~ = ~sitearg~
|
data ~sitearg~ = ~sitearg~
|
||||||
{ settings :: AppConfig DefaultEnv Extra
|
{ settings :: AppConfig DefaultEnv Extra
|
||||||
, getLogger :: Logger
|
|
||||||
, getStatic :: Static -- ^ Settings for static file serving.
|
, getStatic :: Static -- ^ Settings for static file serving.
|
||||||
, connPool :: Database.Persist.Store.PersistConfigPool Settings.PersistConfig -- ^ Database connection pool.
|
, connPool :: Database.Persist.Store.PersistConfigPool Settings.PersistConfig -- ^ Database connection pool.
|
||||||
, httpManager :: Manager
|
, httpManager :: Manager
|
||||||
@ -107,9 +105,6 @@ instance Yesod ~sitearg~ where
|
|||||||
-- The page to be redirected to when authentication is required.
|
-- The page to be redirected to when authentication is required.
|
||||||
authRoute _ = Just $ AuthR LoginR
|
authRoute _ = Just $ AuthR LoginR
|
||||||
|
|
||||||
messageLogger y loc level msg =
|
|
||||||
formatLogText (getLogger y) loc level msg >>= logMsg (getLogger y)
|
|
||||||
|
|
||||||
-- This function creates static content files in the static folder
|
-- This function creates static content files in the static folder
|
||||||
-- and names them based on a hash of their content. This allows
|
-- and names them based on a hash of their content. This allows
|
||||||
-- expiration dates to be set far in the future without worry of
|
-- expiration dates to be set far in the future without worry of
|
||||||
|
|||||||
@ -74,7 +74,6 @@ library
|
|||||||
, transformers >= 0.2.2 && < 0.4
|
, transformers >= 0.2.2 && < 0.4
|
||||||
, wai >= 1.3 && < 1.4
|
, wai >= 1.3 && < 1.4
|
||||||
, wai-extra >= 1.3 && < 1.4
|
, wai-extra >= 1.3 && < 1.4
|
||||||
, wai-logger >= 0.1.2
|
|
||||||
, hamlet >= 1.1 && < 1.2
|
, hamlet >= 1.1 && < 1.2
|
||||||
, shakespeare-js >= 1.0 && < 1.1
|
, shakespeare-js >= 1.0 && < 1.1
|
||||||
, shakespeare-css >= 1.0 && < 1.1
|
, shakespeare-css >= 1.0 && < 1.1
|
||||||
@ -103,7 +102,6 @@ executable yesod
|
|||||||
, http-types >= 0.6.1 && < 0.7
|
, http-types >= 0.6.1 && < 0.7
|
||||||
, blaze-builder >= 0.2.1.4 && < 0.4
|
, blaze-builder >= 0.2.1.4 && < 0.4
|
||||||
, filepath >= 1.1
|
, filepath >= 1.1
|
||||||
, fast-logger >= 0.0.2 && < 0.1
|
|
||||||
, process
|
, process
|
||||||
ghc-options: -Wall -threaded
|
ghc-options: -Wall -threaded
|
||||||
main-is: main.hs
|
main-is: main.hs
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user