The servant-jquery package

[ Tags: bsd3, deprecated, library, web ] [ Propose Tags ]
Deprecated. in favor of servant-js

Automatically derive jquery-based javascript functions to query servant webservices.

You can find an example here which serves the generated javascript to a webpage that allows you to trigger webservice calls.

CHANGELOG


[Skip to Readme]

Properties

Versions 0.2, 0.2.1, 0.2.2, 0.2.2.1, 0.4.0, 0.4.1, 0.4.2, 0.4.3, 0.4.3.1, 0.4.4, 0.4.4.2, 0.4.4.3, 0.4.4.4, 0.4.4.5, 0.4.4.6, 0.4.4.7
Change log CHANGELOG.md
Dependencies aeson, base (>=4.5 && <5), charset, filepath, lens (>=4), servant (==0.4.*), servant-jquery (==0.4.*), servant-server (==0.4.*), stm, text, transformers, warp [details]
License BSD3
Copyright 2014 Alp Mestanogullari
Author Alp Mestanogullari
Maintainer alpmestan@gmail.com
Category Web
Home page http://haskell-servant.github.io/
Bug tracker http://github.com/haskell-servant/servant/issues
Source repository head: git clone http://github.com/haskell-servant/servant.git
Uploaded Thu Mar 17 13:38:57 UTC 2016 by SoenkeHahn
Distributions NixOS:0.4.4.7
Executables counter
Downloads 2009 total (18 in the last 30 days)
Rating 2.0 (1 ratings) [clear rating]
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2016-03-17 [all 1 reports]
Hackage Matrix CI

Modules

[Index]

Flags

NameDescriptionDefaultType
example

Build the example too

DisabledManual

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for servant-jquery-0.4.4.7

[back to package description]

servant-jquery

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