Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

init

  • Loading branch information...
commit 7b66e8e071ab5032a5d05f04bae68b98224d5a9a 0 parents
@nfjinjing authored
24 .gitignore
@@ -0,0 +1,24 @@
+*.swp
+*.swo
+*.o
+*.hi
+.searchpath.default.map
+*.pid
+*.fcgi
+
+*.exe
+
+*.log
+
+/public
+/db
+
+/_darcs
+/dist
+
+manifest
+hello
+Main
+
+.nemesis
+Test
9 LICENSE
@@ -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.
25 Nemesis
@@ -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"
4 Setup.lhs
@@ -0,0 +1,4 @@
+#! /usr/bin/env runhaskell
+
+> import Distribution.Simple
+> main = defaultMain
4 changelog.md
@@ -0,0 +1,4 @@
+2009.5.22
+---------
+
+Init
0  known-issues.md
No changes.
32 loli.cabal
@@ -0,0 +1,32 @@
+Name: loli
+Version: 2009.6.25
+Build-type: Simple
+Synopsis: loli
+Description:
+
+ A minimum web development DSL
+
+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
+
+library
+ ghc-options: -Wall
+ build-depends: base
+ hs-source-dirs: src/
+ exposed-modules:
+ Template
+
+Executable launcher
+ ghc-options: -Wall
+ build-depends: base
+ hs-source-dirs: src/
+ main-is: Launcher.hs
+ other-modules:
+ Template.Other
1  readme.md
@@ -0,0 +1 @@
+readme
9 src/Network/Loli.hs
@@ -0,0 +1,9 @@
+module Network.Loli
+(
+ module Network.Loli.DSL
+ , module Network.Loli.Engine
+)
+where
+
+import Network.Loli.Engine
+import Network.Loli.DSL
39 src/Network/Loli/DSL.hs
@@ -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.Utils hiding (use, get)
+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 = 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
137 src/Network/Loli/Engine.hs
@@ -0,0 +1,137 @@
+{-# LANGUAGE NamedFieldPuns #-}
+
+module Network.Loli.Engine where
+
+import Control.Monad.State
+import Data.Default
+import Data.List (find)
+import Data.Maybe
+import Hack
+import Hack.Contrib.Middleware.Censor
+import Hack.Contrib.Middleware.Config
+import Hack.Contrib.Middleware.NotFound
+import Hack.Contrib.Utils hiding (get, put)
+import Hack.Contrib.Response
+import MPS
+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
+ in
+ use [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)
37 src/Test.hs
@@ -0,0 +1,37 @@
+import Network.Loli
+import Hack.Handler.Happstack
+import Hack
+import Data.ByteString.Lazy.Char8 (pack)
+import Data.Default
+import Hack.Contrib.Middleware.BounceFavicon
+import Hack.Contrib.Middleware.ContentLength
+import Hack.Contrib.Middleware.ContentType
+-- import Hack.Contrib.Middleware.ETag
+-- import Hack.Contrib.Middleware.ShowExceptions
+import Hack.Contrib.Middleware.Static
+import Hack.Contrib.Middleware.URLMap
+import Hack.Contrib.Utils (use)
+
+default_content_type :: String
+default_content_type = "text/plain; charset=UTF-8"
+
+stack =
+ [
+ content_length
+ , content_type default_content_type
+ ]
+
+main = run my_app
+
+my_app = use stack loli_app
+
+hello_app :: Application
+hello_app = const $ return $ def {body = pack "hello world"}
+
+loli_app = loli $ do
+ public Nothing ["/src"]
+
+ get "/hello" (text "hello")
+ get "/" (html "<html><body><p>world</p></body></html>")
+
+ mime "hs" "text/plain"
35 src/Test.txt
@@ -0,0 +1,35 @@
+import Network.Loli
+import Hack.Handler.Happstack
+import Hack
+import Data.ByteString.Lazy.Char8 (pack)
+import Data.Default
+import Hack.Contrib.Middleware.BounceFavicon
+import Hack.Contrib.Middleware.ContentLength
+import Hack.Contrib.Middleware.ContentType
+-- import Hack.Contrib.Middleware.ETag
+-- import Hack.Contrib.Middleware.ShowExceptions
+import Hack.Contrib.Middleware.Static
+import Hack.Contrib.Middleware.URLMap
+import Hack.Contrib.Utils (use)
+
+default_content_type :: String
+default_content_type = "text/plain; charset=UTF-8"
+
+stack =
+ [
+ content_length
+ , content_type default_content_type
+ ]
+
+main = run my_app
+
+my_app = use stack loli_app
+
+hello_app :: Application
+hello_app = const $ return $ def {body = pack "hello world"}
+
+loli_app = loli $ do
+ public Nothing ["/src"]
+
+ get "/hello" (text "hello")
+ get "/" (html "<html><body><p>world</p></body></html>")
Please sign in to comment.
Something went wrong with that request. Please try again.