Loading Main.hs +22 −2 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ import Control.Applicative ((<|>), liftA2) import Data.Monoid ((<>)) import Data.Text.Lazy (Text) import qualified Data.Text.Lazy as L import Text.Read (readMaybe) import System.Environment (lookupEnv) import Web.Scotty getClientIP1 :: ActionM (Maybe Text) Loading @@ -29,6 +31,24 @@ getClientIP = do Nothing -> text "unknown\n" Just rawIp -> text $ rawIp <> "\n" -- | look up an environment variable, if it doesn't exist or is empty, use -- default value instead. Otherwise, call a reader function on it and use that -- value if parse is okay, use default value if parse fail. getEnvDefault :: String -> a -> (String -> Maybe a) -> IO a getEnvDefault variable defaultValue stringReader = do r <- lookupEnv variable return $ case r of Nothing -> defaultValue Just [] -> defaultValue Just xs -> case stringReader xs of Nothing -> defaultValue Just x -> x getListenPort :: IO Int getListenPort = getEnvDefault "PORT" 8081 readMaybe main :: IO () main = scotty 3000 $ do main = do port <- getListenPort scotty port $ do get "/" $ getClientIP Makefile 0 → 100644 +6 −0 Original line number Diff line number Diff line LOCAL_FILE=./.stack-work/install/x86_64-linux/lts-3.0/7.10.2/bin/get-client-ip update: stack clean stack build rsync -air $(LOCAL_FILE) hk:/usr/local/bin/ .PHONY: update Loading
Main.hs +22 −2 Original line number Diff line number Diff line Loading @@ -4,6 +4,8 @@ import Control.Applicative ((<|>), liftA2) import Data.Monoid ((<>)) import Data.Text.Lazy (Text) import qualified Data.Text.Lazy as L import Text.Read (readMaybe) import System.Environment (lookupEnv) import Web.Scotty getClientIP1 :: ActionM (Maybe Text) Loading @@ -29,6 +31,24 @@ getClientIP = do Nothing -> text "unknown\n" Just rawIp -> text $ rawIp <> "\n" -- | look up an environment variable, if it doesn't exist or is empty, use -- default value instead. Otherwise, call a reader function on it and use that -- value if parse is okay, use default value if parse fail. getEnvDefault :: String -> a -> (String -> Maybe a) -> IO a getEnvDefault variable defaultValue stringReader = do r <- lookupEnv variable return $ case r of Nothing -> defaultValue Just [] -> defaultValue Just xs -> case stringReader xs of Nothing -> defaultValue Just x -> x getListenPort :: IO Int getListenPort = getEnvDefault "PORT" 8081 readMaybe main :: IO () main = scotty 3000 $ do main = do port <- getListenPort scotty port $ do get "/" $ getClientIP
Makefile 0 → 100644 +6 −0 Original line number Diff line number Diff line LOCAL_FILE=./.stack-work/install/x86_64-linux/lts-3.0/7.10.2/bin/get-client-ip update: stack clean stack build rsync -air $(LOCAL_FILE) hk:/usr/local/bin/ .PHONY: update