]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
staticFile
[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                                  , ( ["compilers"]
24                                    , staticFile "/etc/compilers" )
25                                  ]
26        in
27          do installHandler sigPIPE Ignore Nothing
28             runHttpd config resources
29
30
31 helloWorld :: ResourceDef
32 helloWorld
33     = ResourceDef {
34         resUsesNativeThread = False
35       , resIsGreedy         = False
36       , resGet
37           = Just $ do time <- liftIO $ getClockTime
38                       foundEntity (strongETag "abcde") time
39                       setContentType $ "text" +/+ "hello"
40                       outputChunk "Hello, "
41                       outputChunk "World!\n"
42       , resHead   = Nothing
43       , resPost
44           = Just $ do str1 <- inputChunk 3
45                       str2 <- inputChunk 3
46                       str3 <- inputChunk 3
47                       setContentType $ "text" +/+ "hello"
48                       output ("[" ++ str1 ++ " - " ++ str2 ++ "#" ++ str3 ++ "]")
49       , resPut    = Nothing
50       , resDelete = Nothing
51       }