module Network.Wai.Handler.Warp.WithApplication (
withApplication,
testWithApplication,
openFreePort,
withFreePort,
) where
import Control.Concurrent
import Control.Concurrent.Async
import Control.Exception
import Network.Socket
import Network.Wai
import Network.Wai.Handler.Warp.Run
import Network.Wai.Handler.Warp.Settings
import Network.Wai.Handler.Warp.Types
withApplication :: IO Application -> (Port -> IO a) -> IO a
withApplication mkApp action = do
app <- mkApp
withFreePort $ \ (port, sock) -> do
started <- mkWaiter
let settings =
defaultSettings{
settingsBeforeMainLoop = notify started ()
}
result <- race
(runSettingsSocket settings sock app)
(waitFor started >> action port)
case result of
Left () -> throwIO $ ErrorCall "Unexpected: runSettingsSocket exited"
Right x -> return x
testWithApplication :: IO Application -> (Port -> IO a) -> IO a
testWithApplication mkApp action = do
callingThread <- myThreadId
app <- mkApp
let wrappedApp request respond =
app request respond `catch` \ e -> do
throwTo callingThread (e :: SomeException)
throwIO e
withApplication (return wrappedApp) action
data Waiter a
= Waiter {
notify :: a -> IO (),
waitFor :: IO a
}
mkWaiter :: IO (Waiter a)
mkWaiter = do
mvar <- newEmptyMVar
return $ Waiter {
notify = putMVar mvar,
waitFor = readMVar mvar
}
openFreePort :: IO (Port, Socket)
openFreePort = do
s <- socket AF_INET Stream defaultProtocol
localhost <- inet_addr "127.0.0.1"
bind s (SockAddrInet aNY_PORT localhost)
listen s 1
port <- socketPort s
return (fromIntegral port, s)
withFreePort :: ((Port, Socket) -> IO a) -> IO a
withFreePort = bracket openFreePort (close . snd)