on cb75ed4 help sign in
33 files changed+513−74
since splitsnapshots
unifiedsplit
.gitignore+5−5 scripts/bump-versions.sh+8−21 scripts/lib/common.sh+32 scripts/start-sandbox.sh+1−5 scripts/test-all.sh+2−6 scripts/update-defaults-nix.sh+1−5 scripts/upload.sh+52 servant-blaze/servant-blaze.cabal+5−1 servant-client/servant-client.cabal+1−1 servant-docs/CHANGELOG.md+4 servant-docs/servant-docs.cabal+2−1 servant-docs/src/Servant/Docs/Internal.hs+1−1 servant-docs/test/Servant/DocsSpec.hs+26−3 servant-examples/CHANGELOG.md+3 servant-examples/servant-examples.cabal+5−1 servant-examples/tutorial/T4.hs+7−7 servant-jquery/CHANGELOG.md+4 servant-jquery/servant-jquery.cabal+1−1 servant-jquery/src/Servant/JQuery.hs+3−2 servant-lucid/servant-lucid.cabal+5−1 servant-mock/LICENSE+30 servant-mock/Setup.hs+2 servant-mock/default.nix+17 servant-mock/example/main.hs+23 servant-mock/servant-mock.cabal+46 servant-mock/shell.nix+18 servant-mock/src/Servant/Mock.hs+189 servant-server/README.md+3−3 servant-server/servant-server.cabal+6−4 servant/CHANGELOG.md+4 servant/servant.cabal+2−5 servant/src/Servant/API/ContentTypes.hs+1−1 servant/src/Servant/Utils/Links.hs+4
.gitignore+5−5
@@ -1,8 +1,8 @@
1−dist
2−bin
3−lib
4−share
5−packages
1+/dist
2+/bin
3+/lib
4+/share
5+/packages
66*-packages.conf.d
77cabal-dev
88add-source-timestamps
scripts/bump-versions.sh+8−21
@@ -16,33 +16,17 @@
1616set -o nounset
1717set -o errexit
1818
19−DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
20−DRY_RUN=false
21−POSITION="none"
22−SOURCES_TXT="$( dirname $DIR)/sources.txt"
23−
24−declare -a SOURCES
25−readarray -t SOURCES < "$SOURCES_TXT"
19+DIR=$( dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))
20+. "${DIR}/scripts/lib/common.sh"
2621
2722usage () {
28− echo " bump-versions <POSITION> [-d|--dry-run]"
23+ echo " bump-versions.sh <POSITION> [-d|--dry-run]"
2924 echo " | [-h|--help]"
3025 echo " Bumps the specified positional version of all servant packages."
3126 echo " POSITION is a number between 0 and 3, inclusive."
3227 exit 0
3328}
3429
35−join () { local IFS="$1"; shift; echo "$*"; }
36−
37−versions_equal () {
38− local NUM=$(find . -name 'servant*.cabal' | xargs grep "^version:" | awk '{ print $2 }' | uniq -c | wc -l)
39− if [ 1 -eq $NUM ] ; then
40− return 0
41− else
42− echo "versions of packages are not all the same!" && exit 1
43− fi
44−}
45−
4630if [ $# -eq 0 ] ; then
4731 echo "expecting one or more arguments. Got 0"
4832 usage
@@ -71,8 +55,11 @@ while [ "${1:-unset}" != "unset" ] ; do
7155done
7256
7357if $DRY_RUN ; then
74− bumper --dry-run -"$POSITION" $(join , "${SOURCES[@]}")
58+ echo "Would have bumped position ${POSITION} on these packages:"
59+ ( cd "$ROOT" && bumper --dry-run -"$POSITION" $(join , "${SOURCES[@]}") )
7560else
76− bumper -"$POSITION" $(join , "${SOURCES[@]}")
61+ ( cd "$ROOT" && bumper -"$POSITION" $(join , "${SOURCES[@]}") )
7762fi
7863
64+# Trailing newline, bumper does not ship with its own.
65+echo
scripts/lib/common.shnew file+32
@@ -0,0 +1,32 @@
1+#!/bin/bash -
2+#===============================================================================
3+#
4+# FILE: lib/common.sh
5+#
6+# DESCRIPTION: Common functions for servant's shell scripts
7+# Meant to be sourced rather than run.
8+#
9+# REQUIREMENTS: bash >= 4
10+#===============================================================================
11+
12+
13+DIR=$( dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))
14+ROOT=$( dirname $DIR )
15+DRY_RUN=false
16+POSITION="none"
17+SOURCES_TXT="$( dirname $DIR)/sources.txt"
18+CABAL=${CABAL:-cabal}
19+
20+declare -a SOURCES
21+readarray -t SOURCES < "$SOURCES_TXT"
22+
23+join () { local IFS="$1"; shift; echo "$*"; }
24+
25+versions_equal () {
26+ local NUM=$(cd "$ROOT" && find . -name 'servant*.cabal' | xargs grep "^version:" | awk '{ print $2 }' | uniq -c | wc -l)
27+ if [ 1 -eq $NUM ] ; then
28+ return 0
29+ else
30+ echo "versions of packages are not all the same!" && exit 1
31+ fi
32+}
scripts/start-sandbox.sh+1−5
@@ -14,11 +14,7 @@ set -o nounset
1414set -o errexit
1515
1616DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
17−SOURCES_TXT="$( dirname $DIR)/sources.txt"
18−CABAL=${CABAL:-cabal}
19−
20−declare -a SOURCES
21−readarray -t SOURCES < "$SOURCES_TXT"
17+. "$DIR"/lib/common.sh
2218
2319prepare_sandbox () {
2420 $CABAL sandbox init
scripts/test-all.sh+2−6
@@ -15,13 +15,9 @@ set -o nounset
1515set -o errexit
1616
1717DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
18−GHC_FLAGS="-Werror"
19−SOURCES_TXT="$( dirname $DIR)/sources.txt"
20−CABAL=${CABAL:-cabal}
21−
22−declare -a SOURCES
23−readarray -t SOURCES < "$SOURCES_TXT"
18+. "$DIR"/lib/common.sh
2419
20+GHC_FLAGS="-Werror"
2521
2622prepare_sandbox () {
2723 $CABAL sandbox init
scripts/update-defaults-nix.sh+1−5
@@ -12,11 +12,7 @@ set -o nounset
1212set -o errexit
1313
1414DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
15−BASE_DIR="$( dirname $DIR)"
16−SOURCES_TXT="$BASE_DIR/sources.txt"
17−
18−declare -a SOURCES
19−readarray -t SOURCES < "$SOURCES_TXT"
15+. "$DIR"/lib/common.sh
2016
2117for s in ${SOURCES[@]} ; do
2218 echo $s
scripts/upload.shnew file+52
@@ -0,0 +1,52 @@
1+#!/bin/bash -
2+#===============================================================================
3+#
4+# FILE: upload.sh
5+#
6+# USAGE: ./upload.sh <USER> <PASSWORD>
7+#
8+# DESCRIPTION: Uploads all servant packages to Hackage
9+#
10+# REQUIREMENTS: cabal, bash >= 4
11+# AUTHOR: Julian K. Arni
12+# CREATED: 05.06.2015 13:05
13+#===============================================================================
14+
15+set -o nounset
16+set -o errexit
17+
18+DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
19+. "$DIR"/lib/common.sh
20+
21+usage () {
22+ echo " upload.sh <USER> <PASSWORD>"
23+ echo " Uploads all servant packages to Hackage"
24+ exit 0
25+}
26+
27+
28+upload_package () {
29+ local package="$1"
30+ local user="$2"
31+ local pass="$3"
32+ local cabalFile="$package.cabal"
33+ pushd "$package"
34+ local version=$(grep -i '^version:' $cabalFile | awk '{ print $2 }')
35+ local sdist="dist/${package}-${version}.tar.gz"
36+ cabal sdist
37+ echo "User is: $user"
38+ cabal upload --user="$user" --password="$pass" "$sdist"
39+ popd
40+}
41+
42+
43+if [ $# -ne 2 ] ; then
44+ echo "expecting two arguments."
45+ usage
46+fi
47+
48+versions_equal
49+
50+for s in ${SOURCES[@]} ; do
51+ upload_package "$s" "$1" "$2"
52+done
servant-blaze/servant-blaze.cabal+5−1
@@ -2,7 +2,7 @@
22-- documentation, see http://haskell.org/cabal/users-guide/
33
44name: servant-blaze
5−version: 0.4.1
5+version: 0.4.4.2
66synopsis: Blaze-html support for servant
77-- description:
88homepage: http://haskell-servant.github.io/
@@ -15,6 +15,10 @@ category: Web
1515build-type: Simple
1616-- extra-source-files:
1717cabal-version: >=1.10
18+bug-reports: http://github.com/haskell-servant/servant/issues
19+source-repository head
20+ type: git
21+ location: http://github.com/haskell-servant/servant.git
1822
1923library
2024 exposed-modules: Servant.HTML.Blaze
servant-client/servant-client.cabal+1−1
@@ -1,5 +1,5 @@
11name: servant-client
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: automatical derivation of querying functions for servant webservices
44description:
55 This library lets you derive automatically Haskell functions that
servant-docs/CHANGELOG.md+4
@@ -1,3 +1,7 @@
1+0.4.3
2+-----
3+* docsWith will no longer eat your documentation (https://github.com/haskell-servant/servant/pull/124)
4+
150.4
26---
37* `Delete` now is like `Get`, `Post`, `Put`, and `Patch` and returns a response body
servant-docs/servant-docs.cabal+2−1
@@ -1,5 +1,5 @@
11name: servant-docs
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: generate API docs for your servant webservice
44description:
55 Library for generating API docs from a servant API definition.
@@ -70,6 +70,7 @@ test-suite spec
7070 base
7171 , aeson
7272 , hspec
73+ , lens
7374 , servant
7475 , servant-docs
7576 , string-conversions
servant-docs/src/Servant/Docs/Internal.hs+1−1
@@ -338,7 +338,7 @@ extraInfo p action =
338338docsWith :: HasDocs layout => [DocIntro] -> ExtraInfo layout -> Proxy layout -> API
339339docsWith intros (ExtraInfo endpoints) p =
340340 docs p & apiIntros <>~ intros
341− & apiEndpoints %~ HM.unionWith combineAction endpoints
341+ & apiEndpoints %~ HM.unionWith (flip combineAction) endpoints
342342
343343
344344-- | Generate the docs for a given API that implements 'HasDocs' with with any
servant-docs/test/Servant/DocsSpec.hs+26−3
@@ -7,7 +7,9 @@
77{-# OPTIONS_GHC -fno-warn-orphans #-}
88module Servant.DocsSpec where
99
10+import Control.Lens
1011import Data.Aeson
12+import Data.Monoid
1113import Data.Proxy
1214import Data.String.Conversions (cs)
1315import GHC.Generics
@@ -21,7 +23,27 @@ spec = describe "Servant.Docs" $ do
2123
2224 describe "markdown" $ do
2325 let md = markdown (docs (Proxy :: Proxy TestApi1))
24−
26+ tests md
27+
28+ describe "markdown with extra info" $ do
29+ let
30+ extra = extraInfo
31+ (Proxy :: Proxy (Get '[JSON, PlainText] Int))
32+ (defAction & notes <>~ [DocNote "Get an Integer" ["get an integer in Json or plain text"]])
33+ <>
34+ extraInfo
35+ (Proxy :: Proxy (ReqBody '[JSON] String :> Post '[JSON] Datatype1))
36+ (defAction & notes <>~ [DocNote "Post data" ["Posts some Json data"]])
37+ md = markdown (docsWith [] extra (Proxy :: Proxy TestApi1))
38+ tests md
39+ it "contains the extra info provided" $ do
40+ md `shouldContain` "Get an Integer"
41+ md `shouldContain` "Post data"
42+ md `shouldContain` "get an integer in Json or plain text"
43+ md `shouldContain` "Posts some Json data"
44+
45+ where
46+ tests md = do
2547 it "mentions supported content-types" $ do
2648 md `shouldContain` "application/json"
2749 md `shouldContain` "text/plain;charset=utf-8"
@@ -34,10 +56,11 @@ spec = describe "Servant.Docs" $ do
3456 md `shouldContain` "POST"
3557 md `shouldContain` "GET"
3658
37− it "contains response samples" $ do
59+ it "contains response samples" $
3860 md `shouldContain` "{\"dt1field1\":\"field 1\",\"dt1field2\":13}"
39− it "contains request body samples" $ do
61+ it "contains request body samples" $
4062 md `shouldContain` "17"
63+
4164-- * APIs
4265
4366data Datatype1 = Datatype1 { dt1field1 :: String
servant-examples/CHANGELOG.mdnew file+3
@@ -0,0 +1,3 @@
1+0.4.3
2+-----
3+* Clarify some variable names in the examples + semantic html pedantry
servant-examples/servant-examples.cabal+5−1
@@ -1,5 +1,5 @@
11name: servant-examples
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: Example programs for servant
44description: Example programs for servant,
55 showcasing solutions to common needs.
@@ -12,6 +12,10 @@ maintainer: alpmestan@gmail.com
1212category: Web
1313build-type: Simple
1414cabal-version: >=1.10
15+bug-reports: http://github.com/haskell-servant/servant/issues
16+source-repository head
17+ type: git
18+ location: http://github.com/haskell-servant/servant.git
1519
1620executable tutorial
1721 main-is: tutorial.hs
servant-examples/tutorial/T4.hs+7−7
@@ -25,11 +25,11 @@ instance ToJSON Person
2525
2626-- HTML serialization of a single person
2727instance ToHtml Person where
28− toHtml p =
28+ toHtml person =
2929 tr_ $ do
30− td_ (toHtml $ firstName p)
31− td_ (toHtml $ lastName p)
32− td_ (toHtml . show $ age p)
30+ td_ (toHtml $ firstName person)
31+ td_ (toHtml $ lastName person)
32+ td_ (toHtml . show $ age person)
3333
3434 toHtmlRaw = toHtml
3535
@@ -37,9 +37,9 @@ instance ToHtml Person where
3737instance ToHtml [Person] where
3838 toHtml persons = table_ $ do
3939 tr_ $ do
40− td_ "first name"
41− td_ "last name"
42− td_ "age"
40+ th_ "first name"
41+ th_ "last name"
42+ th_ "age"
4343
4444 foldMap toHtml persons
4545
servant-jquery/CHANGELOG.md+4
@@ -1,3 +1,7 @@
1+0.4.3
2+-----
3+* Content type now set to JSON when a request body is sent (#122)
4+
150.4
26---
37* `Delete` now is like `Get`, `Post`, `Put`, and `Patch` and returns a response body
servant-jquery/servant-jquery.cabal+1−1
@@ -1,5 +1,5 @@
11name: servant-jquery
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: Automatically derive (jquery) javascript functions to query servant webservices
44description:
55 Automatically derive jquery-based javascript functions to query servant webservices.
servant-jquery/src/Servant/JQuery.hs+3−2
@@ -65,13 +65,14 @@ generateJS req = "\n" <>
6565
6666 dataBody =
6767 if req ^. reqBody
68− then "\n , data: JSON.stringify(body)\n"
68+ then " , data: JSON.stringify(body)\n" <>
69+ " , contentType: 'application/json'\n"
6970 else ""
7071
7172 reqheaders =
7273 if null hs
7374 then ""
74− else "\n , headers: { " ++ headersStr ++ " }\n"
75+ else " , headers: { " ++ headersStr ++ " }\n"
7576
7677 where headersStr = intercalate ", " $ map headerStr hs
7778 headerStr header = "\"" ++
servant-lucid/servant-lucid.cabal+5−1
@@ -2,7 +2,7 @@
22-- documentation, see http://haskell.org/cabal/users-guide/
33
44name: servant-lucid
5−version: 0.4.1
5+version: 0.4.4.2
66synopsis: Servant support for lucid
77-- description:
88homepage: http://haskell-servant.github.io/
@@ -15,6 +15,10 @@ category: Web
1515build-type: Simple
1616-- extra-source-files:
1717cabal-version: >=1.10
18+bug-reports: http://github.com/haskell-servant/servant/issues
19+source-repository head
20+ type: git
21+ location: http://github.com/haskell-servant/servant.git
1822
1923library
2024 exposed-modules: Servant.HTML.Lucid
servant-mock/LICENSEnew file+30
@@ -0,0 +1,30 @@
1+Copyright (c) 2015, Alp Mestanogullari
2+
3+All rights reserved.
4+
5+Redistribution and use in source and binary forms, with or without
6+modification, are permitted provided that the following conditions are met:
7+
8+ * Redistributions of source code must retain the above copyright
9+ notice, this list of conditions and the following disclaimer.
10+
11+ * Redistributions in binary form must reproduce the above
12+ copyright notice, this list of conditions and the following
13+ disclaimer in the documentation and/or other materials provided
14+ with the distribution.
15+
16+ * Neither the name of Alp Mestanogullari nor the names of other
17+ contributors may be used to endorse or promote products derived
18+ from this software without specific prior written permission.
19+
20+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
servant-mock/Setup.hsnew file+2
@@ -0,0 +1,2 @@
1+import Distribution.Simple
2+main = defaultMain
servant-mock/default.nixnew file+17
@@ -0,0 +1,17 @@
1+{ mkDerivation, aeson, base, bytestring, http-types, QuickCheck
2+, servant, servant-server, stdenv, transformers, wai, warp
3+}:
4+mkDerivation {
5+ pname = "servant-mock";
6+ version = "0.5";
7+ src = ./.;
8+ isLibrary = true;
9+ isExecutable = true;
10+ buildDepends = [
11+ aeson base bytestring http-types QuickCheck servant servant-server
12+ transformers wai warp
13+ ];
14+ homepage = "http://github.com/haskell-servant/servant";
15+ description = "Derive a mock server for free from your servant API types";
16+ license = stdenv.lib.licenses.bsd3;
17+}
servant-mock/example/main.hsnew file+23
@@ -0,0 +1,23 @@
1+{-# LANGUAGE DataKinds #-}
2+{-# LANGUAGE DeriveGeneric #-}
3+{-# LANGUAGE GeneralizedNewtypeDeriving #-}
4+{-# LANGUAGE TypeOperators #-}
5+import Data.Aeson
6+import GHC.Generics
7+import Network.Wai.Handler.Warp
8+import Servant
9+import Servant.Mock
10+import Test.QuickCheck.Arbitrary
11+
12+newtype User = User { username :: String }
13+ deriving (Eq, Show, Arbitrary, Generic)
14+
15+instance ToJSON User
16+
17+type API = "user" :> Get '[JSON] User
18+
19+api :: Proxy API
20+api = Proxy
21+
22+main :: IO ()
23+main = run 8080 (serve api $ mock api)
servant-mock/servant-mock.cabalnew file+46
@@ -0,0 +1,46 @@
1+name: servant-mock
2+version: 0.5
3+synopsis: Derive a mock server for free from your servant API types
4+description:
5+ Derive a mock server for free from your servant API types
6+ .
7+ See the @Servant.Mock@ module for the documentation and an example.
8+homepage: http://github.com/haskell-servant/servant
9+license: BSD3
10+license-file: LICENSE
11+author: Alp Mestanogullari
12+maintainer: alpmestan@gmail.com
13+copyright: 2015 Alp Mestanogullari
14+category: Web
15+build-type: Simple
16+cabal-version: >=1.10
17+
18+flag example
19+ description: Build the example too
20+ manual: True
21+ default: False
22+
23+library
24+ exposed-modules:
25+ Servant.Mock
26+ build-depends:
27+ base >=4.7 && <5,
28+ bytestring >= 0.10 && <0.11,
29+ http-types >= 0.8 && <0.9,
30+ servant >= 0.4,
31+ servant-server >= 0.4,
32+ transformers >= 0.3 && <0.5,
33+ QuickCheck >= 2.8 && <2.9,
34+ wai >= 3.0 && <3.1
35+ hs-source-dirs: src
36+ default-language: Haskell2010
37+
38+executable mock-app
39+ main-is: main.hs
40+ hs-source-dirs: example
41+ default-language: Haskell2010
42+ build-depends: aeson, base, servant-mock, servant-server, QuickCheck, warp
43+ if flag(example)
44+ buildable: True
45+ else
46+ buildable: False
servant-mock/shell.nixnew file+18
@@ -0,0 +1,18 @@
1+let
2+ pkgs = import <nixpkgs> {};
3+
4+ haskellPackages = pkgs.haskellPackages.override {
5+ overrides = self: super: {
6+ servant = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant {}) "--ghc-options=-Werror");
7+ servant-client = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-client {}) "--ghc-options=-Werror");
8+ servant-docs = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-docs {}) "--ghc-options=-Werror");
9+ servant-js = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-js {}) "--ghc-options=-Werror");
10+ servant-server = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-server {}) "--ghc-options=-Werror");
11+ servant-examples = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-examples {}) "--ghc-options=-Werror");
12+ servant-blaze = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-blaze {}) "--ghc-options=-Werror");
13+ servant-lucid = pkgs.haskell.lib.dontCheck (pkgs.haskell.lib.appendConfigureFlag (self.callPackage ../servant-lucid {}) "--ghc-options=-Werror");
14+ servant-mock = pkgs.haskell.lib.appendConfigureFlag (self.callPackage ./. {}) "--ghc-options=-Werror";
15+ };
16+ };
17+
18+in haskellPackages.servant-mock.env
servant-mock/src/Servant/Mock.hsnew file+189
@@ -0,0 +1,189 @@
1+{-# LANGUAGE CPP #-}
2+{-# LANGUAGE DataKinds #-}
3+{-# LANGUAGE FlexibleContexts #-}
4+{-# LANGUAGE FlexibleInstances #-}
5+{-# LANGUAGE ScopedTypeVariables #-}
6+{-# LANGUAGE TypeFamilies #-}
7+{-# LANGUAGE TypeOperators #-}
8+-- |
9+-- Module : Servant.Mock
10+-- Copyright : 2015 Alp Mestanogullari
11+-- License : BSD3
12+--
13+-- Maintainer : Alp Mestanogullari <alpmestan@gmail.com>
14+-- Stability : experimental
15+-- Portability : portable
16+--
17+-- Automatically derive a mock webserver that implements some API type,
18+-- just from the said API type's definition.
19+--
20+-- Using this module couldn't be simpler. Given some API type, like:
21+--
22+-- > type API = "user" :> Get '[JSON] User
23+--
24+-- that describes your web application, all you have to do is define
25+-- a 'Proxy' to it:
26+--
27+-- > myAPI :: Proxy API
28+-- > myAPI = Proxy
29+--
30+-- and call 'mock', which has the following type:
31+--
32+-- @
33+-- 'mock' :: 'HasMock' api => 'Proxy' api -> 'Server' api
34+-- @
35+--
36+-- What this says is, given some API type @api@ that it knows it can
37+-- "mock", 'mock' hands you an implementation of the API type. It does so
38+-- by having each request handler generate a random value of the
39+-- appropriate type (@User@ in our case). All you need for this to work is
40+-- to provide 'Arbitrary' instances for the data types returned as response
41+-- bodies, hence appearing next to 'Delete', 'Get', 'Patch', 'Post' and 'Put'.
42+--
43+-- To put this all to work and run the mock server, just call 'serve' on the
44+-- result of 'mock' to get an 'Application' that you can then run with warp.
45+--
46+-- @
47+-- main :: IO ()
48+-- main = Network.Wai.Handler.Warp.run 8080 $
49+-- 'serve' myAPI ('mock' myAPI)
50+-- @
51+module Servant.Mock ( HasMock(..) ) where
52+
53+#if !MIN_VERSION_base(4,8,0)
54+import Control.Applicative
55+#endif
56+import Control.Monad.IO.Class
57+import Data.ByteString.Lazy.Char8 (pack)
58+import Data.Proxy
59+import GHC.TypeLits
60+import Network.HTTP.Types.Status
61+import Network.Wai
62+import Servant
63+import Servant.API.ContentTypes
64+import Test.QuickCheck.Arbitrary (Arbitrary (..), vector)
65+import Test.QuickCheck.Gen (Gen, generate)
66+
67+-- | 'HasMock' defines an interpretation of API types
68+-- than turns them into random-response-generating
69+-- request handlers, hence providing an instance for
70+-- all the combinators of the core /servant/ library.
71+class HasServer api => HasMock api where
72+ -- | Calling this method creates request handlers of
73+ -- the right type to implement the API described by
74+ -- @api@ that just generate random response values of
75+ -- the right type. E.g:
76+ --
77+ -- @
78+ -- type API = "user" :> Get '[JSON] User
79+ -- :<|> "book" :> Get '[JSON] Book
80+ --
81+ -- api :: Proxy API
82+ -- api = Proxy
83+ --
84+ -- -- let's say we will start with the frontend,
85+ -- -- and hence need a placeholder server
86+ -- server :: Server API
87+ -- server = mock api
88+ -- @
89+ --
90+ -- What happens here is that @'Server' API@
91+ -- actually "means" 2 request handlers, of the following types:
92+ --
93+ -- @
94+ -- getUser :: EitherT ServantErr IO User
95+ -- getBook :: EitherT ServantErr IO Book
96+ -- @
97+ --
98+ -- So under the hood, 'mock' uses the 'IO' bit to generate
99+ -- random values of type 'User' and 'Book' every time these
100+ -- endpoints are requested.
101+ mock :: Proxy api -> Server api
102+
103+instance (HasMock a, HasMock b) => HasMock (a :<|> b) where
104+ mock _ = mock (Proxy :: Proxy a) :<|> mock (Proxy :: Proxy b)
105+
106+instance (KnownSymbol path, HasMock rest) => HasMock (path :> rest) where
107+ mock _ = mock (Proxy :: Proxy rest)
108+
109+instance (KnownSymbol s, FromText a, HasMock rest) => HasMock (Capture s a :> rest) where
110+ mock _ = \_ -> mock (Proxy :: Proxy rest)
111+
112+instance (AllCTUnrender ctypes a, HasMock rest) => HasMock (ReqBody ctypes a :> rest) where
113+ mock _ = \_ -> mock (Proxy :: Proxy rest)
114+
115+instance HasMock rest => HasMock (RemoteHost :> rest) where
116+ mock _ = \_ -> mock (Proxy :: Proxy rest)
117+
118+instance HasMock rest => HasMock (IsSecure :> rest) where
119+ mock _ = \_ -> mock (Proxy :: Proxy rest)
120+
121+instance HasMock rest => HasMock (Vault :> rest) where
122+ mock _ = \_ -> mock (Proxy :: Proxy rest)
123+
124+instance HasMock rest => HasMock (HttpVersion :> rest) where
125+ mock _ = \_ -> mock (Proxy :: Proxy rest)
126+
127+instance (KnownSymbol s, FromText a, HasMock rest)
128+ => HasMock (QueryParam s a :> rest) where
129+ mock _ = \_ -> mock (Proxy :: Proxy rest)
130+
131+instance (KnownSymbol s, FromText a, HasMock rest)
132+ => HasMock (QueryParams s a :> rest) where
133+ mock _ = \_ -> mock (Proxy :: Proxy rest)
134+
135+instance (KnownSymbol s, HasMock rest) => HasMock (QueryFlag s :> rest) where
136+ mock _ = \_ -> mock (Proxy :: Proxy rest)
137+
138+instance (KnownSymbol s, FromText a, HasMock rest)
139+ => HasMock (MatrixParam s a :> rest) where
140+ mock _ = \_ -> mock (Proxy :: Proxy rest)
141+
142+instance (KnownSymbol s, FromText a, HasMock rest)
143+ => HasMock (MatrixParams s a :> rest) where
144+ mock _ = \_ -> mock (Proxy :: Proxy rest)
145+
146+instance (KnownSymbol s, HasMock rest) => HasMock (MatrixFlag s :> rest) where
147+ mock _ = \_ -> mock (Proxy :: Proxy rest)
148+
149+instance (KnownSymbol h, FromText a, HasMock rest) => HasMock (Header h a :> rest) where
150+ mock _ = \_ -> mock (Proxy :: Proxy rest)
151+
152+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Delete ctypes a) where
153+ mock _ = mockArbitrary
154+
155+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Get ctypes a) where
156+ mock _ = mockArbitrary
157+
158+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Patch ctypes a) where
159+ mock _ = mockArbitrary
160+
161+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Post ctypes a) where
162+ mock _ = mockArbitrary
163+
164+instance (Arbitrary a, AllCTRender ctypes a) => HasMock (Put ctypes a) where
165+ mock _ = mockArbitrary
166+
167+instance HasMock Raw where
168+ mock _ = \req respond -> do
169+ bdy <- genBody
170+ respond $ responseLBS status200 [] bdy
171+
172+ where genBody = pack <$> generate (vector 100 :: Gen [Char])
173+
174+mockArbitrary :: (MonadIO m, Arbitrary a) => m a
175+mockArbitrary = liftIO (generate arbitrary)
176+
177+-- utility instance
178+instance (Arbitrary (HList ls), Arbitrary a)
179+ => Arbitrary (Headers ls a) where
180+ arbitrary = Headers <$> arbitrary <*> arbitrary
181+
182+instance Arbitrary (HList '[]) where
183+ arbitrary = pure HNil
184+
185+instance (Arbitrary a, Arbitrary (HList hs))
186+ => Arbitrary (HList (Header h a ': hs)) where
187+ arbitrary = HCons <$> fmap Header arbitrary <*> arbitrary
188+
189+
servant-server/README.md+3−3
@@ -4,13 +4,13 @@
44
55This library lets you *implement* an HTTP server with handlers for each endpoint of a servant API, handling most of the boilerplate for you.
66
7−## Getting started
7+## Tutorial
88
9−We've written a [Getting Started](http://haskell-servant.github.io/getting-started/) guide that introduces the core types and features of servant. After this article, you should be able to write your first servant webservices, learning the rest from the haddocks' examples.
9+We've written a [tutorial](http://haskell-servant.github.io/tutorial/) that introduces the core types and features of servant.
1010
1111## Repositories and Haddocks
1212
13−- The core [servant](http://github.com/haskell-servant) package - [docs](http://hackage.haskell.org/package/servant)
13+- The core [servant](http://github.com/haskell-servant/servant/tree/master/servant) package - [docs](http://hackage.haskell.org/package/servant)
1414- Implementing an HTTP server for a webservice API with [servant-server](http://github.com/haskell-servant/servant/tree/master/servant-server) - [docs](http://hackage.haskell.org/package/servant-server)
1515- (Haskell) client-side function generation with [servant-client](http://github.com/haskell-servant/servant/tree/master/servant-client) - [docs](http://hackage.haskell.org/package/servant-client)
1616- (Javascript) client-side function generation with [servant-jquery](http://github.com/haskell-servant/servant/tree/master/servant-jquery) - [docs](http://hackage.haskell.org/package/servant-jquery)
servant-server/servant-server.cabal+6−4
@@ -1,5 +1,5 @@
11name: servant-server
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: A family of combinators for defining webservices APIs and serving them
44description:
55 A family of combinators for defining webservices APIs and serving them
@@ -25,9 +25,10 @@ tested-with: GHC >= 7.8
2525extra-source-files:
2626 CHANGELOG.md
2727 README.md
28+bug-reports: http://github.com/haskell-servant/servant/issues
2829source-repository head
2930 type: git
30− location: http://github.com/haskell-servant/servant-server.git
31+ location: http://github.com/haskell-servant/servant.git
3132
3233
3334library
@@ -51,14 +52,15 @@ library
5152 , safe >= 0.3 && < 0.4
5253 , servant == 0.4.*
5354 , split >= 0.2 && < 0.3
54− , string-conversions >= 0.3 && < 0.4
55+ , string-conversions >= 0.3 && < 0.5
5556 , system-filepath >= 0.4 && < 0.5
5657 , filepath >= 1
5758 , text >= 1.2 && < 1.3
5859 , transformers >= 0.3 && < 0.5
5960 , wai >= 3.0 && < 3.1
6061 , wai-app-static >= 3.0 && < 3.2
61− , warp >= 3.0 && < 3.1
62+ , warp >= 3.0 && < 3.2
63+
6264 hs-source-dirs: src
6365 default-language: Haskell2010
6466 ghc-options: -Wall
servant/CHANGELOG.md+4
@@ -1,3 +1,7 @@
1+0.4.3
2+-----
3+* Add missing HasLink instance for Header (https://github.com/haskell-servant/servant/issues/128)
4+
150.4.1
26-----
37* Allow whitespace after parsing JSON
servant/servant.cabal+2−5
@@ -1,14 +1,11 @@
11name: servant
2−version: 0.4.1
2+version: 0.4.4.2
33synopsis: A family of combinators for defining webservices APIs
44description:
55 A family of combinators for defining webservices APIs and serving them
66 .
77 You can learn about the basics in the <http://haskell-servant.github.io/tutorial tutorial>.
88 .
9− <https://github.com/haskell-servant/servant-server/blob/master/example/greet.hs Here>'s a runnable example, with comments, that defines a dummy API and
10− implements a webserver that serves this API, using the <http://hackage.haskell.org/package/servant-server servant-server> package.
11− .
129 <https://github.com/haskell-servant/servant/blob/master/servant/CHANGELOG.md CHANGELOG>
1310homepage: http://haskell-servant.github.io/
1411Bug-reports: http://github.com/haskell-servant/servant/issues
@@ -55,7 +52,7 @@ library
5552 , http-media >= 0.4 && < 0.7
5653 , http-types == 0.8.*
5754 , text >= 1 && < 2
58− , string-conversions >= 0.3 && < 0.4
55+ , string-conversions >= 0.3 && < 0.5
5956 , network-uri >= 2.6
6057 hs-source-dirs: src
6158 default-language: Haskell2010
servant/src/Servant/API/ContentTypes.hs+1−1
@@ -166,7 +166,7 @@ instance ( AllMimeRender ctyps a, IsNonEmpty ctyps
166166 handleAcceptH _ (AcceptHeader accept) val = M.mapAcceptMedia lkup accept
167167 where pctyps = Proxy :: Proxy ctyps
168168 amrs = allMimeRender pctyps val
169− lkup = fmap (\(a,b) -> (a, (cs $ show a, b))) amrs
169+ lkup = fmap (\(a,b) -> (a, (fromStrict $ M.renderHeader a, b))) amrs
170170
171171
172172--------------------------------------------------------------------------
servant/src/Servant/Utils/Links.hs+4
@@ -339,6 +339,10 @@ instance (ToText v, HasLink sub)
339339 toLink (Proxy :: Proxy sub) $
340340 addSegment (escape . unpack $ toText v) l
341341
342+instance HasLink sub => HasLink (Header sym a :> sub) where
343+ type MkLink (Header sym a :> sub) = MkLink sub
344+ toLink _ = toLink (Proxy :: Proxy sub)
345+
342346-- Verb (terminal) instances
343347instance HasLink (Get y r) where
344348 type MkLink (Get y r) = URI