diff --git a/Yesod/Handler.hs b/Yesod/Handler.hs index 78cd5d43..214f4832 100644 --- a/Yesod/Handler.hs +++ b/Yesod/Handler.hs @@ -3,6 +3,7 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE TypeSynonymInstances #-} -- FIXME remove {-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE PackageImports #-} --------------------------------------------------------- -- -- Module : Yesod.Handler @@ -39,7 +40,7 @@ import Yesod.Rep import Control.Exception hiding (Handler) import Control.Applicative -import Control.Monad.Trans +import "transformers" Control.Monad.Trans import Control.Monad.Attempt import Control.Monad (liftM, ap) @@ -103,7 +104,8 @@ runHandler (Handler handler) eh rr y cts = do case contents' of Left e -> do Response _ hs ct c <- runHandler (eh e) specialEh rr y cts - return $ Response (getStatus e) hs ct c + let hs' = hs ++ getHeaders e + return $ Response (getStatus e) hs' ct c Right a -> do (ct, c) <- a cts return $ Response 200 headers ct c diff --git a/Yesod/Rep.hs b/Yesod/Rep.hs index c0f96443..68ad4e42 100644 --- a/Yesod/Rep.hs +++ b/Yesod/Rep.hs @@ -37,12 +37,15 @@ module Yesod.Rep , plain , Template (..) , TemplateFile (..) + , Static (..) + , StaticFile (..) #if TEST , testSuite #endif ) where import Data.ByteString.Lazy (ByteString) +import qualified Data.ByteString.Lazy as BL import Data.Text.Lazy (Text) import Data.Maybe (mapMaybe) import Data.Function (on) @@ -134,6 +137,9 @@ instance HasReps RepChooser where reps = error "reps of RepChooser" chooseRep = id +instance HasReps () where + reps = [(TypePlain, const $ return $ cs "")] + instance HasReps [(ContentType, Content)] where reps = error "reps of [(ContentType, Content)]" chooseRep a cts = return $ @@ -170,6 +176,18 @@ instance HasReps TemplateFile where return $ cs $ unJsonDoc $ cs ho) ] +data Static = Static ContentType ByteString +instance HasReps Static where + reps = error "reps of Static" + chooseRep (Static ct bs) _ = return (ct, Content bs) + +data StaticFile = StaticFile ContentType FilePath +instance HasReps StaticFile where + reps = error "reps of StaticFile" + chooseRep (StaticFile ct fp) _ = do + bs <- BL.readFile fp + return (ct, Content bs) + -- Useful instances of HasReps instance HasReps HtmlObject where reps = diff --git a/Yesod/Resource.hs b/Yesod/Resource.hs index 88bf8333..56d1caa6 100644 --- a/Yesod/Resource.hs +++ b/Yesod/Resource.hs @@ -51,11 +51,7 @@ import Control.Monad ((<=<), unless) import Data.Object.Yaml import Yesod.Handler import Data.Maybe (fromJust) -#if TEST -import Yesod.Rep hiding (testSuite) -#else -import Yesod.Rep -#endif +import Yesod.Rep (chooseRep) import Control.Arrow #if TEST diff --git a/examples/fact.html b/examples/fact.html new file mode 100644 index 00000000..db3e135f --- /dev/null +++ b/examples/fact.html @@ -0,0 +1,12 @@ + + + + Factorials + + +
+

+

+
+ + diff --git a/examples/fact.lhs b/examples/fact.lhs new file mode 100644 index 00000000..e41c6ae3 --- /dev/null +++ b/examples/fact.lhs @@ -0,0 +1,27 @@ +\begin{code} +{-# LANGUAGE QuasiQuotes #-} + +import Yesod +import Hack.Handler.SimpleServer + +data Fact = Fact +instance Yesod Fact where + handlers = [$resources| +/: + Get: index +/#num: + Get: fact +/fact: + Get: factRedirect +|] + +index = return $ StaticFile TypeHtml "examples/fact.html" +fact i = return $ toHtmlObject $ show $ product [1..fromIntegral i] +factRedirect = do + i <- getParam "num" + redirect $ "../" ++ i ++ "/" + return () + +main :: IO () +main = putStrLn "Running..." >> run 3000 (toHackApp Fact) +\end{code} diff --git a/yesod.cabal b/yesod.cabal index 2abb4387..22725226 100644 --- a/yesod.cabal +++ b/yesod.cabal @@ -113,3 +113,11 @@ executable hellotemplate Buildable: False ghc-options: -Wall main-is: examples/hellotemplate.lhs + +executable fact + if flag(buildsamples) + Buildable: True + else + Buildable: False + ghc-options: -Wall + main-is: examples/fact.lhs