Created
July 11, 2015 00:45
-
-
Save dbbbit/8b29726b0c273e2a031d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--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