Skip to content

Instantly share code, notes, and snippets.

@cdepillabout
Last active August 29, 2015 14:23
Show Gist options
  • Save cdepillabout/c2b8b1807e1f571fdb45 to your computer and use it in GitHub Desktop.
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)
dist
cabal-dev
*.o
*.hi
*.chi
*.chs.h
*.dyn_o
*.dyn_hi
.hpc
.hsenv
.cabal-sandbox/
cabal.sandbox.config
*.prof
*.aux
*.hp
.stack-work/
{-# 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
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.
-- 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: cdep.illabout@gmail.com
-- 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
import Distribution.Simple
main = defaultMain
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