]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
d7e0071c8767f72caf9e9bc72c0ef08e748c3b66
[Lucu.git] / examples / HelloWorld.hs
1 {-# LANGUAGE
2     OverloadedStrings
3   , UnicodeSyntax
4   #-}
5 import Control.Applicative
6 import Control.Monad.Unicode
7 import qualified Data.ByteString.Lazy.Char8 as Lazy
8 import Network.HTTP.Lucu
9
10 main ∷ IO ()
11 main = let config    = defaultConfig { cnfServerPort = "9999" }
12            resources = mkResTree
13                        [ ([]         , helloWorld               )
14                        , (["urandom"], staticFile "/dev/urandom")
15                        , (["inc"    ], staticDir "/usr/include" )
16                        ]
17            fallbacks = [ \ path → case path of
18                                      ["hello"] → return $ Just helloWorld
19                                      _         → return Nothing
20                        ]
21        in
22          do putStrLn "Access http://localhost:9999/ with your browser."
23             runHttpd config resources fallbacks
24
25 helloWorld ∷ ResourceDef
26 helloWorld
27     = emptyResource {
28         resGet
29           = Just $ do setContentType $ parseMIMEType "text/hello"
30                       putChunk "Hello, "
31                       putChunk "World!\n"
32                       putChunks =≪ Lazy.pack <$> getRemoteAddr'
33       , resPost
34           = Just $ do str1 ← getChunk 3
35                       str2 ← getChunk 3
36                       str3 ← getChunk 3
37                       setContentType $ parseMIMEType "text/hello"
38                       putChunks $ Lazy.fromChunks ["[", str1, " - ", str2, "#", str3, "]"]
39       }