From 94f4548adc448cecf16f3648f3d425c91b0c1eb6 Mon Sep 17 00:00:00 2001 From: patrick brisbin Date: Fri, 23 Sep 2011 09:23:44 -0400 Subject: [PATCH] This reverts commit 22ca2af4215d41ee60ad76d14aa69adc6027ac52. --- yesod-default/Yesod/Default/Main.hs | 37 +++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/yesod-default/Yesod/Default/Main.hs b/yesod-default/Yesod/Default/Main.hs index ee6c83c5..9cb36d8d 100644 --- a/yesod-default/Yesod/Default/Main.hs +++ b/yesod-default/Yesod/Default/Main.hs @@ -2,16 +2,24 @@ {-# LANGUAGE DeriveDataTypeable #-} module Yesod.Default.Main ( defaultMain + , defaultRunner , defaultDevelApp , defaultDevelAppWith ) where +import Yesod.Core import Yesod.Default.Config import Yesod.Logger (Logger, makeLogger, logString, logLazyText, flushLogger) import Network.Wai (Application) import Network.Wai.Handler.Warp (run) import Network.Wai.Middleware.Debug (debugHandle) +#ifndef WINDOWS +import qualified System.Posix.Signals as Signal +import Control.Concurrent (forkIO, killThread) +import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar) +#endif + -- | Run your app, taking environment and port settings from the -- commandline. -- @@ -32,6 +40,35 @@ defaultMain load withSite = do logger <- makeLogger withSite config logger $ run (appPort config) +-- | Run your application continously, listening for SIGINT and exiting +-- when recieved +-- +-- > withYourSite :: AppConfig DefaultEnv -> Logger -> (Application -> IO a) -> IO () +-- > withYourSite conf logger f = do +-- > Settings.withConnectionPool conf $ \p -> do +-- > runConnectionPool (runMigration yourMigration) p +-- > defaultRunner f $ YourSite conf logger p +-- +-- TODO: ifdef WINDOWS +-- +defaultRunner :: (YesodDispatch y y, Yesod y) + => (Application -> IO a) + -> y -- ^ your foundation type + -> IO () +defaultRunner f h = +#ifdef WINDOWS + toWaiApp h >>= f >> return () +#else + do + tid <- forkIO $ toWaiApp h >>= f >> return () + flag <- newEmptyMVar + _ <- Signal.installHandler Signal.sigINT (Signal.CatchOnce $ do + putStrLn "Caught an interrupt" + killThread tid + putMVar flag ()) Nothing + takeMVar flag +#endif + -- | Run your development app using the provided @'DefaultEnv'@ type -- -- > withDevelAppPort :: Dynamic