Skip to content

Instantly share code, notes, and snippets.

@xkikeg
Last active December 18, 2015 11:18
Show Gist options
  • Save xkikeg/5774231 to your computer and use it in GitHub Desktop.
Save xkikeg/5774231 to your computer and use it in GitHub Desktop.
Yesod Book :: Sessions :: session operations
{-# LANGUAGE TypeFamilies, QuasiQuotes, TemplateHaskell, MultiParamTypeClasses, OverloadedStrings #-}
import Yesod
import Control.Applicative ((<$>), (<*>))
import qualified Web.ClientSession as CS
data SessionExample = SessionExample
mkYesod "SessionExample" [parseRoutes|
/ Root GET POST
|]
getRoot :: Handler Html
getRoot = do
sess <- getSession
giveUrlRenderer [hamlet|
<form method=post>
<input type=text name=key>
<input type=text name=val>
<input type=submit>
<h1>#{show sess}
|]
postRoot :: Handler ()
postRoot = do
(key, mval) <- runInputPost $ (,) <$> ireq textField "key" <*> iopt textField "val"
case mval of
Nothing -> deleteSession key
Just val -> setSession key val
liftIO $ print (key, mval)
redirect Root
instance Yesod SessionExample where
-- Make the session timeout 1 minute so that it's easier to play with
makeSessionBackend _ =
defaultClientSessionBackend 1 CS.defaultKeyFile >>= return . Just
instance RenderMessage SessionExample FormMessage where
renderMessage _ _ = defaultFormMessage
main :: IO ()
main = warp 3000 SessionExample
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment