-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:c3d2/ta-haskell-yesod
- Loading branch information
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
This file contains 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
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 |
This file contains 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
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} |
This file contains 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
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 | |||
}) |