]> gitweb @ CieloNegro.org - Lucu.git/blobdiff - examples/Multipart.hs
Use blaze-html instead of HXT.
[Lucu.git] / examples / Multipart.hs
index b7faa38dc97405aebb0d909a11d2cd2fa344f913..ab857a8be0b13bf85f5beed89028011ca9a97cc0 100644 (file)
@@ -1,35 +1,56 @@
-import Data.List
+{-# LANGUAGE
+    OverloadedStrings
+  , QuasiQuotes
+  , UnicodeSyntax
+  #-}
+import qualified Data.ByteString.Lazy.Char8 as Lazy
+import Control.Applicative
+import qualified Data.Collections as C
+import Control.Monad.Unicode
 import Data.Maybe
+import Data.Monoid.Unicode
 import Network
 import Network.HTTP.Lucu
+import Prelude hiding (head)
+import Prelude.Unicode
+import Text.Blaze hiding (text)
+import Text.Blaze.Html5 hiding (text)
+import Text.Blaze.Html5.Attributes hiding (form, title)
+import Text.Blaze.Renderer.Utf8
 
-main :: IO ()
-main = let config    = defaultConfig { cnfServerPort = PortNumber 9999 }
-           resources = mkResTree [ ([], resMain) ]
+main ∷ IO ()
+main = let config = defaultConfig { cnfServerPort = "9999" }
+           tree   ∷ ResourceTree
+           tree   = C.fromList [ ([], nonGreedy resMain) ]
        in
          do putStrLn "Access http://localhost:9999/ with your browser."
-            runHttpd config resources
+            withSocketsDo ∘ runHttpd config $ resourceMap tree
 
-
-resMain :: ResourceDef
-resMain 
-    = ResourceDef {
-        resUsesNativeThread = False
-      , resIsGreedy         = False
-      , resGet
-          = Just $ do setContentType $ read "text/html"
-                      output ("<title>Multipart Form Test</title>" ++
-                              "<form action=\"/\" method=\"post\" enctype=\"multipart/form-data\">" ++
-                              "  Enter some value:" ++
-                              "  <input type=\"text\" name=\"val\">" ++
-                              "  <input type=\"submit\" value=\"Submit\">" ++
-                              "</form>")
-      , resHead   = Nothing
-      , resPost
-          = Just $ do form <- inputForm defaultLimit
-                      let value = fromMaybe "" $ fmap snd $ find ((== "val") . fst) form
-                      setContentType $ read "text/plain"
-                      output ("You entered: " ++ value)
-      , resPut    = Nothing
-      , resDelete = Nothing
-      }
\ No newline at end of file
+resMain ∷ Resource
+resMain = C.fromList
+          [ ( GET
+            , do setContentType [mimeType| text/html; charset="UTF-8" |]
+                 putBuilder
+                     $ renderHtmlBuilder
+                     $ docTypeHtml ! lang "en"
+                     $ do head $ do meta ! charset "UTF-8"
+                                    title "Multipart Form Test"
+                          body $ form ! action  "/"
+                                      ! method  "post"
+                                      ! enctype "multipart/form-data"
+                               $ do toHtml ("Upload some file:" ∷ String)
+                                    input ! type_ "text"   ! name  "text"
+                                    input ! type_ "file"   ! name  "file"
+                                    input ! type_ "submit" ! value "Submit"
+            )
+          , ( POST
+            , do f ← getForm Nothing
+                 let text     = fromMaybe (∅) $ fdContent <$> lookup "text" f
+                     file     = fromMaybe (∅) $ fdContent <$> lookup "file" f
+                     fileName = fdFileName =≪ lookup "file" f
+                 setContentType $ parseMIMEType "text/plain"
+                 putChunks $ "You entered \"" ⊕ text ⊕ "\".\n"
+                 putChunks $ "You uploaded a " ⊕ Lazy.pack (show $ Lazy.length file) ⊕ " bytes long file.\n"
+                 putChunks $ "The file name is " ⊕ Lazy.pack (show fileName) ⊕ ".\n"
+            )
+          ]