我通常用于在单独的线程forkIO
中生成。simpleHTTP
然后waitForTermination
等待^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
来自happstack-state
包。由于多种原因,它确实需要移动到其他地方。如果您不想安装happstack-state
,可以将本地副本复制并粘贴到您的应用中:
-- | 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