module Network.Wai.Middleware.Timeout
( timeout
, timeoutStatus
, timeoutAs
) where
import Network.HTTP.Types (Status, status503)
import Network.Wai
import qualified System.Timeout as Timeout
timeout :: Int -> Middleware
timeout = timeoutStatus status503
timeoutStatus :: Status -> Int -> Middleware
timeoutStatus status = timeoutAs $ responseLBS status [] ""
timeoutAs :: Response -> Int -> Middleware
timeoutAs timeoutReponse seconds app req respond =
maybe (respond timeoutReponse) pure
=<< Timeout.timeout (seconds * 1000000) (app req respond)