#! /usr/bin/env nix-shell
#! nix-shell -i runhaskell -p 'ghc.withPackages(pkgs: with pkgs; [Spock])'
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
( (<>) )
import Web.Spock
( SpockM, runSpock, spock, get, text#! /usr/bin/env nix-shell
#! nix-shell -i runhaskell -p 'ghc.withPackages(pkgs: with pkgs; [Spock])'
{-# LANGUAGE OverloadedStrings #-}
import Data.Monoid
( (<>) )
import Web.Spock
( SpockM, runSpock, spock, get, textread a request parameter (first from POST variables and then GET query parameters)
, paramlike param above but throws an error if the request parameter is missing
, param' )
import Web.Spock.Config
( SpockCfg ( spc_csrfProtection )
, PoolOrConn ( PCNoDatabase )
, defaultSpockCfg
)
data AppSession = EmptySession
data AppState = EmptyState
main :: IO ()
main = do
defConfig <- defaultSpockCfg EmptySession PCNoDatabase EmptyState
let config = defConfig { spc_csrfProtection = True }
runSpock 5000 (spock config webapp)
webapp :: SpockM () AppSession AppState ()
webapp = doThis route handles GET /hello and looks for a query parameter named name.
We are forced to handle the absence of the name query parameter since param returns a Maybe.
Try visiting:
get "hello" $ do
mName <- param "name"
case mName of
Nothing ->
text ("Hello stranger! Sorry, I did not get your name.")
Just name ->
text ("Hello " <> name <> "! Nice to meet you.")This route handles GET /howdy and requires the name query parameter to be present.
param' throws an error if the name query parameter is missing.
Try visiting:
get "howdy" $ do
name <- param' "name"
text ("Howdy " <> name <> "! How goes it?")