Last active
December 18, 2015 11:18
-
-
Save xkikeg/5774231 to your computer and use it in GitHub Desktop.
Yesod Book :: Sessions :: session operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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