File: cookie.hs

package info (click to toggle)
haskell-cgi 3001.5.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 164 kB
  • sloc: haskell: 828; makefile: 3
file content (28 lines) | stat: -rw-r--r-- 863 bytes parent folder | download | duplicates (4)
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
import Network.CGI
   ( CGI, CGIResult, runCGI, output, setCookie, newCookie, getCookie
   )
import Text.XHtml
   ( Html, h1, p, header, body, (+++), thetitle, (<<), renderHtml
   )

setCounterCookie :: Int -> CGI ()
setCounterCookie n = setCookie (newCookie "mycookie" (show n))

firstTime :: CGI [Html]
firstTime = do setCounterCookie 1
               return [h1 << "Welcome!"]

returnVisitor :: Int -> CGI [Html]
returnVisitor c =
    do setCounterCookie (c + 1)
       return [h1 << "Welcome back!",
               p << ("I have seen you " ++ show c ++ " times before.")]

cgiMain :: CGI CGIResult
cgiMain = do mc <- getCookie "mycookie"
             h <- maybe firstTime (returnVisitor . read) mc
             output $ renderHtml $ header << thetitle << "Cookie example"
                                    +++ body << h

main :: IO ()
main = runCGI cgiMain