git.iko.soy/typeable/servant
on 8ee9b60 help sign in
typeable/servant/README.md
97 lines 3.0 kB history raw
1# servant-jquery
3[![Build Status](https://secure.travis-ci.org/haskell-servant/servant-jquery.svg)](http://travis-ci.org/haskell-servant/servant-jquery)
4[![Coverage Status](https://coveralls.io/repos/haskell-servant/servant-jquery/badge.svg)](https://coveralls.io/r/haskell-servant/servant-jquery)
6![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)
8This library lets you derive automatically (JQuery based) Javascript functions that let you query each endpoint of a *servant* webservice.
10## Example
12Read more about the following example [here](https://github.com/haskell-servant/servant-jquery/tree/master/examples#examples).
14``` haskell
15{-# LANGUAGE DataKinds #-}
16{-# LANGUAGE TypeOperators #-}
17{-# LANGUAGE DeriveGeneric #-}
18{-# LANGUAGE GeneralizedNewtypeDeriving #-}
20import Control.Concurrent.STM
21import Control.Monad.IO.Class
22import Data.Aeson
23import Data.Proxy
24import GHC.Generics
25import Network.Wai.Handler.Warp (run)
26import Servant
27import Servant.JQuery
28import System.FilePath
30-- * A simple Counter data type
31newtype Counter = Counter { value :: Int }
32 deriving (Generic, Show, Num)
34instance ToJSON Counter
36-- * Shared counter operations
38-- Creating a counter that starts from 0
39newCounter :: IO (TVar Counter)
40newCounter = newTVarIO 0
42-- Increasing the counter by 1
43counterPlusOne :: MonadIO m => TVar Counter -> m Counter
44counterPlusOne counter = liftIO . atomically $ do
45 oldValue <- readTVar counter
46 let newValue = oldValue + 1
47 writeTVar counter newValue
48 return newValue
50currentValue :: MonadIO m => TVar Counter -> m Counter
51currentValue counter = liftIO $ readTVarIO counter
53-- * Our API type
54type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
55 :<|> "counter" :> Get Counter -- endpoint to get the current value
56 :<|> Raw -- used for serving static files
58testApi :: Proxy TestApi
59testApi = Proxy
61-- * Server-side handler
63-- where our static files reside
64www :: FilePath
65www = "examples/www"
67-- defining handlers
68server :: TVar Counter -> Server TestApi
69server counter = counterPlusOne counter -- (+1) on the TVar
70 :<|> currentValue counter -- read the TVar
71 :<|> serveDirectory www -- serve static files
73runServer :: TVar Counter -- ^ shared variable for the counter
74 -> Int -- ^ port the server should listen on
75 -> IO ()
76runServer var port = run port (serve testApi $ server var)
78-- * Generating the JQuery code
80incCounterJS :<|> currentValueJS :<|> _ = jquery testApi
82writeJS :: FilePath -> [AjaxReq] -> IO ()
83writeJS fp functions = writeFile fp $
84 concatMap generateJS functions
86main :: IO ()
87main = do
88 -- write the JS code to www/api.js at startup
89 writeJS (www </> "api.js")
90 [ incCounterJS, currentValueJS ]
92 -- setup a shared counter
93 cnt <- newCounter
95 -- listen to requests on port 8080
96 runServer cnt 8080
97```