Skip to content

Instantly share code, notes, and snippets.

@dbbbit
Created July 11, 2015 00:45
Show Gist options
  • Save dbbbit/8b29726b0c273e2a031d to your computer and use it in GitHub Desktop.
Save dbbbit/8b29726b0c273e2a031d to your computer and use it in GitHub Desktop.
--cabal install Network--
import Network.Socket
import Control.Exception
main :: IO()
main = do
hole <- socket AF_INET Stream defaultProtocol
bind hole (SockAddrInet 3000 0)
listen hole 5
handle_request hole
sClose hole
-- server
handle_request hole = do
(conn, _) <- accept hole
req <- (recv conn 4096) `catch` (\(SomeException e) -> return "eof")
let url = getUrl req
let resp = router url
putStrLn (url ++ " " ++ resp)
send conn resp
sClose conn
handle_request hole
-- framework utils
getUrl msg = (words ((lines msg) !! 0)) !! 1
-- application define
hello = "hello,world"
-- routing config
router :: String -> String
router "/hello" = hello
router "/hello/" = hello
router _ = "404 not found"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment