jmacro-rpc-happstack (empty) → 0.1
raw patch · 4 files changed
+150/−0 lines, 4 filesdep +basedep +bytestringdep +containerssetup-changed
Dependencies added: base, bytestring, containers, happstack-server, jmacro, jmacro-rpc, mtl
Files
- LICENSE +30/−0
- Network/JMacroRPC/Happstack.hs +89/−0
- Setup.hs +2/−0
- jmacro-rpc-happstack.cabal +29/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2011, Gershom Bazerman++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 Gershom Bazerman nor the names of other+ 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+OWNER 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.
+ Network/JMacroRPC/Happstack.hs view
@@ -0,0 +1,89 @@+{-# LANGUAGE ScopedTypeVariables, QuasiQuotes, RankNTypes, FlexibleContexts #-}++{- |+Module : Network.JMacroRPC.Happstack+Copyright : (c) Gershom Bazerman, 2012+License : BSD 3 Clause+Maintainer : gershomb@gmail.com+Stability : experimental++Happstack backend for JMacro-RPC.++Example usage:++> {-# LANGUAGE QuasiQuotes #-}+> module Main where+> import Network.JMacroRPC.Happstack+> import Happstack.Server+> import Language.Javascript.JMacro+> import Control.Concurrent+> import Network.JMacroRPC.Base+> import Text.XHtml hiding(dir)+> +> jsScript f = script (primHtml f) ! [thetype "text/javascript"]+> jsScript' = jsScript . show . renderJs+> +> testPage :: IO (ServerPartT IO Response)+> testPage = mkConversationPageNoCulling pageFun (newMVar (1::Int)) jRpcs+> where pageFun js = return $ toResponse $ +> (header << [script ! [src "https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"] << noHtml]) ++++> jsScript' js ++++> jsScript' ([jmacro|$(\ +> { +> var b = $("<button>click me!</button>");+> $("body").append(b);+> b.click(\ {+> var c = getCounter();+> alert ("counter is: " + c);+> });+> });+> |]);+> jRpcs = [getCounterRPC]+> getCounterRPC = +> toJsonConvRPC "getCounter" $ \s -> retRight =<< modifyMVar s (\i -> return (i+1,i))+>+> retRight :: a -> IO (Either String a)+> retRight = return . Right+>+> main = simpleHTTP nullConf =<< testPage++Every invocation of this page (including from the same browser) will have a distinct, stateful, counter, stored server-side.++-}++module Network.JMacroRPC.Happstack where+ +import Prelude hiding (tail, init, head, last, minimum, maximum, foldr1, foldl1, (!!), read)+import Control.Applicative+import Control.Monad.Trans+import Data.Maybe+import Data.Monoid+import Network.JMacroRPC.Base+import Happstack.Server+import Language.Javascript.JMacro+import qualified Data.ByteString.Char8 as B+import qualified Data.ByteString.Lazy.Char8 as BL+import Data.IntMap(IntMap)++serveRpcs :: (MonadIO m, Functor m, Monad m) => (Int -> m s) -> [JsonRPC m s] -> ServerPartT m Response+serveRpcs stateFun rpcs = do+ rq <- fromMaybe (BL.empty) . fmap unBody <$> (takeRequestBody =<< askRq)+ toResponseBS (B.pack "application/json") <$> lift (handleRpcs stateFun rpcs rq)++serveSimpleRpcs :: (MonadIO m, Functor m, Monad m) => [JsonRPC m ()] -> ServerPartT m Response+serveSimpleRpcs rpcs = serveRpcs (const $ return ()) rpcs++-- | This general handler allows explicit culling of conversation state.+mkConversationPage :: forall m s timestamp. (Monad m, Functor m, MonadIO m) => + IO timestamp -- ^ Get an abstract timestamp + -> (IntMap (timestamp,s) -> IO (IntMap (timestamp,s))) -- ^ Cull a map of conversations based on a timestamp+ -> (JStat -> ServerPartT m Response) -- ^ Take some JMacro Javascript and splice it into some generated page. + -> IO s -- ^ Generate an empty "initial" state for a conversation. States are responsible for using their own MVars or the like to allow sharing.+ -> [JsonRPC m s] -- ^ JSON RPCs to serve and make available to clientside javascript+ -> IO (ServerPartT m Response)+mkConversationPage getStamp cullMap pageFun emptyState rpcs = (\(rpcPage, mainPage) -> dir "jrpcs" rpcPage `mappend` mainPage) <$> mkConversationPageGen getStamp cullMap serveRpcs pageFun emptyState rpcs++-- | This simple handler allows conversation state to grow without bounds.+mkConversationPageNoCulling :: forall m s. (Monad m, Functor m, MonadIO m) => (JStat -> ServerPartT m Response) -> IO s -> [JsonRPC m s] -> IO (ServerPartT m Response)+mkConversationPageNoCulling pageFun emptyState rpcs = mkConversationPage (return ()) return pageFun emptyState rpcs+
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ jmacro-rpc-happstack.cabal view
@@ -0,0 +1,29 @@+Name: jmacro-rpc-happstack+Version: 0.1+Homepage: http://patch-tag.com/r/gershomb/jmacro-rpc+License: BSD3+License-file: LICENSE+Author: Gershom Bazerman+Maintainer: gershomb@gmail.com+Category: Network+Build-type: Simple+Cabal-version: >=1.6+Synopsis: Happstack backend for jmacro-rpc+Description: Provides functions for serving jmacro-rpc json rpcs from Happstack.++Library+ Exposed-modules: Network.JMacroRPC.Happstack+ + Build-depends: base >= 4, base < 7, bytestring > 0.9, jmacro > 0.5, happstack-server > 6, jmacro-rpc >= 0.1, mtl >= 2, containers >= 0.4++ ghc-options: -Wall ++ -- Modules not exported by this package.+ -- Other-modules: + + -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools: + +source-repository head+ type: darcs+ location: http://patch-tag.com/r/gershomb/jmacro-rpc