]> gitweb @ CieloNegro.org - Lucu.git/commitdiff
examples
authorPHO <pho@cielonegro.org>
Sat, 26 Nov 2011 06:15:10 +0000 (15:15 +0900)
committerPHO <pho@cielonegro.org>
Sat, 26 Nov 2011 06:15:10 +0000 (15:15 +0900)
Ditz-issue: e0312227f40a0fa92d4c5d69a64dad473f54389a

examples/Implanted.hs
examples/ImplantedSmall.hs
examples/Multipart.hs
examples/SSL.hs

index 82d98e74d4c7a683f255fca5b0e2565eb5945ec6..9259c1361a08a3a7fd5eae50bbddfb253e024944 100644 (file)
@@ -1,12 +1,14 @@
 {-# LANGUAGE
     UnicodeSyntax
   #-}
+import qualified Data.Collections as C
 import MiseRafturai
 import Network.HTTP.Lucu
 
 main ∷ IO ()
-main = let config    = defaultConfig { cnfServerPort = "9999" }
-           resources = mkResTree [ ([], miseRafturai) ]
+main = let config = defaultConfig { cnfServerPort = "9999" }
+           tree   ∷ ResourceTree
+           tree   = C.fromList [ ([], nonGreedy miseRafturai) ]
        in
          do putStrLn "Access http://localhost:9999/ with your browser."
-            runHttpd config resources []
+            runHttpd config $ resourceMap tree
index a985cae482d20037d125cda916bd3dbef756f33b..f7e8856d6b350ab31320e94315e51059a930e2e7 100644 (file)
@@ -1,13 +1,14 @@
 {-# LANGUAGE
     UnicodeSyntax
   #-}
+import qualified Data.Collections as C
 import Network.HTTP.Lucu
 import SmallFile
 
 main ∷ IO ()
-main = let config    = defaultConfig { cnfServerPort = "9999" }
-           resources = mkResTree [ ([], smallFile) ]
+main = let config = defaultConfig { cnfServerPort = "9999" }
+           tree   ∷ ResourceTree
+           tree   = C.fromList [ ([], nonGreedy smallFile) ]
        in
          do putStrLn "Access http://localhost:9999/ with your browser."
-            runHttpd config resources []
-                                   
\ No newline at end of file
+            runHttpd config $ resourceMap tree
index 8ddc6189be39a8ad942d372671819fd6f066e53f..49945ec666268401256147eee98b201cd07a9876 100644 (file)
@@ -4,38 +4,40 @@
   #-}
 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.HTTP.Lucu
 
 main ∷ IO ()
-main = let config    = defaultConfig { cnfServerPort = "9999" }
-           resources = mkResTree [ ([], resMain) ]
+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 []
+            runHttpd config $ resourceMap tree
 
-
-resMain ∷ ResourceDef
-resMain 
-    = emptyResource {
-        resGet
-          = Just $ do setContentType $ parseMIMEType "text/html"
-                      putChunks $ "<title>Multipart Form Test</title>\n"
-                                ⊕ "<form action=\"/\" method=\"post\" enctype=\"multipart/form-data\">\n"
-                                ⊕ "  Upload some file:\n"
-                                ⊕ "  <input type=\"text\" name=\"text\">\n"
-                                ⊕ "  <input type=\"file\" name=\"file\">\n"
-                                ⊕ "  <input type=\"submit\" value=\"Submit\">\n"
-                                ⊕ "</form>\n"
-      , resPost
-          = Just $ do form ← getForm Nothing
-                      let text     = fromMaybe (∅) $ fdContent <$> lookup "text" form
-                          file     = fromMaybe (∅) $ fdContent <$> lookup "file" form
-                          fileName = fdFileName =≪ lookup "file" form
-                      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"
-      }
+resMain ∷ Resource
+resMain = C.fromList
+          [ ( GET
+            , do setContentType $ parseMIMEType "text/html"
+                 putChunks $ "<title>Multipart Form Test</title>\n"
+                           ⊕ "<form action=\"/\" method=\"post\" enctype=\"multipart/form-data\">\n"
+                           ⊕ "  Upload some file:\n"
+                           ⊕ "  <input type=\"text\" name=\"text\">\n"
+                           ⊕ "  <input type=\"file\" name=\"file\">\n"
+                           ⊕ "  <input type=\"submit\" value=\"Submit\">\n"
+                           ⊕ "</form>\n"
+            )
+          , ( POST
+            , do form ← getForm Nothing
+                 let text     = fromMaybe (∅) $ fdContent <$> lookup "text" form
+                     file     = fromMaybe (∅) $ fdContent <$> lookup "file" form
+                     fileName = fdFileName =≪ lookup "file" form
+                 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"
+            )
+          ]
index b9b76a3919e7c12f7930c08b2714f5d47d568637..23de8b04062c4d6b48b80b2b525ae913aab7610c 100644 (file)
@@ -5,9 +5,10 @@
   , UnicodeSyntax
   #-}
 import Control.Applicative
-import "mtl" Control.Monad.Trans
+import Control.Monad.IO.Class
 import Control.Monad.Unicode
 import qualified Data.ByteString.Lazy.Char8 as Lazy
+import qualified Data.Collections as C
 import Data.Time.Clock
 import Network.HTTP.Lucu
 import OpenSSL
@@ -26,29 +27,31 @@ main = withOpenSSL $
           SSL.contextSetCertificate    ctx cert
           SSL.contextSetDefaultCiphers ctx
 
-          let config    = defaultConfig {
-                            cnfServerPort = "9000"
-                          , cnfSSLConfig  = Just SSLConfig {
-                                              sslServerPort = "9001"
-                                            , sslContext    = ctx
-                                            }
-                          }
-              resources = mkResTree [ ([], helloWorld) ]
+          let config = defaultConfig {
+                         cnfServerPort = "9000"
+                       , cnfSSLConfig  = Just SSLConfig {
+                                           sslServerPort = "9001"
+                                         , sslContext    = ctx
+                                         }
+                       }
+              tree   ∷ ResourceTree
+              tree   = C.fromList [ ([], nonGreedy helloWorld) ]
           putStrLn "Access https://localhost:9001/ with your browser."
-          runHttpd config resources []
+          runHttpd config $ resourceMap tree
 
-helloWorld ∷ ResourceDef
+helloWorld ∷ Resource
 helloWorld 
-    = emptyResource {
-        resGet
-          = Just $ do setContentType [mimeType| text/plain |]
-                      putChunk "getRemoteCertificate = "
-                      cert ← do cert ← getRemoteCertificate
-                                case cert of
-                                  Just c  → liftIO $ Lazy.pack <$> printX509 c
-                                  Nothing → return "Nothing"
-                      putChunks cert
-      }
+    = C.fromList
+      [ ( GET
+        , do setContentType [mimeType| text/plain |]
+             putChunk "getRemoteCertificate = "
+             cert ← do cert ← getRemoteCertificate
+                       case cert of
+                         Just c  → liftIO $ Lazy.pack <$> printX509 c
+                         Nothing → return "Nothing"
+             putChunks cert
+        )
+      ]
 
 genCert ∷ KeyPair k ⇒ k → IO X509
 genCert pkey
@@ -61,4 +64,4 @@ genCert pkey
          setNotAfter     cert =≪ addUTCTime (365 * 24 * 60 * 60) <$> getCurrentTime
          setPublicKey    cert pkey
          signX509        cert pkey Nothing
-         return cert
\ No newline at end of file
+         return cert