I generally use forkIO
to spawn simpleHTTP
in a separate thread. And then waitForTermination
to wait for ^C
.
module Main where
import Control.Concurrent (killThread, forkIO)
import Happstack.Server (nullConf, simpleHTTP, ok, toResponse)
import Happstack.State (waitForTermination)
main :: IO ()
main =
do putStrLn "begin server"
httpThreadId <- forkIO $ simpleHTTP nullConf (ok $ toResponse "This site rules!")
waitForTermination
killThread httpThreadId
putStrLn "end server"
waitForTermination
comes from the happstack-state
package. It really needs to be moved somewhere else for multiple reasons. If you don't want to install happstack-state
you can copy and paste a local copy into your app:
-- | Wait for a signal.
-- On unix, a signal is sigINT or sigTERM.
waitForTermination :: IO ()
waitForTermination
= do istty <- queryTerminal stdInput
mv <- newEmptyMVar
installHandler softwareTermination (CatchOnce (putMVar mv ())) Nothing
case istty of
True -> do installHandler keyboardSignal (CatchOnce (putMVar mv ())) Nothing
return ()
False -> return ()
takeMVar mv