on 999fbde help sign in
typeable/wai/test.hs
38 lines 1.1 kB history raw
1import Network.Wai
2import Network.Wai.Handler.SimpleServer
3import qualified Data.ByteString.Char8 as B8
4import qualified Data.ByteString as B
6main :: IO ()
7main = putStrLn "http://localhost:3000/" >> run 3000 app
9app :: Application
10app req = case B8.unpack $ pathInfo req of
11 "/post/" -> postResponse $ requestBody req
12 _ -> indexResponse
14indexResponse :: IO Response
15indexResponse = return Response
16 { status = 200
17 , statusMessage = B8.pack "OK"
18 , headers = [(B8.pack "Content-Type", B8.pack "text/html")]
19 , body = index
20 }
22postResponse :: IO (Maybe B.ByteString) -> IO Response
23postResponse rb = return Response
24 { status = 200
25 , statusMessage = B8.pack "OK"
26 , headers = [(B8.pack "Content-Type", B8.pack "text/plain")]
27 , body = postBody rb
28 }
30index :: ResponseBody -> IO ()
31index rb = sendFile rb "index.html"
33postBody :: IO (Maybe B.ByteString) -> ResponseBody -> IO ()
34postBody req res = do
35 mbs <- req
36 case mbs of
37 Nothing -> return ()
38 Just bs -> sendByteString res bs >> postBody req res