1. Ping Chen
  2. scripts

Commits

Ping Chen  committed 719d57b

warp -p port_num

  • Participants
  • Parent commits 18168c0
  • Branches master

Comments (0)

Files changed (2)

File grep-hackage

-#!/bin/sh
-ghc-pkg list | grep $1

File warp

View file
 import Filesystem.Path.CurrentOS (decodeString)
 import Network.Wai.Handler.Warp (run)
 import Network.Wai.Application.Static (staticApp, defaultFileServerSettings)
+import System.Console.GetOpt
 import System.Directory (getCurrentDirectory)
- 
+import System.IO
+import System.Environment (getArgs)
+import System.Exit
+
+data Flag = Help | Port Int
+            deriving Eq
+
+parseArgs :: IO Int
+parseArgs = do
+    argv <- getArgs
+    case parse argv of
+         ([], [], [])                           -> return 3030
+         (opts, [], [])
+            | Help `elem` opts                  -> help
+            | [Port n] <- filter (/=Help) opts  -> return n
+         (_,_,errs)                             -> die errs
+  where
+    parse argv = getOpt Permute options argv
+    header     = "Usage: warp [-h] [-p port_num]"
+    info       = usageInfo header options
+    dump       = hPutStrLn stderr
+    die errs   = dump (concat errs ++ info) >> exitWith (ExitFailure 1)
+    help       = dump info                  >> exitWith ExitSuccess
+
+options :: [OptDescr Flag]
+options = [Option ['h'] ["help"] (NoArg Help)
+                  "Show this help message",
+           Option ['p'] ["port"] (ReqArg (\s -> Port (read s)) "Port")
+                  "Port number to use (default 3030)"]
+
 main :: IO ()
 main = do
+    port <- parseArgs
     pwd <- getCurrentDirectory
     let settings = defaultFileServerSettings $ decodeString pwd
-    putStrLn "Serving HTTP on 0.0.0.0 port 3000"
-    run 3000 $  staticApp settings
+    putStrLn $ "Serving HTTP on 0.0.0.0 port " ++ (show port)
+    run port $ staticApp settings