Added two basic examples

This commit is contained in:
Michael Snoyman 2009-12-17 19:27:03 +02:00
parent ac450c9513
commit a7cfa5f667
10 changed files with 131 additions and 25 deletions

View File

@ -19,6 +19,9 @@ module Yesod
, module Yesod.Definitions , module Yesod.Definitions
, module Yesod.Handler , module Yesod.Handler
, module Yesod.Resource , module Yesod.Resource
, module Data.Object.Html
, module Yesod.Rep
, module Data.Convertible.Text
, Application , Application
) where ) where
@ -29,3 +32,6 @@ import Yesod.Definitions
import Yesod.Handler import Yesod.Handler
import Yesod.Resource import Yesod.Resource
import Hack (Application) import Hack (Application)
import Yesod.Rep
import Data.Object.Html
import Data.Convertible.Text

View File

@ -26,9 +26,6 @@ import qualified Web.Authenticate.Rpxnow as Rpxnow
import qualified Web.Authenticate.OpenId as OpenId import qualified Web.Authenticate.OpenId as OpenId
import Data.Enumerable import Data.Enumerable
import Data.Object.Html
import Data.Convertible.Text
import Yesod import Yesod
import Yesod.Constants import Yesod.Constants

View File

@ -86,13 +86,15 @@ instance HasReps SitemapResponse where
[ (TypeXml, return . cs) [ (TypeXml, return . cs)
] ]
sitemap :: Yesod yesod => IO [SitemapUrl] -> Handler yesod SitemapResponse sitemap :: YesodApproot yesod
=> IO [SitemapUrl]
-> Handler yesod SitemapResponse
sitemap urls' = do sitemap urls' = do
yesod <- getYesod yesod <- getYesod
urls <- liftIO urls' urls <- liftIO urls'
return $ SitemapResponse urls $ approot yesod return $ SitemapResponse urls $ approot yesod
robots :: Yesod yesod => Handler yesod Plain robots :: YesodApproot yesod => Handler yesod Plain
robots = do robots = do
yesod <- getYesod yesod <- getYesod
return $ plain $ "Sitemap: " ++ unApproot (approot yesod) return $ plain $ "Sitemap: " ++ unApproot (approot yesod)

View File

@ -38,6 +38,10 @@ import Data.Char (isDigit)
import Language.Haskell.TH.Syntax import Language.Haskell.TH.Syntax
import Language.Haskell.TH.Quote import Language.Haskell.TH.Quote
{- Debugging
import Language.Haskell.TH.Ppr
import System.IO
-}
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import Control.Exception (Exception) import Control.Exception (Exception)
@ -263,7 +267,10 @@ instance Exception RepeatedVerb
rpnodesTHCheck :: [RPNode] -> Q Exp rpnodesTHCheck :: [RPNode] -> Q Exp
rpnodesTHCheck nodes = do rpnodesTHCheck nodes = do
nodes' <- runIO $ checkRPNodes nodes nodes' <- runIO $ checkRPNodes nodes
-- For debugging purposes runIO $ putStrLn $ pprint res {- For debugging purposes
rpnodesTH nodes' >>= runIO . putStrLn . pprint
runIO $ hFlush stdout
-}
rpnodesTH nodes' rpnodesTH nodes'
notFoundVerb :: Verb -> Handler yesod a notFoundVerb :: Verb -> Handler yesod a
@ -338,11 +345,6 @@ countParams (RP rpps) = helper 0 rpps where
helper i (Static _:rest) = helper i rest helper i (Static _:rest) = helper i rest
helper i (_:rest) = helper (i + 1) rest helper i (_:rest) = helper (i + 1) rest
instance Lift RPNode where
lift (RPNode rp vm) = do
rp' <- lift rp
vm' <- liftVerbMap vm $ countParams rp
return $ TupE [rp', vm']
instance Lift RP where instance Lift RP where
lift (RP rpps) = do lift (RP rpps) = do
rpps' <- lift rpps rpps' <- lift rpps
@ -365,14 +367,17 @@ liftVerbMap :: VerbMap -> Int -> Q Exp
liftVerbMap (AllVerbs s) _ = do liftVerbMap (AllVerbs s) _ = do
cr <- [|(.) (fmap chooseRep)|] cr <- [|(.) (fmap chooseRep)|]
return $ cr `AppE` ((VarE $ mkName s) `AppE` (VarE $ mkName "verb")) return $ cr `AppE` ((VarE $ mkName s) `AppE` (VarE $ mkName "verb"))
liftVerbMap (Verbs vs) params = liftVerbMap (Verbs vs) params = do
return $ CaseE (VarE $ mkName "verb") cr0 <- [|fmap chooseRep|]
$ map helper vs ++ [whenNotFound] cr1 <- [|(.) (fmap chooseRep)|]
let cr = if params == 0 then cr0 else cr1
return $ CaseE (VarE $ mkName "verb")
$ map (helper cr) vs ++ [whenNotFound]
where where
helper :: (Verb, String) -> Match helper :: Exp -> (Verb, String) -> Match
helper (v, f) = helper cr (v, f) =
Match (ConP (mkName $ show v) []) Match (ConP (mkName $ show v) [])
(NormalB $ VarE $ mkName f) (NormalB $ cr `AppE` VarE (mkName f))
[] []
whenNotFound :: Match whenNotFound :: Match
whenNotFound = whenNotFound =

View File

@ -1,6 +1,7 @@
-- | The basic typeclass for a Yesod application. -- | The basic typeclass for a Yesod application.
module Yesod.Yesod module Yesod.Yesod
( Yesod (..) ( Yesod (..)
, YesodApproot (..)
, toHackApp , toHackApp
) where ) where
@ -35,6 +36,7 @@ class Yesod a where
errorHandler :: ErrorResult -> Handler a RepChooser errorHandler :: ErrorResult -> Handler a RepChooser
errorHandler = defaultErrorHandler errorHandler = defaultErrorHandler
class Yesod a => YesodApproot a where
-- | An absolute URL to the root of the application. -- | An absolute URL to the root of the application.
approot :: a -> Approot approot :: a -> Approot

View File

@ -0,0 +1,22 @@
\begin{code}
{-# LANGUAGE QuasiQuotes #-}
import Yesod
import Hack.Handler.SimpleServer
data HelloWorld = HelloWorld
instance Yesod HelloWorld where
handlers = [$resources|
/:
Get: helloWorld
|]
helloWorld :: Handler HelloWorld TemplateFile
helloWorld = return $ TemplateFile "examples/template.html" $ cs
[ ("title", "Hello world!")
, ("content", "Hey look!! I'm <auto escaped>!")
]
main :: IO ()
main = putStrLn "Running..." >> run 3000 (toHackApp HelloWorld)
\end{code}

19
examples/helloworld.lhs Normal file
View File

@ -0,0 +1,19 @@
\begin{code}
{-# LANGUAGE QuasiQuotes #-}
import Yesod
import Hack.Handler.SimpleServer
data HelloWorld = HelloWorld
instance Yesod HelloWorld where
handlers = [$resources|
/:
Get: helloWorld
|]
helloWorld :: Handler HelloWorld HtmlObject
helloWorld = return $ cs "Hello world!"
main :: IO ()
main = putStrLn "Running..." >> run 3000 (toHackApp HelloWorld)
\end{code}

26
examples/template.html Normal file
View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>$o.title$</title>
<style>
body {
background-color: #ffc;
}
#wrapper {
width: 600px;
margin: 2em auto;
background-color: #fefefe;
border: 1px solid black;
padding: 1em;
font-family: sans-serif;
}
</style>
</head>
<body>
<div id="wrapper">
$o.content$
</div>
</body>
</html>

View File

@ -2,8 +2,6 @@
{-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE QuasiQuotes #-}
import Yesod import Yesod
import Yesod.Rep
import Data.Object.Html
data MyYesod = MyYesod data MyYesod = MyYesod
@ -11,18 +9,18 @@ instance Show (Handler MyYesod RepChooser) where show _ = "Another handler"
getStatic :: Verb -> [String] -> Handler MyYesod HtmlObject getStatic :: Verb -> [String] -> Handler MyYesod HtmlObject
getStatic v p = return $ toHtmlObject ["getStatic", show v, show p] getStatic v p = return $ toHtmlObject ["getStatic", show v, show p]
pageIndex :: Handler MyYesod RepChooser pageIndex :: Handler MyYesod HtmlObject
pageIndex = return $ chooseRep $ toHtmlObject ["pageIndex"] pageIndex = return $ toHtmlObject ["pageIndex"]
pageAdd :: Handler MyYesod RepChooser pageAdd :: Handler MyYesod RepChooser
pageAdd = return $ chooseRep $ toHtmlObject ["pageAdd"] pageAdd = return $ chooseRep $ toHtmlObject ["pageAdd"]
pageDetail :: String -> Handler MyYesod RepChooser pageDetail :: String -> Handler MyYesod RepChooser
pageDetail s = return $ chooseRep $ toHtmlObject ["pageDetail", s] pageDetail s = return $ chooseRep $ toHtmlObject ["pageDetail", s]
pageDelete :: String -> Handler MyYesod RepChooser pageDelete :: String -> Handler MyYesod HtmlObject
pageDelete s = return $ chooseRep $ toHtmlObject ["pageDelete", s] pageDelete s = return $ toHtmlObject ["pageDelete", s]
pageUpdate :: String -> Handler MyYesod RepChooser pageUpdate :: String -> Handler MyYesod RepChooser
pageUpdate s = return $ chooseRep $ toHtmlObject ["pageUpdate", s] pageUpdate s = return $ chooseRep $ toHtmlObject ["pageUpdate", s]
userInfo :: Int -> Handler MyYesod RepChooser userInfo :: Int -> Handler MyYesod HtmlObject
userInfo i = return $ chooseRep $ toHtmlObject ["userInfo", show i] userInfo i = return $ toHtmlObject ["userInfo", show i]
instance Show (Verb -> Handler MyYesod RepChooser) where instance Show (Verb -> Handler MyYesod RepChooser) where
show _ = "verb -> handler" show _ = "verb -> handler"

View File

@ -16,6 +16,10 @@ flag buildtests
description: Build the executable to run unit tests description: Build the executable to run unit tests
default: False default: False
flag buildsamples
description: Build the executable to run unit tests
default: False
flag nolib flag nolib
description: Skip building of the library. description: Skip building of the library.
default: False default: False
@ -87,3 +91,28 @@ executable runtests
Buildable: False Buildable: False
ghc-options: -Wall ghc-options: -Wall
main-is: runtests.hs main-is: runtests.hs
executable quasi-test
if flag(buildsamples)
Buildable: True
else
Buildable: False
ghc-options: -Wall
main-is: test/quasi-resource.hs
executable helloworld
if flag(buildsamples)
Buildable: True
else
Buildable: False
ghc-options: -Wall
build-depends: hack-handler-simpleserver >= 0.2.0 && < 0.3
main-is: examples/helloworld.lhs
executable hellotemplate
if flag(buildsamples)
Buildable: True
else
Buildable: False
ghc-options: -Wall
main-is: examples/hellotemplate.lhs