No description
Find a file
2015-01-21 18:27:25 +11:00
examples first shot at generating function names automatically 2014-12-01 17:36:18 +01:00
src/Servant Extended HeaderArg to allow header arguments to replace the contents of a template, allowing specially composed headers to create specially formatted headers 2015-01-21 18:27:25 +11:00
test Extended HeaderArg to allow header arguments to replace the contents of a template, allowing specially composed headers to create specially formatted headers 2015-01-21 18:27:25 +11:00
.travis.yml travis: build with ghc 7.8 and clone servant from git before building 2014-12-02 17:50:44 +01:00
CHANGELOG.md add a changelog, bump version to 0.2.2 2015-01-04 17:41:51 +01:00
docs.sh adapt to the servant/servant-server split, prepare new release 2014-12-10 16:29:50 +01:00
LICENSE add LICENSE files to all projects 2014-12-01 16:38:43 +01:00
README.md polish up cabal file, add README 2014-12-08 12:01:56 +01:00
servant-jquery.cabal Extended HeaderArg to allow header arguments to replace the contents of a template, allowing specially composed headers to create specially formatted headers 2015-01-21 18:27:25 +11:00
Setup.hs first shot for jquery codegen based on a servant API type 2014-11-25 01:36:34 +01:00
TODO.md first shot for jquery codegen based on a servant API type 2014-11-25 01:36:34 +01:00

servant-jquery

Build Status

servant

This library lets you derive automatically (JQuery based) Javascript functions that let you query each endpoint of a servant webservice.

Example

Read more about the following example here.

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

import Control.Concurrent.STM
import Control.Monad.IO.Class
import Data.Aeson
import Data.Proxy
import GHC.Generics
import Network.Wai.Handler.Warp (run)
import Servant
import Servant.JQuery
import System.FilePath

-- * A simple Counter data type
newtype Counter = Counter { value :: Int }
  deriving (Generic, Show, Num)

instance ToJSON Counter

-- * Shared counter operations

-- Creating a counter that starts from 0
newCounter :: IO (TVar Counter)
newCounter = newTVarIO 0

-- Increasing the counter by 1
counterPlusOne :: MonadIO m => TVar Counter -> m Counter
counterPlusOne counter = liftIO . atomically $ do
  oldValue <- readTVar counter
  let newValue = oldValue + 1
  writeTVar counter newValue
  return newValue

currentValue :: MonadIO m => TVar Counter -> m Counter
currentValue counter = liftIO $ readTVarIO counter

-- * Our API type
type TestApi = "counter" :> Post Counter -- endpoint for increasing the counter
          :<|> "counter" :> Get  Counter -- endpoint to get the current value
          :<|> Raw                       -- used for serving static files 

testApi :: Proxy TestApi
testApi = Proxy

-- * Server-side handler

-- where our static files reside
www :: FilePath
www = "examples/www"

-- defining handlers
server :: TVar Counter -> Server TestApi
server counter = counterPlusOne counter     -- (+1) on the TVar
            :<|> currentValue counter       -- read the TVar
            :<|> serveDirectory www         -- serve static files

runServer :: TVar Counter -- ^ shared variable for the counter
          -> Int          -- ^ port the server should listen on
          -> IO ()
runServer var port = run port (serve testApi $ server var)

-- * Generating the JQuery code

incCounterJS :<|> currentValueJS :<|> _ = jquery testApi

writeJS :: FilePath -> [AjaxReq] -> IO ()
writeJS fp functions = writeFile fp $
  concatMap generateJS functions

main :: IO ()
main = do
  -- write the JS code to www/api.js at startup
  writeJS (www </> "api.js")
          [ incCounterJS, currentValueJS ]

  -- setup a shared counter
  cnt <- newCounter

  -- listen to requests on port 8080
  runServer cnt 8080