@@ -1,8 +1,94 @@
1name: servant-jquery
1name: servant-jquery
2version: 0.2
2version: 0.2
3synopsis: Automatically derive jquery-based javascript functions to query servant webservices
3synopsis: Automatically derive jquery-based javascript functions to query servant webservices
4−description: Automatically derive jquery-based javascript functions to query servant webservices
5+ Automatically derive jquery-based javascript functions to query servant webservices.
5−homepage: http://github.com/alpmestan/servant
6+ .
7+ Example below that serves the generated javascript to a webpage that lets you
8+ trigger webservice calls.
10+ > {-# LANGUAGE DataKinds #-}
11+ > {-# LANGUAGE TypeOperators #-}
12+ > {-# LANGUAGE DeriveGeneric #-}
13+ > {-# LANGUAGE GeneralizedNewtypeDeriving #-}
15+ > import Control.Concurrent.STM
16+ > import Control.Monad.IO.Class
19+ > import GHC.Generics
20+ > import Network.Wai.Handler.Warp (run)
22+ > import Servant.JQuery
23+ > import System.FilePath
25+ > -- * A simple Counter data type
26+ > newtype Counter = Counter { value :: Int }
27+ > deriving (Generic, Show, Num)
29+ > instance ToJSON Counter
31+ > -- * Shared counter operations
33+ > -- Creating a counter that starts from 0
34+ > newCounter :: IO (TVar Counter)
35+ > newCounter = newTVarIO 0
37+ > -- Increasing the counter by 1
38+ > counterPlusOne :: MonadIO m => TVar Counter -> m Counter
39+ > counterPlusOne counter = liftIO . atomically $ do
40+ > oldValue <- readTVar counter
41+ > let newValue = oldValue + 1
42+ > writeTVar counter newValue
45+ > currentValue :: MonadIO m => TVar Counter -> m Counter
46+ > currentValue counter = liftIO $ readTVarIO counter
49+ > type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
50+ > :<|> "counter" :> Get Counter -- endpoint to get the current value
51+ > :<|> Raw -- used for serving static files
53+ > testApi :: Proxy TestApi
56+ > -- * Server-side handler
58+ > -- where our static files reside
60+ > www = "examples/www"
62+ > -- defining handlers
63+ > server :: TVar Counter -> Server TestApi
64+ > server counter = counterPlusOne counter -- (+1) on the TVar
65+ > :<|> currentValue counter -- read the TVar
66+ > :<|> serveDirectory www -- serve static files
68+ > runServer :: TVar Counter -- ^ shared variable for the counter
69+ > -> Int -- ^ port the server should listen on
71+ > runServer var port = run port (serve testApi $ server var)
73+ > -- * Generating the JQuery code
75+ > incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
77+ > writeJS :: FilePath -> [AjaxReq] -> IO ()
78+ > writeJS fp functions = writeFile fp $
79+ > concatMap generateJS functions
83+ > -- write the JS code to www/api.js at startup
84+ > writeJS (www </> "api.js")
85+ > [ incCounterJS, currentValueJS ]
87+ > -- setup a shared counter
90+ > -- listen to requests on port 8080
6license: BSD3
92license: BSD3
7license-file: LICENSE
93license-file: LICENSE
8author: Alp Mestanogullari
94author: Alp Mestanogullari
@@ -11,15 +97,19 @@ copyright: 2014 Alp Mestanogullari
11category: Web
97category: Web
12build-type: Simple
98build-type: Simple
13cabal-version: >=1.10
99cabal-version: >=1.10
100+homepage: http://haskell-servant.github.io/
101+Bug-reports: http://github.com/haskell-servant/servant-jquery/issues
102+source-repository head
104+ location: http://github.com/haskell-servant/servant-jquery.git
16 exposed-modules: Servant.JQuery
107 exposed-modules: Servant.JQuery
17 other-modules: Servant.JQuery.Internal
108 other-modules: Servant.JQuery.Internal
19 build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4
109 build-depends: base >=4.5 && <5, servant >= 0.2, lens >= 4
20 hs-source-dirs: src
110 hs-source-dirs: src
21 default-language: Haskell2010
111 default-language: Haskell2010
22− ghc-options: -O2 -Wall
112+ ghc-options: -Wall
24executable counter
114executable counter
25 main-is: counter.hs
115 main-is: counter.hs