public
Last active

  • Download Gist
Tutorial.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE OverloadedStrings #-}
 
module Main where
 
import qualified Data.ByteString.Char8 as B
import qualified Data.Text as T
import Data.Maybe
import Snap
import Snap.Snaplet.Heist
import Snap.Snaplet.Session
import Snap.Snaplet.Session.Backends.CookieSession
 
data App = App
{ _heist :: Snaplet (Heist App)
, _sess :: Snaplet (SessionManager)
}
 
makeLenses [''App]
 
appInit :: SnapletInit App App
appInit = makeSnaplet "myapp" "My example application" Nothing $ do
hs <- nestSnaplet "heist" heist $ heistInit "templates"
ss <- nestSnaplet "session" sess $ initCookieSessionManager "config/session.txt" "_session" (Just 3600)
addRoutes [ ("/hello", writeText "hello world")
, ("/session", sessionHandler)
, ("", heistServe)
]
return $ App hs ss
 
sessionHandler :: Handler App App ()
sessionHandler = method GET getter <|> method POST setter
where
getter = do
sessionList <- with sess $ sessionToList
mapM_ (writeText . fst) sessionList
writeText "\ngetter\n"
setter = do
mkey <- getParam "key"
mvalue <- getParam "value"
withSession sess . withTop sess $ setInSession (convert mkey) (convert mvalue)
getter
convert = T.pack . B.unpack . (fromMaybe "set-error")
 
instance HasHeist App where heistLens = subSnaplet heist
 
main :: IO ()
main = serveSnaplet defaultConfig appInit

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.