]> gitweb @ CieloNegro.org - Lucu.git/blob - examples/HelloWorld.hs
Rename: Resource --> Rsrc; ResourceDef --> Resource
[Lucu.git] / examples / HelloWorld.hs
1 {-# LANGUAGE
2     OverloadedStrings
3   , QuasiQuotes
4   , UnicodeSyntax
5   #-}
6 import Control.Applicative
7 import Control.Monad.Unicode
8 import qualified Data.ByteString.Lazy.Char8 as Lazy
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 helloWorld ∷ ResourceDef
27 helloWorld
28     = emptyResource {
29         resGet
30           = Just $ do setContentType [mimeType| text/hello |]
31                       putChunk "Hello, "
32                       putChunk "World!\n"
33                       putChunks =≪ Lazy.pack <$> getRemoteAddr'
34       , resPost
35           = Just $ do str1 ← getChunk 3
36                       str2 ← getChunk 3
37                       str3 ← getChunk 3
38                       setContentType [mimeType| text/hello |]
39                       putChunks $ Lazy.fromChunks ["[", str1, " - ", str2, "#", str3, "]"]
40       }