Skip to content

Commit

Permalink
Merge branch 'master' of github.com:c3d2/ta-haskell-yesod
Browse files Browse the repository at this point in the history
  • Loading branch information
maloi committed Aug 29, 2012
2 parents 9fd7471 + 459f030 commit 6149e8f
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.html
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h1>Themenabend Haskell &amp; Yesod</h1>
<p>Chaos Computer Club Dresden</p> <p>Chaos Computer Club Dresden</p>
<p>2012-08-29</p> <p>2012-08-29</p>
<p class="note">Genesis: 1990</p> <p class="note">Genesis: 1990</p>
<p class="note">Most Advanced Flow Control</p>
<p class="note">Academic Reputation</p>
</div> </div>


<div class="slide"> <div class="slide">
Expand Down Expand Up @@ -267,7 +269,9 @@ <h1>Record-Syntax</h1>
Prelude> :t meinInteger Prelude> :t meinInteger
meinInteger :: MeinTyp -> Integer meinInteger :: MeinTyp -> Integer
Prelude> meinInteger fnord Prelude> meinInteger fnord
23</div> 23</pre>
<p class="note">Parameterisierte Typen!</p>
</div>
<!-- Classes, instances --> <!-- Classes, instances -->
<div class="slide"> <div class="slide">
<h1>Classes &amp; Instances</h1> <h1>Classes &amp; Instances</h1>
Expand Down Expand Up @@ -1415,6 +1419,10 @@ <h1>WAI: Motivation</h1>
<a href="http://www.yesodweb.com/blog/2011/03/preliminary-warp-cross-language-benchmarks">www.yesodweb.com/blog/2011/03/preliminary-warp-cross-language-benchmarks</a> <a href="http://www.yesodweb.com/blog/2011/03/preliminary-warp-cross-language-benchmarks">www.yesodweb.com/blog/2011/03/preliminary-warp-cross-language-benchmarks</a>
</p> </p>
</div> </div>
<div class="slide">
<h1>Yesod</h1>
<p>Live-Coding!</p>
</div>
<!-- <!--
mtl mtl
lists: monomorph lists: monomorph
Expand Down
81 changes: 81 additions & 0 deletions yesod-example/Application.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,81 @@
{-# LANGUAGE TemplateHaskell, QuasiQuotes, MultiParamTypeClasses, TypeFamilies, OverloadedStrings #-}
module Application where

import Control.Applicative
import Yesod
import Yesod.Form.Jquery
import Text.Hamlet
import qualified Data.Text as T
import Control.Concurrent.STM



app :: IO Application
app = do
dents <- liftIO $ atomically $ newTVar []
toWaiAppPlain $ ChatApp dents


data Dent = Dent
{ dentSender :: T.Text
, dentBody :: T.Text
}

data ChatApp = ChatApp { chatDents :: TVar [Dent] }

getDents = chatDents <$> getYesod

mkYesod "ChatApp" [parseRoutes|
/ HomeR GET
/send SendR POST
|]

instance Yesod ChatApp where
defaultLayout widget =
do pc <- widgetToPageContent widget
hamletToRepHtml $(hamletFile "default-layout.hamlet")

instance YesodJquery ChatApp

instance RenderMessage ChatApp FormMessage where
renderMessage _ _ = defaultFormMessage


getHomeR :: Handler RepHtml
getHomeR = do
(form, enctype) <- generateFormPost dentForm
dents <- getDents >>=
liftIO . atomically . readTVar
defaultLayout $ do
setTitle "Welcome to Yitter"
[whamlet|
<h1>Chat
<form method=POST action=@{SendR} enctype=#{enctype}>
^{form}
<input type=submit value=Ok>
$forall dent <- dents
<article>
<h2>#{dentSender dent}
<p>#{dentBody dent}
|]

dentForm = renderDivs $ Dent
<$> areq textField "Name: " Nothing
<*> areq textField "Body: " Nothing

postSendR :: Handler RepHtml
postSendR = do
((result, widget), enctype) <- runFormPost dentForm
case result of
FormSuccess dent -> do
dents <- getDents
liftIO $ atomically $
modifyTVar dents $ (dent :)
redirect HomeR
_ ->
defaultLayout [whamlet|
<h1>Oops
|]


-- TODO: i18n, form validation
9 changes: 9 additions & 0 deletions yesod-example/default-layout.hamlet
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,9 @@
$newline always
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>#{pageTitle pc}
^{pageHead pc}
<body>
<div #page>
^{pageBody pc}
19 changes: 19 additions & 0 deletions yesod-example/main.hs
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,19 @@
module Main where

import System.IO
import Network.Wai.Handler.Warp (runSettings, defaultSettings,
settingsHost, settingsPort, settingsOnException)
import Data.Conduit.Network (HostPreference (HostIPv6))
import Application


main :: IO ()
main = app >>=
runSettings (defaultSettings
{ settingsHost = HostIPv6
, settingsPort = 8000
, settingsOnException = \e ->
hPrint stdout e
>>
hFlush stdout
})

0 comments on commit 6149e8f

Please sign in to comment.