static: update sample-embed.hs and point the old embedded static to the new one

This commit is contained in:
John Lenz 2013-09-12 12:21:47 -05:00
parent 2ad3977712
commit 8e16fd2227
3 changed files with 45 additions and 20 deletions

View File

@ -35,7 +35,6 @@ module Yesod.Static
-- * Smart constructor -- * Smart constructor
, static , static
, staticDevel , staticDevel
, embed
-- * Combining CSS/JS -- * Combining CSS/JS
-- $combining -- $combining
, combineStylesheets' , combineStylesheets'
@ -54,6 +53,8 @@ module Yesod.Static
, publicFiles , publicFiles
-- * Hashing -- * Hashing
, base64md5 , base64md5
-- * Embed
, embed
#ifdef TEST_EXPORT #ifdef TEST_EXPORT
, getFileListPieces , getFileListPieces
#endif #endif
@ -134,8 +135,11 @@ staticDevel dir = do
hashLookup <- cachedETagLookupDevel dir hashLookup <- cachedETagLookupDevel dir
return $ Static $ webAppSettingsWithLookup (F.decodeString dir) hashLookup return $ Static $ webAppSettingsWithLookup (F.decodeString dir) hashLookup
-- | Produce a 'Static' based on embedding all of the static -- | Produce a 'Static' based on embedding all of the static files' contents in the
-- files' contents in the executable at compile time. -- executable at compile time.
--
-- You should use "Yesod.EmbeddedStatic" instead, it is much more powerful.
--
-- Nota Bene: if you replace the scaffolded 'static' call in Settings/StaticFiles.hs -- Nota Bene: if you replace the scaffolded 'static' call in Settings/StaticFiles.hs
-- you will need to change the scaffolded addStaticContent. Otherwise, some of your -- you will need to change the scaffolded addStaticContent. Otherwise, some of your
-- assets will be 404'ed. This is because by default yesod will generate compile those -- assets will be 404'ed. This is because by default yesod will generate compile those

View File

@ -1,23 +1,42 @@
{-# LANGUAGE QuasiQuotes, TypeFamilies, MultiParamTypeClasses #-} {-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies #-}
{-# LANGUAGE OverloadedStrings #-} -- | This embeds just a single file; it embeds the source code file
{-# LANGUAGE TemplateHaskell #-} -- \"sample-embed.hs\" from the current directory so when you compile,
import Yesod.Static -- the sample-embed.hs file must be in the current directory.
import Yesod.Dispatch --
-- Try toggling the development argument to 'mkEmbeddedStatic'. When the
-- development argument is true the file \"sample-embed.hs\" is reloaded
-- from disk on every request (try changing it after you start the server).
-- When development is false, the contents are embedded and the sample-embed.hs
-- file does not even need to be present during runtime.
module Main where
import Yesod.Core import Yesod.Core
import Network.Wai.Handler.Warp (run) import Yesod.EmbeddedStatic
staticFiles "." mkEmbeddedStatic False "eStatic" [embedFile "sample-embed.hs"]
data Sample = Sample -- The above will generate variables
getStatic _ = $(embed "tests") -- eStatic :: EmbeddedStatic
mkYesod "Sample" [parseRoutes| -- sample_embed_hs :: Route EmbeddedStatic
/ RootR GET
/static StaticR Static getStatic data MyApp = MyApp { getStatic :: EmbeddedStatic }
mkYesod "MyApp" [parseRoutes|
/ HomeR GET
/static StaticR EmbeddedStatic getStatic
|] |]
instance Yesod Sample where approot _ = ""
getRootR = do instance Yesod MyApp where
redirectText RedirectPermanent "static" addStaticContent = embedStaticContent getStatic StaticR Right
return ()
main = toWaiApp Sample >>= run 3000 getHomeR :: Handler Html
getHomeR = defaultLayout $ do
toWidget [julius|console.log("Hello World");|]
[whamlet|
<h1>Hello
<p>Check the
<a href=@{StaticR sample_embed_hs}>embedded file
|]
main :: IO ()
main = warp 3000 $ MyApp eStatic

View File

@ -12,6 +12,8 @@ build-type: Simple
homepage: http://www.yesodweb.com/ homepage: http://www.yesodweb.com/
description: Static file serving subsite for Yesod Web Framework. description: Static file serving subsite for Yesod Web Framework.
extra-source-files: extra-source-files:
sample.hs
sample-embed.hs
test/*.hs test/*.hs
test/fs/bar/baz test/fs/bar/baz
test/fs/tmp/ignored test/fs/tmp/ignored