]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
Fixed many bugs...
[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 Data.Monoid.Unicode
9 import Network.HTTP.Lucu
10
11 main ∷ IO ()
12 main = let config    = defaultConfig { cnfServerPort = "9999" }
13            resources = mkResTree
14                        [ ([]         , helloWorld               )
15                        , (["urandom"], staticFile "/dev/urandom")
16                        , (["inc"    ], staticDir "/usr/include" )
17                        ]
18            fallbacks = [ \ path → case path of
19                                      ["hello"] → return $ Just helloWorld
20                                      _         → return Nothing
21                        ]
22        in
23          do putStrLn "Access http://localhost:9999/ with your browser."
24             runHttpd config resources fallbacks
25
26
27 helloWorld ∷ ResourceDef
28 helloWorld
29     = emptyResource {
30         resGet
31           = Just $ do setContentType $ parseMIMEType "text/hello"
32                       outputChunk "Hello, "
33                       outputChunk "World!\n"
34                       outputChunk =≪ Lazy.pack <$> getRemoteAddr'
35                       
36       , resPost
37           = Just $ do str1 ← inputChunk 3
38                       str2 ← inputChunk 3
39                       str3 ← inputChunk 3
40                       setContentType $ parseMIMEType "text/hello"
41                       output ("[" ⊕ str1 ⊕ " - " ⊕ str2 ⊕ "#" ⊕ str3 ⊕ "]")
42       }