packages feed

loli (empty) → 2009.6.25

raw patch · 12 files changed

+295/−0 lines, 12 filesdep +basedep +data-defaultdep +hacksetup-changed

Dependencies added: base, data-default, hack, hack-contrib, mps, mtl, utf8-string

Files

+ LICENSE view
@@ -0,0 +1,9 @@+Copyright (c) 2009, Jinjing Wang+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Nemesis view
@@ -0,0 +1,25 @@+nemesis = do+  +  clean+    [ "**/*.hi"+    , "**/*.o"+    , "manifest"+    ]+    +  desc "prepare cabal dist"+  task "dist" $ do+    sh "cabal clean"+    sh "cabal configure"+    sh "cabal sdist"++  desc "start console"+  task "i" (sh "ghci -isrc src/Test.hs")++  desc "put all .hs files in manifest"+  task "manifest" $ do+    sh "find . | grep 'hs$' > manifest"++  task "test" $ do+    sh "ghc --make -isrc src/Test.hs -o Test"+    sh "echo done.."+    sh "./Test"
+ Setup.lhs view
@@ -0,0 +1,4 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> main = defaultMain
+ changelog.md view
@@ -0,0 +1,4 @@+2009.5.22+---------++Init
+ known-issues.md view
+ loli.cabal view
@@ -0,0 +1,28 @@+Name:                 loli+Version:              2009.6.25+Build-type:           Simple+Synopsis:             loli+Description:+        +    A minimum web dev DSL in Haskell++License:              BSD3+License-file:         LICENSE+Author:               Wang, Jinjing+Maintainer:           Wang, Jinjing <nfjinjing@gmail.com>+Build-Depends:        base+Cabal-version:        >= 1.2+category:             Web+homepage:             http://github.com/nfjinjing/loli+data-files:           readme.md, changelog.md, Nemesis, known-issues.md, src/Test.hs++library+  ghc-options: -Wall+  build-depends: base > 4 && <= 5, data-default, hack >= 2009.5.19, hack-contrib >= 2009.6.25, utf8-string, mps >= 2009.6.25, mtl+  hs-source-dirs: src/+  exposed-modules:  +                      Network.Loli+  other-modules:+                      Network.Loli.Config+                      Network.Loli.DSL+                      Network.Loli.Engine
+ readme.md view
@@ -0,0 +1,13 @@+### Example++    import Network.Loli+    import Hack.Handler.Happstack++    main = run . loli $ do++      get "/hello" (text "hello world")+      get "/" (html "<html><body><p>loli power!</p></body></html>")++      public (Just ".") ["/src"]++      mime "hs" "text/plain"
+ src/Network/Loli.hs view
@@ -0,0 +1,9 @@+module Network.Loli+(+    module Network.Loli.DSL+  , module Network.Loli.Engine+)+where++import Network.Loli.Engine+import Network.Loli.DSL
+ src/Network/Loli/Config.hs view
@@ -0,0 +1,15 @@+module Network.Loli.Config where++import Hack+import Hack.Contrib.Middleware.ContentLength+import Hack.Contrib.Middleware.ContentType++pre_installed_middlewares :: [Middleware]+pre_installed_middlewares = +  [+    content_length+  , content_type default_content_type+  ]+  where+    default_content_type :: String+    default_content_type = "text/plain; charset=UTF-8"
+ src/Network/Loli/DSL.hs view
@@ -0,0 +1,39 @@+module Network.Loli.DSL where+++import MPS+import Prelude hiding ((.), (>), (^))+import Network.Loli.Engine+import Hack+import Hack.Contrib.Constants+import Hack.Contrib.Response+import Data.ByteString.Lazy.UTF8 (fromString)+import Hack.Contrib.Middleware.Static++app :: Application -> AppUnit+app = set_application > update++text :: String -> AppUnit+text x = do+  response $ set_content_type _TextPlain+  response $ set_body (x.fromString)++html :: String -> AppUnit+html x = do+  response $ set_content_type _TextHtml+  response $ set_body (x.fromString)++get, put, delete, post :: String -> AppUnit -> Unit+get    = route GET+put    = route PUT+delete = route DELETE+post   = route POST++middleware :: Middleware -> Unit+middleware x = add_middleware x .update++mime :: String -> String -> Unit+mime k v = add_mime k v .update++public :: Maybe String -> [String] -> Unit+public r xs = middleware $ static r xs
+ src/Network/Loli/Engine.hs view
@@ -0,0 +1,138 @@+{-# LANGUAGE NamedFieldPuns #-}++module Network.Loli.Engine where++import Control.Monad.State+import Data.Default+import Data.List (find)+import Hack+import Hack.Contrib.Middleware.Censor+import Hack.Contrib.Middleware.Config+import Hack.Contrib.Middleware.NotFound+import Hack.Contrib.Response+import Hack.Contrib.Utils hiding (get, put)+import MPS+import Network.Loli.Config+import Prelude hiding ((.), (/), (>), (^))+++type RoutePath = (RequestMethod, String, AppUnit)+type EnvFilter = Env -> Env+type ResponseFilter = Response -> Response+type Param = (String, String)+data AppState = AppState+  {+    application :: Application+  , env_filters :: [EnvFilter]+  , response_filters :: [ResponseFilter]+  , path :: String+  }++instance Default AppState where+  def = AppState def [id] [id] def++type AppUnit = State AppState ()++run_app :: String -> AppUnit -> Application+run_app path unit = +  let state = execState unit def {path}+      before = state.env_filters.map config+      after = state.response_filters.map (to_io_filter > censor)+  in+  state.application.use (before ++ after)+  where+    to_io_filter f = \x -> return (f x)++router :: [RoutePath] -> Middleware+router h app' = \env'' ->+  let path             = env''.path_info+      script           = env''.script_name+      mod_env location = env'' +        { scriptName  = script ++ location+        , pathInfo    = path.drop (location.length)+        }+  in+  case h.find (match_route env'') of+    Nothing -> app' env''+    Just (_, location, app_state) -> +      run_app location app_state (mod_env location)+  where+    match_route env' (method, path, _) = +      env'.request_method.is method && env'.path_info.starts_with path+++data Loli = Loli+  {+    routes :: [RoutePath]+  , middlewares :: [Middleware]+  , mimes :: [(String, String)]+  }++instance Default Loli where+  def = Loli def def def++type Unit = State Loli ()++++loli :: Unit -> Application+loli unit = run unit (not_found empty_app)+  where+    run :: Unit -> Middleware+    run unit' = +      let s           = execState unit' def+          paths       = s.routes+          +          loli_app    = router paths+          mime_filter = lookup_mime (s.mimes)+          stack       = s.middlewares.use+          pre         = pre_installed_middlewares.use+      in+      use [pre, mime_filter, stack, loli_app]++set_application :: Application -> AppState -> AppState+set_application application x = x { application }++update :: (MonadState a m, Functor m) => (a -> a) -> m ()+update f = get ^ f >>= put++insert_last :: a -> [a] -> [a]+insert_last x xs = xs ++ [x]++add_route :: RoutePath -> Loli -> Loli+add_route r s = let xs = s.routes in s {routes = xs.insert_last r}++route :: RequestMethod -> String -> AppUnit -> Unit+route r s u = update $ add_route (r, s, u)++add_middleware :: Middleware -> Loli -> Loli+add_middleware x s = +  let xs = s.middlewares in s {middlewares = xs.insert_last x}++add_mime :: String -> String -> Loli -> Loli+add_mime k v s = let xs = s.mimes in s {mimes = xs.insert_last (k, v)}++add_env_filter :: EnvFilter -> AppState -> AppState+add_env_filter x s = +  let xs = s.env_filters in s {env_filters = xs.insert_last x}++add_response_filter :: ResponseFilter -> AppState -> AppState+add_response_filter x s = +  let xs = s.response_filters in s {response_filters = xs.insert_last x}+++request :: EnvFilter-> AppUnit+request x = add_env_filter x .update++response :: ResponseFilter -> AppUnit+response x = add_response_filter x .update+++-- middleware+lookup_mime :: [(String, String)] -> Middleware+lookup_mime h app env = do+  r <- app env+  case h.only_fst.find mime >>= flip lookup h of+    Nothing -> return r+    Just v -> return $ r.set_content_type v+  where mime x = env.path_info.ends_with ('.' : x)
+ src/Test.hs view
@@ -0,0 +1,11 @@+import Network.Loli+import Hack.Handler.Happstack++main = run . loli $ do+  +  get "/hello" (text "hello world")+  get "/" (html "<html><body><p>loli power!</p></body></html>")++  public (Just ".") ["/src"]+    +  mime "hs" "text/plain"