]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - examples/HelloWorld.hs
hlint
[Lucu.git] / examples / HelloWorld.hs
index 52ceceb14f34b937951288940a5b962502769335..2d240e8bb8bb0b751cdaf1d56a76c24b159dd82f 100644 (file)
@@ -1,9 +1,48 @@
+{-# LANGUAGE
+    OverloadedStrings
+  , QuasiQuotes
+  , UnicodeSyntax
+  #-}
+import Control.Applicative
+import Control.Monad.Unicode
+import qualified Data.ByteString.Lazy.Char8 as Lazy
+import qualified Data.Collections as C
+import Data.Monoid.Unicode
 import Network
-import Network.HTTP.Lucu.Config
-import Network.HTTP.Lucu.Httpd
+import Network.HTTP.Lucu
+import Prelude.Unicode
 
-main :: IO ()
-main = let config    = defaultConfig { cnfServerPort = PortNumber 9999 }
-           resources = mkResourceTable []
+main ∷ IO ()
+main = let config    = defaultConfig { cnfServerPort = "9999" }
+           mapper    = resourceMap resources ⊕ resourceMap fallbacks
+           resources ∷ ResourceTree
+           resources = C.fromList
+                       [ ([]          , nonGreedy helloWorld)
+                       , (["urandom" ], nonGreedy $ staticFile "/dev/urandom")
+                       , (["inc"     ], greedy    $ staticDir  "/usr/include")
+                       , (["inc", "t"], nonGreedy $ staticFile "/usr/include/time.h")
+                       ]
+           fallbacks ∷ Path → Maybe (Path, Resource)
+           fallbacks path
+               | path ≡ ["hello"] = Just (path, helloWorld)
+               | otherwise        = Nothing
        in
-         runHttpd config resources
\ No newline at end of file
+         do putStrLn "Access http://localhost:9999/ with your browser."
+            withSocketsDo $ runHttpd config mapper
+
+helloWorld ∷ Resource
+helloWorld = C.fromList
+             [ ( GET
+               , do setContentType [mimeType| text/hello |]
+                    putChunk "Hello, "
+                    putChunk "World!\n"
+                    putChunks =≪ Lazy.pack <$> getRemoteAddr'
+               )
+             , ( POST
+               , do str1 ← getChunk 3
+                    str2 ← getChunk 3
+                    str3 ← getChunk 3
+                    setContentType [mimeType| text/hello |]
+                    putChunks $ Lazy.fromChunks ["[", str1, " - ", str2, "#", str3, "]"]
+               )
+             ]