@@ -35,6 +35,7 @@ import Network.Wai.Handler.Warp.Settings
35import qualified Network.Wai.Handler.Warp.Timeout as T
35import qualified Network.Wai.Handler.Warp.Timeout as T
36import Network.Wai.Handler.Warp.Types
36import Network.Wai.Handler.Warp.Types
37import Network.Wai.Internal (ResponseReceived (ResponseReceived))
37import Network.Wai.Internal (ResponseReceived (ResponseReceived))
38+import System.Environment (getEnvironment)
38import System.IO.Error (isFullErrorType, ioeGetErrorType)
39import System.IO.Error (isFullErrorType, ioeGetErrorType)
40#if WINDOWS
41#if WINDOWS
@@ -71,6 +72,23 @@ allowInterrupt = unblock $ return ()
71run :: Port -> Application -> IO ()
72run :: Port -> Application -> IO ()
72run p = runSettings defaultSettings { settingsPort = p }
73run p = runSettings defaultSettings { settingsPort = p }
75+-- | Run an 'Application' on the port present in the @PORT@ environment variable
77+-- Uses the 'Port' given when the variable is unset.
80+runEnv :: Port -> Application -> IO ()
82+ mp <- fmap (lookup "PORT") getEnvironment
84+ maybe (run p app) runReadPort mp
87+ runReadPort :: String -> IO ()
88+ runReadPort sp = case reads sp of
89+ ((p', _):_) -> run p' app
90+ _ -> fail $ "Invalid value in $PORT: " ++ sp
74-- | Run an 'Application' with the given 'Settings'.
92-- | Run an 'Application' with the given 'Settings'.
75runSettings :: Settings -> Application -> IO ()
93runSettings :: Settings -> Application -> IO ()
76runSettings set app = withSocketsDo $
94runSettings set app = withSocketsDo $