Skip to content

Commit

Permalink
static: update sample-embed.hs and point the old embedded static to t…
Browse files Browse the repository at this point in the history
…he new one
  • Loading branch information
John Lenz committed Sep 12, 2013
1 parent 2ad3977 commit 8e16fd2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
10 changes: 7 additions & 3 deletions yesod-static/Yesod/Static.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ module Yesod.Static
-- * Smart constructor
, static
, staticDevel
, embed
-- * Combining CSS/JS
-- $combining
, combineStylesheets'
Expand All @@ -54,6 +53,8 @@ module Yesod.Static
, publicFiles
-- * Hashing
, base64md5
-- * Embed
, embed
#ifdef TEST_EXPORT
, getFileListPieces
#endif
Expand Down Expand Up @@ -134,8 +135,11 @@ staticDevel dir = do
hashLookup <- cachedETagLookupDevel dir
return $ Static $ webAppSettingsWithLookup (F.decodeString dir) hashLookup

-- | Produce a 'Static' based on embedding all of the static
-- files' contents in the executable at compile time.
-- | Produce a 'Static' based on embedding all of the static files' contents in the
-- 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
-- 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
Expand Down
53 changes: 36 additions & 17 deletions yesod-static/sample-embed.hs
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
{-# LANGUAGE QuasiQuotes, TypeFamilies, MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}
import Yesod.Static
import Yesod.Dispatch
{-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies #-}
-- | This embeds just a single file; it embeds the source code file
-- \"sample-embed.hs\" from the current directory so when you compile,
-- the sample-embed.hs file must be in the current directory.
--
-- 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 Network.Wai.Handler.Warp (run)
import Yesod.EmbeddedStatic

mkEmbeddedStatic False "eStatic" [embedFile "sample-embed.hs"]

-- The above will generate variables
-- eStatic :: EmbeddedStatic
-- sample_embed_hs :: Route EmbeddedStatic

staticFiles "."
data MyApp = MyApp { getStatic :: EmbeddedStatic }

data Sample = Sample
getStatic _ = $(embed "tests")
mkYesod "Sample" [parseRoutes|
/ RootR GET
/static StaticR Static getStatic
mkYesod "MyApp" [parseRoutes|
/ HomeR GET
/static StaticR EmbeddedStatic getStatic
|]
instance Yesod Sample where approot _ = ""

getRootR = do
redirectText RedirectPermanent "static"
return ()
instance Yesod MyApp where
addStaticContent = embedStaticContent getStatic StaticR Right

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 = toWaiApp Sample >>= run 3000
main :: IO ()
main = warp 3000 $ MyApp eStatic
2 changes: 2 additions & 0 deletions yesod-static/yesod-static.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ build-type: Simple
homepage: http://www.yesodweb.com/
description: Static file serving subsite for Yesod Web Framework.
extra-source-files:
sample.hs
sample-embed.hs
test/*.hs
test/fs/bar/baz
test/fs/tmp/ignored
Expand Down

0 comments on commit 8e16fd2

Please sign in to comment.