Does not reload template dir on each request

This commit is contained in:
Michael Snoyman 2010-01-24 00:56:08 +02:00
parent 953d66542a
commit b460e9d477
4 changed files with 26 additions and 17 deletions

View File

@ -14,6 +14,7 @@ import Yesod.Constants
import Yesod.Definitions import Yesod.Definitions
import Yesod.Handler import Yesod.Handler
import Yesod.Utils import Yesod.Utils
import Yesod.Template (TemplateGroup)
import Data.Maybe (fromMaybe) import Data.Maybe (fromMaybe)
import Data.Convertible.Text import Data.Convertible.Text
@ -27,7 +28,8 @@ import Hack.Middleware.Jsonp
import Hack.Middleware.MethodOverride import Hack.Middleware.MethodOverride
class Yesod a where class Yesod a where
-- | Please use the Quasi-Quoter, you\'ll be happier. FIXME more info. -- | Please use the Quasi-Quoter, you\'ll be happier. For more information,
-- see the examples/fact.lhs sample.
handlers :: Resource -> Verb -> Handler a RepChooser handlers :: Resource -> Verb -> Handler a RepChooser
-- | The encryption key to be used for encrypting client sessions. -- | The encryption key to be used for encrypting client sessions.
@ -74,26 +76,33 @@ defaultErrorHandler (InternalError e) =
[ ("Internal server error", e) [ ("Internal server error", e)
] ]
toHackApp :: Yesod y => y -> Hack.Application toHackApp :: Yesod y => y -> IO Hack.Application
toHackApp a env = do toHackApp a = do
key <- encryptKey a key <- encryptKey a
let app' = toHackApp' a app' <- toHackApp' a
let mins = clientSessionDuration a let mins = clientSessionDuration a
(gzip $ cleanPath $ jsonp $ methodOverride return $ gzip
$ clientsession encryptedCookies key mins $ app') env $ cleanPath
$ jsonp
$ methodOverride
$ clientsession encryptedCookies key mins
$ app'
toHackApp' :: Yesod y => y -> Hack.Application toHackApp' :: Yesod y => y -> IO Hack.Application
toHackApp' y env = do toHackApp' y = do
let td = templateDir y
tg <- if null td
then return nullGroup
else directoryGroupRecursiveLazy td
return $ toHackApp'' y tg
toHackApp'' :: Yesod y => y -> TemplateGroup -> Hack.Env -> IO Hack.Response
toHackApp'' y tg env = do
let (Right resource) = splitPath $ Hack.pathInfo env let (Right resource) = splitPath $ Hack.pathInfo env
types = httpAccept env types = httpAccept env
verb = cs $ Hack.requestMethod env verb = cs $ Hack.requestMethod env
handler = handlers resource verb handler = handlers resource verb
rr = cs env rr = cs env
-- FIXME don't do the templateDir thing for each request
let td = templateDir y
tg <- if null td
then return nullGroup
else directoryGroupRecursiveLazy td
res <- runHandler handler errorHandler rr y tg types res <- runHandler handler errorHandler rr y tg types
let langs = ["en"] -- FIXME let langs = ["en"] -- FIXME
responseToHackResponse langs res responseToHackResponse langs res

View File

@ -89,7 +89,7 @@ one piece of data.
> factRedirect :: Handler y () > factRedirect :: Handler y ()
> factRedirect = do > factRedirect = do
> i <- getParam "num" > i <- runRequest $ getParam "num"
> redirect $ "../" ++ i ++ "/" > redirect $ "../" ++ i ++ "/"
The following line would be unnecesary if we had a type signature on The following line would be unnecesary if we had a type signature on
@ -102,4 +102,4 @@ you could use CGI, FastCGI or a more powerful server. Just check out Hackage
for options (any package starting hack-handler- should suffice). for options (any package starting hack-handler- should suffice).
> main :: IO () > main :: IO ()
> main = putStrLn "Running..." >> run 3000 (toHackApp Fact) > main = putStrLn "Running..." >> toHackApp Fact >>= run 3000

View File

@ -26,5 +26,5 @@ helloGroup = template "real-template" "foo" (cs "bar") $ return []
main :: IO () main :: IO ()
main = do main = do
putStrLn "Running..." putStrLn "Running..."
run 3000 $ toHackApp HelloWorld toHackApp HelloWorld >>= run 3000
\end{code} \end{code}

View File

@ -15,5 +15,5 @@ helloWorld :: Handler HelloWorld HtmlObject
helloWorld = return $ cs "Hello world!" helloWorld = return $ cs "Hello world!"
main :: IO () main :: IO ()
main = putStrLn "Running..." >> run 3000 (toHackApp HelloWorld) main = putStrLn "Running..." >> toHackApp HelloWorld >>= run 3000
\end{code} \end{code}