Skip to content

Instantly share code, notes, and snippets.

@manuelleduc
Created December 9, 2017 23:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuelleduc/c0a9827a95f7272b78b896de64f42383 to your computer and use it in GitHub Desktop.
Save manuelleduc/c0a9827a95f7272b78b896de64f42383 to your computer and use it in GitHub Desktop.
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where
import Web.Spock
import Web.Spock.Config
import Control.Monad.Trans
import Data.Aeson hiding (json)
import Data.IORef
import Data.Monoid
import qualified Data.Text as T
import GHC.Generics
import Network.HTTP.Types.Status
data MySession = EmptySession
newtype MyAppState = DummyAppState (IORef Int)
data User = User {
login:: String,
pass :: String
} deriving (Generic, Show)
instance ToJSON User
instance FromJSON User
main :: IO ()
main =
do ref <- newIORef 0
spockCfg <- defaultSpockCfg EmptySession PCNoDatabase (DummyAppState ref)
runSpock 8080 (spock spockCfg app)
type ApiAction a = SpockAction () MySession MyAppState a
app :: SpockM () MySession MyAppState ()
app =
do get root $
text "Hello World!"
get ("hello" <//> var) $ \name ->
do (DummyAppState ref) <- getState
visitorNumber <- liftIO $ atomicModifyIORef' ref $ \i -> (i+1, i+1)
text ("Hello " <> name <> ", you are visitor number " <> T.pack (show visitorNumber))
post "login" $ do
usr <- jsonBody' :: ApiAction User
case usr of
User "admin" "admin" -> json $ object ["yolo" .= String "success"]
_ -> do setStatus status401
json $ object ["yolo" .= String "fail"]
name: seminaire-diverse2017-spock
version: 0.1.0.0
-- synopsis:
-- description:
homepage: https://github.com/githubuser/seminaire-diverse2017-spock#readme
license: BSD3
license-file: LICENSE
author: Author name here
maintainer: example@example.com
copyright: 2017 Author name here
category: Web
build-type: Simple
extra-source-files: README.md
cabal-version: >=1.10
executable seminaire-diverse2017-spock
hs-source-dirs: app
main-is: Main.hs
default-language: Haskell2010
build-depends: base >= 4.7 && < 5
, Spock >= 0.11
, mtl
, text
, aeson
, http-types
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment