Skip to content

Commit

Permalink
Replace servant-jquery with servant-js in remaining files
Browse files Browse the repository at this point in the history
  • Loading branch information
freezeboy committed Nov 2, 2015
1 parent 27aea39 commit a90d390
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
39 changes: 22 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

![servant](https://raw.githubusercontent.com/haskell-servant/servant/master/servant.png)

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

It contains a powerful system allowing you to generate functions for several frameworks (Angular, AXios, JQuery) as well as
vanilla (framework-free) javascript code.

## Example

Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-jquery/tree/master/examples#examples).
Read more about the following example [here](https://github.com/haskell-servant/servant/tree/master/servant-js/tree/master/examples#examples).

``` haskell
{-# LANGUAGE DataKinds #-}
Expand All @@ -21,7 +24,7 @@ import Data.Proxy
import GHC.Generics
import Network.Wai.Handler.Warp (run)
import Servant
import Servant.JQuery
import Servant.JS
import System.FilePath

-- * A simple Counter data type
Expand All @@ -48,13 +51,21 @@ 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
type TestApi = "counter" :> Post '[JSON] Counter -- endpoint for increasing the counter
:<|> "counter" :> Get '[JSON] Counter -- endpoint to get the current value

type TestApi' = TestApi -- The API we want a JS handler for
:<|> Raw -- used for serving static files

-- this proxy only targets the proper endpoints of our API,
-- not the static file serving bit
testApi :: Proxy TestApi
testApi = Proxy

-- this proxy targets everything
testApi' :: Proxy TestApi'
testApi' = Proxy

-- * Server-side handler

-- where our static files reside
Expand All @@ -65,26 +76,20 @@ www = "examples/www"
server :: TVar Counter -> Server TestApi
server counter = counterPlusOne counter -- (+1) on the TVar
:<|> currentValue counter -- read the TVar

server' :: TVar Counter -> Server TestApi'
server counter = server counter
:<|> 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
runServer var port = run port (serve testApi' $ server' var)

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

-- setup a shared counter
cnt <- newCounter
Expand Down
6 changes: 3 additions & 3 deletions docs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
SERVANT_DIR=/tmp/servant-jquery-gh-pages
SERVANT_DIR=/tmp/servant-js-gh-pages

# Make a temporary clone

Expand All @@ -10,7 +10,7 @@ cd $SERVANT_DIR

# Make sure to pull the latest

git remote add haskell-servant [email protected]:haskell-servant/servant-jquery.git
git remote add haskell-servant [email protected]:haskell-servant/servant-js.git

git fetch haskell-servant

Expand All @@ -36,7 +36,7 @@ cd $SERVANT_DIR

rm *
rm -rf build
mv doc/html/servant-jquery/* .
mv doc/html/servant-js/* .
rm -r doc/

# Add everything
Expand Down
8 changes: 4 additions & 4 deletions servant-js.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ name: servant-js
version: 0.5
synopsis: Automatically derive javascript functions to query servant webservices.
description:
Automatically derive jquery-based javascript functions to query servant webservices.
Automatically derive javascript functions to query servant webservices.
.
Supports deriving functions using vanilla javascript AJAX requests, Angular or JQuery.
Supports deriving functions using vanilla javascript AJAX requests, Angulari, Axios or JQuery.
.
You can find an example <https://github.com/haskell-servant/servant/blob/master/servant-jquery/examples/counter.hs here>
You can find an example <https://github.com/haskell-servant/servant/blob/master/servant-js/examples/counter.hs here>
which serves the generated javascript to a webpage that allows you to trigger
webservice calls.
.
<https://github.com/haskell-servant/servant/blob/master/servant-jquery/CHANGELOG.md CHANGELOG>
<https://github.com/haskell-servant/servant/blob/master/servant-js/CHANGELOG.md CHANGELOG>
license: BSD3
license-file: LICENSE
author: Alp Mestanogullari
Expand Down

0 comments on commit a90d390

Please sign in to comment.