Skip to content

Commit

Permalink
removed tail . init, added new cookies example
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramiro Pastor committed Nov 15, 2018
1 parent 0c2831d commit fa46908
Show file tree
Hide file tree
Showing 3 changed files with 171 additions and 1 deletion.
132 changes: 132 additions & 0 deletions attic/Examples/Cookies.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
{-# LANGUAGE OverloadedStrings #-}


module Main where

import Control.Applicative (optional)
import Control.Monad (msum)
import Data.Maybe (fromMaybe)
import Data.Text.Lazy (unpack)
import Happstack.Server
import Text.Blaze.Html5 (Html, (!))
import qualified Text.Blaze.Html5 as H
import qualified Text.Blaze.Html5.Attributes as A

--------------------------------------------------------------------------------


serve :: ServerPart Response -> IO ()
serve part =
let
ramQuota = 1 * 10^6
diskQuota = 20 * 10^6
tmpDir = "/tmp/"
policy = defaultBodyPolicy tmpDir diskQuota ramQuota (ramQuota `div` 10)
in
simpleHTTP (nullConf { port = 8000}) $ do
decodeBody policy
part



main :: IO ()
main = serve $ msum [setCookie, getCookie]


setCookie :: ServerPart Response
setCookie = do
method POST
newCk <- lookText "new-msg"
addCookies
[(,) Session (mkCookie "theCookie" $ unpack newCk)
]
seeOther ("/" :: String) $ toResponse ()


getCookie :: ServerPart Response
getCookie = do
method GET
mMemory <- optional $ lookCookieValue "theCookie"
let memory = fromMaybe "No saved message." mMemory
ok $ toResponse $ viewCookie memory


--------------------------------------------------------------------------------


viewCookie :: String -> Html
viewCookie msg =
H.html $ do
H.head $ do
H.title "Happstack cookies example"
H.style (H.toHtml viewCookieCss)
H.body $
H.form
! A.method "post"
! A.action "/"
$ do
H.h3 "Happstack cookies example"
H.p "The message in your cookie says:"
H.h5 (H.toHtml msg)
H.p "Enter new message:"
H.div
! A.style "display:flex; align-items: center;"
$ do
H.input
! A.type_ "text"
! A.name "new-msg"
H.button ""
! A.type_ "submit"



viewCookieCss :: String
viewCookieCss = concat
[ "* {box-sizing:border-box; margin:0; padding:0; }"
, "body {"
, " display:flex; justify-content:center; align-items:center; "
, " min-height:100vh; "
, " font-family: Arial, Helvetica, sans-serif; color: navy;"
, " background-image: "
, " radial-gradient(circle farthest-corner at 5% 50%, gold, transparent),"
, " radial-gradient(circle farthest-corner at 95% 50%, #f06, transparent);"
, "} "
, "form { "
, " padding: 40px 60px;"
, " border: 1px solid navy; border-radius: 15px;"
, " background-color: rgb(235,235,255);"
, " box-shadow: 1px 1px 5px 3px rgba(0,0,0,0.2);"
, " }"
, "h3 {"
, " margin-bottom:30px; padding-bottom:8px; border-bottom:1px solid silver;"
, " font-variant: small-caps; color: navy;"
, " text-transform: capitalize;"
, "}"
, "p {"
, " font-size: 12px; margin-bottom: 12px;"
, "}"
, "h5 {"
, " margin-bottom: 20px; text-align: center; color: teal;"
, "}"
, "input {"
, " height: 45px; width: 350px; margin-right: 15px; padding: 10px;"
, " border: 1px solid navy; border-radius: 2px; "
, "}"
, "button {"
, " height: 45px; width: 45px; margin-left: 0px;"
, " display: flex; justify-content: center; align-items: center;"
, " border: 1px solid navy; border-radius: 100px;"
, " background: white;"
, " font-size: 25px;"
, " transition: all 2s; cursor: pointer;"
, "}"
, "input:focus {border-color: blue;}"
, "button:hover {"
, " background-color: rgb(80,255,100); "
, " transform: rotate(1440deg); height: 60px; width: 60px;"
, " margin-left: 15px;"
, "}"
]


--------------------------------------------------------------------------------
38 changes: 38 additions & 0 deletions attic/Examples/dist-newstyle/cache/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
packages: ./*.cabal
optional-packages: ./*/*.cabal
remote-repo-cache: /home/ramiro/.cabal/packages
logs-dir: /home/ramiro/.cabal/logs
world-file: /home/ramiro/.cabal/world
verbose: 1
solver: choose
build-summary: /home/ramiro/.cabal/logs/build.log
doc-index-file: $datadir/doc/$arch-$os-$compiler/index.html
max-backjumps: 2000
reorder-goals: False
strong-flags: False
remote-build-reporting: anonymous
report-planning-failure: False
one-shot: False
jobs: $ncpus
offline: False
extra-prog-path: /home/ramiro/.cabal/bin
compiler: ghc
compiler: ghc
documentation: False
haddock-keep-temp-files: False
haddock-hoogle: False
haddock-html: False
haddock-executables: False
haddock-tests: False
haddock-benchmarks: False
haddock-internal: False
haddock-hyperlink-source: False

repository hackage.haskell.org
url: http://hackage.haskell.org/
root-keys: fe331502606802feac15e514d9b9ea83fee8b6ffef71335479a2e68d84adc6b0
1ea9ba32c526d1cc91ab5e5bd364ec5e9e8cb67179a471872f6e26f0ae773d42
2c6c3627bd6c982990239487f1abd02e08a02e6cf16edb105a8012d444d870c3
0a5c7ea47cd1b15f01f5f51a33adda7e655bc0f0b0615baa8e271f4c3351e21d
51f0161b906011b52c6613376b1ae937670da69322113a246a09f807c62f6921
key-threshold: 3
2 changes: 1 addition & 1 deletion src/Happstack/Server/RqData.hs
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ lookCookie name
Nothing -> rqDataError $ strMsg $ "lookCookie: cookie not found: " ++ name
Just c -> return c{cookieValue = f c}
where
f = unEscapeString . init . tail . cookieValue
f = unEscapeString . cookieValue

-- | gets the named cookie as a string
lookCookieValue :: (Functor m, Monad m, HasRqData m) => String -> m String
Expand Down

0 comments on commit fa46908

Please sign in to comment.