Last active
August 29, 2015 14:23
-
-
Save cdepillabout/c2b8b1807e1f571fdb45 to your computer and use it in GitHub Desktop.
notes on some of the difficult concepts used in servant (to be expanded into blog post)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dist | |
cabal-dev | |
*.o | |
*.hi | |
*.chi | |
*.chs.h | |
*.dyn_o | |
*.dyn_hi | |
.hpc | |
.hsenv | |
.cabal-sandbox/ | |
cabal.sandbox.config | |
*.prof | |
*.aux | |
*.hp | |
.stack-work/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE DataKinds #-} | |
{-# LANGUAGE TypeOperators #-} | |
module Main where | |
import Control.Monad.Trans.Either (EitherT) | |
import Network.Wai (Application) | |
import Network.Wai.Handler.Warp (run) | |
import Network.Wai.Middleware.RequestLogger (logStdoutDev) | |
import Servant | |
( (:>), (:<|>)((:<|>)), Get, JSON, Proxy(..), ServantErr, ServerT, serve ) | |
-- | A representation of our REST API at the type level. | |
-- | |
-- This defines two routes: | |
-- * /dogs -- Responds to HTTP GET with a list of integers in JSON format. | |
-- * /cats -- Responds to HTTP GET with a list of Strings in JSON format. | |
type MyAPI = "dogs" :> Get '[JSON] [Int] | |
:<|> "cats" :> Get '[JSON] [String] | |
-- | A Warp 'Application' that will serve our API. | |
app :: Application | |
app = serve (Proxy :: Proxy MyAPI) myAPI | |
-- | Our entire API. You can see that it is a combination of the 'dogNums' | |
-- handler and the 'cats' handler. | |
-- One way of writing the type. | |
-- myAPI :: EitherT ServantErr IO [Int] | |
-- :<|> EitherT ServantErr IO [String] | |
-- | |
-- Another way of writing the type. | |
-- myAPI :: ServerT (Get '[JSON] [Int]) (EitherT ServantErr IO) | |
-- :<|> ServerT (Get '[JSON] [String]) (EitherT ServantErr IO) | |
-- | |
-- The shortest way of writing the type. | |
myAPI :: ServerT MyAPI (EitherT ServantErr IO) | |
myAPI = dogNums :<|> cats | |
-- | A handler for the @/dogs@ route. It just returns a list of the integers | |
-- one to four. | |
dogNums :: EitherT ServantErr IO [Int] | |
dogNums = return [1,2,3,4] | |
-- | A handler for the @/cats@ route. | |
cats :: EitherT ServantErr IO [String] | |
cats = return ["long-haired", "short-haired"] | |
-- | Run our 'app' as a Warp 'Application'. | |
main :: IO () | |
main = run 32323 $ logStdoutDev app | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2015, (cdep) illabout | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without | |
modification, are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright | |
notice, this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above | |
copyright notice, this list of conditions and the following | |
disclaimer in the documentation and/or other materials provided | |
with the distribution. | |
* Neither the name of (cdep) illabout nor the names of other | |
contributors may be used to endorse or promote products derived | |
from this software without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Initial servant-notes.cabal generated by cabal init. For further | |
-- documentation, see http://haskell.org/cabal/users-guide/ | |
name: servant-notes | |
version: 0.1.0.0 | |
-- synopsis: | |
-- description: | |
license: BSD3 | |
license-file: LICENSE | |
author: (cdep) illabout | |
maintainer: [email protected] | |
-- copyright: | |
-- category: | |
build-type: Simple | |
-- extra-source-files: | |
cabal-version: >=1.10 | |
executable servant-notes | |
main-is: example.hs | |
-- other-modules: | |
-- other-extensions: | |
build-depends: base >=4.7 && <5 | |
, either >= 4.3 && < 4.4 | |
, mtl >= 2.1 && < 2.3 | |
, servant >= 0.4 && < 0.5 | |
, servant-server >= 0.4 && < 0.5 | |
, wai >= 3 && < 4 | |
, wai-extra >= 3 && < 4 | |
, warp >= 3 && < 4 | |
ghc-options: -Wall | |
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Distribution.Simple | |
main = defaultMain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
flags: {} | |
packages: | |
- '.' | |
extra-deps: | |
- servant-0.4.2 | |
- servant-server-0.4.2 | |
# resolver: lts-2.14 | |
resolver: nightly-2015-06-17 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment