on 644bfda help sign in
typeable/wai/test.hs
41 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 = Status200
17 , headers = [(ContentType, B8.pack "text/html")]
18 , body = index
19 }
21postResponse :: IO (Maybe B.ByteString) -> IO Response
22postResponse rb = return Response
23 { status = Status200
24 , headers = [(ContentType, B8.pack "text/plain")]
25 , body = Right $ postBody rb
26 }
28index :: Either FilePath a
29index = Left "index.html"
31postBody :: IO (Maybe B.ByteString) -> Enumerator
32postBody req = Enumerator $ \f a -> helper f a where
33 helper f a = do
34 mbs <- req
35 case mbs of
36 Nothing -> return a
37 Just bs -> do
38 res <- f a bs
39 case res of
40 Left a' -> return a'
41 Right a' -> helper f a'