]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
d584009c61cd3f4411836e18f2d0e299741ded70
[Lucu.git] / examples / HelloWorld.hs
1 import Control.Monad.Trans
2 import Data.Maybe
3 import Network
4 import Network.HTTP.Lucu.Config
5 import Network.HTTP.Lucu.ETag
6 import Network.HTTP.Lucu.Httpd
7 import Network.HTTP.Lucu.MIMEType
8 import Network.HTTP.Lucu.MIMEType.Guess
9 import Network.HTTP.Lucu.Parser
10 import Network.HTTP.Lucu.Parser.Http
11 import Network.HTTP.Lucu.Resource
12 import Network.HTTP.Lucu.Resource.Tree
13 import Network.HTTP.Lucu.Response
14 import Network.HTTP.Lucu.StaticFile
15 import Network.URI
16 import System.Posix.Signals
17 import System.Time
18
19 main :: IO ()
20 main = let config    = defaultConfig { cnfServerPort = PortNumber 9999 }
21            resources = mkResTree [ ( []
22                                    , helloWorld )
23
24                                  , ( ["urandom"]
25                                    , staticFile "/dev/urandom" )
26
27                                  , ( ["inc"]
28                                    , staticDir "/usr/include" )
29                                  ]
30        in
31          do installHandler sigPIPE Ignore Nothing
32             runHttpd config resources
33
34
35 helloWorld :: ResourceDef
36 helloWorld
37     = ResourceDef {
38         resUsesNativeThread = False
39       , resIsGreedy         = False
40       , resGet
41           = Just $ do --time <- liftIO $ getClockTime
42                       --foundEntity (strongETag "abcde") time
43                       setContentType $ "text" </> "hello"
44                       outputChunk "Hello, "
45                       outputChunk "World!\n"
46       , resHead   = Nothing
47       , resPost
48           = Just $ do str1 <- inputChunk 3
49                       str2 <- inputChunk 3
50                       str3 <- inputChunk 3
51                       setContentType $ "text" </> "hello"
52                       output ("[" ++ str1 ++ " - " ++ str2 ++ "#" ++ str3 ++ "]")
53       , resPut    = Nothing
54       , resDelete = Nothing
55       }