Skip to content

Instantly share code, notes, and snippets.

@prutz1311
Created April 13, 2018 20:50
Show Gist options
  • Save prutz1311/bc70e293fa9bb91f116e960704d79b10 to your computer and use it in GitHub Desktop.
Save prutz1311/bc70e293fa9bb91f116e960704d79b10 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-#LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE TypeFamilies #-}
module Main where
import Servant.API
import Servant.Server
import Control.Concurrent.STM
import Data.Aeson (FromJSON, ToJSON)
import Data.Proxy
import Control.Monad.IO.Class
import Network.Wai
import Network.Wai.Handler.Warp (run)
newtype CounterVal = CounterVal {getCounterVal :: Int}
deriving (Show, Num, FromJSON, ToJSON)
type GetCounter = Get '[JSON] CounterVal
type StepCounter = "step" :> Post '[] NoContent
type Counter = GetCounter :<|> StepCounter
counterAPI :: Proxy Counter
counterAPI = Proxy
handleGetCounter :: TVar CounterVal -> Server GetCounter
handleGetCounter ctr = liftIO $ readTVarIO ctr
handleStepCounter :: TVar CounterVal -> Server StepCounter
handleStepCounter ctr = do
liftIO $ atomically $ modifyTVar ctr (+ 1)
return NoContent
handleCounter :: TVar CounterVal -> Server Counter
handleCounter ctr = handleGetCounter ctr
:<|> handleStepCounter ctr
start :: IO ()
start = do
initCtr <- newTVarIO 0
run 8000 (serve counterAPI (handleCounter initCtr))
main :: IO ()
main = start
$ cabal build
Building servant-counter-example-0.1.0.0...
Preprocessing executable 'servant-counter-example' for
servant-counter-example-0.1.0.0...
[1 of 1] Compiling Main ( src/Main.hs, dist/build/servant-counter-example/servant-counter-example-tmp/Main.o )
src/Main.hs:45:13: error:
• No instance for (Servant.API.ContentTypes.AllCTRender
'[] NoContent)
arising from a use of ‘serve’
• In the second argument of ‘run’, namely
‘(serve counterAPI (handleCounter initCtr))’
In a stmt of a 'do' block:
run 8000 (serve counterAPI (handleCounter initCtr))
In the expression:
do { initCtr <- newTVarIO 0;
run 8000 (serve counterAPI (handleCounter initCtr)) }
-- Initial servant-greet-example.cabal generated by cabal init. For
-- further documentation, see http://haskell.org/cabal/users-guide/
-- The name of the package.
name: servant-greet-example
-- The package version. See the Haskell package versioning policy (PVP)
-- for standards guiding when and how versions should be incremented.
-- https://wiki.haskell.org/Package_versioning_policy
-- PVP summary: +-+------- breaking API changes
-- | | +----- non-breaking API additions
-- | | | +--- code changes with no API change
version: 0.1.0.0
-- A short (one-line) description of the package.
-- synopsis:
-- A longer description of the package.
-- description:
-- The license under which the package is released.
license: BSD3
-- The file containing the license text.
license-file: LICENSE
-- The package author(s).
-- author:
-- An email address to which users can send suggestions, bug reports, and
-- patches.
-- maintainer:
-- A copyright notice.
-- copyright:
category: Web
build-type: Simple
-- Extra files to be distributed with the package, such as examples or a
-- README.
extra-source-files: ChangeLog.md
-- Constraint on the version of Cabal needed to build this package.
cabal-version: >=1.10
executable servant-greet-example
-- .hs or .lhs file containing the Main module.
main-is: Main.hs
-- Modules included in this executable, other than Main.
-- other-modules:
-- LANGUAGE extensions used by modules in this package.
-- other-extensions:
-- Other library packages from which modules are imported.
build-depends: base >=4.9 && <4.10
, servant ==0.13.*
, servant-server ==0.13.*
, aeson >=1.2.3.0 && <1.4
, stm ==2.4.5.*
, warp ==3.2.18.*
, text
, wai
-- Directories containing source files.
hs-source-dirs: src
-- Base language which the package is written in.
default-language: Haskell2010
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment