@@ -35,6 +35,7 @@ import Network.Wai.Handler.Warp.Settings
3535import qualified Network.Wai.Handler.Warp.Timeout as T
3636import Network.Wai.Handler.Warp.Types
3737import Network.Wai.Internal (ResponseReceived (ResponseReceived))
38+import System.Environment (getEnvironment)
3839import System.IO.Error (isFullErrorType, ioeGetErrorType)
3940
4041#if WINDOWS
@@ -71,6 +72,23 @@ allowInterrupt = unblock $ return ()
7172run :: Port -> Application -> IO ()
7273run p = runSettings defaultSettings { settingsPort = p }
7374
75+-- | Run an 'Application' on the port present in the @PORT@ environment variable
76+--
77+-- Uses the 'Port' given when the variable is unset.
78+--
79+-- Since X.Y.Z
80+runEnv :: Port -> Application -> IO ()
81+runEnv p app = do
82+ mp <- fmap (lookup "PORT") getEnvironment
83+
84+ maybe (run p app) runReadPort mp
85+
86+ where
87+ runReadPort :: String -> IO ()
88+ runReadPort sp = case reads sp of
89+ ((p', _):_) -> run p' app
90+ _ -> fail $ "Invalid value in $PORT: " ++ sp
91+
7492-- | Run an 'Application' with the given 'Settings'.
7593runSettings :: Settings -> Application -> IO ()
7694runSettings set app = withSocketsDo $