]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
Implemented fallback handler.
[Lucu.git] / examples / HelloWorld.hs
1 import Network
2 import Network.HTTP.Lucu
3
4 main :: IO ()
5 main = let config    = defaultConfig { cnfServerPort = PortNumber 9999 }
6            resources = mkResTree [ ( []
7                                    , helloWorld )
8
9                                  , ( ["urandom"]
10                                    , staticFile "/dev/urandom" )
11
12                                  , ( ["inc"]
13                                    , staticDir "/usr/include" )
14                                  ]
15            fallbacks = [ \ path -> case path of
16                                      ["hello"] -> return $ Just helloWorld
17                                      _         -> return Nothing
18                        ]
19        in
20          do putStrLn "Access http://localhost:9999/ with your browser."
21             runHttpd config resources fallbacks
22
23
24 helloWorld :: ResourceDef
25 helloWorld
26     = ResourceDef {
27         resUsesNativeThread = False
28       , resIsGreedy         = False
29       , resGet
30           = Just $ do --time <- liftIO $ getClockTime
31                       --foundEntity (strongETag "abcde") time
32                       setContentType $ read "text/hello"
33                       outputChunk "Hello, "
34                       outputChunk "World!\n"
35       , resHead   = Nothing
36       , resPost
37           = Just $ do str1 <- inputChunk 3
38                       str2 <- inputChunk 3
39                       str3 <- inputChunk 3
40                       setContentType $ read "text/hello"
41                       output ("[" ++ str1 ++ " - " ++ str2 ++ "#" ++ str3 ++ "]")
42       , resPut    = Nothing
43       , resDelete = Nothing
44       }