sample-embed.hs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. {-# LANGUAGE TemplateHaskell, QuasiQuotes, TypeFamilies #-}
  2. -- | This embeds just a single file; it embeds the source code file
  3. -- \"sample-embed.hs\" from the current directory so when you compile,
  4. -- the sample-embed.hs file must be in the current directory.
  5. --
  6. -- Try toggling the development argument to 'mkEmbeddedStatic'. When the
  7. -- development argument is true the file \"sample-embed.hs\" is reloaded
  8. -- from disk on every request (try changing it after you start the server).
  9. -- When development is false, the contents are embedded and the sample-embed.hs
  10. -- file does not even need to be present during runtime.
  11. module Main where
  12. import Yesod.Core
  13. import Yesod.EmbeddedStatic
  14. mkEmbeddedStatic False "eStatic" [embedFile "sample-embed.hs"]
  15. -- The above will generate variables
  16. -- eStatic :: EmbeddedStatic
  17. -- sample_embed_hs :: Route EmbeddedStatic
  18. data MyApp = MyApp { getStatic :: EmbeddedStatic }
  19. mkYesod "MyApp" [parseRoutes|
  20. / HomeR GET
  21. /static StaticR EmbeddedStatic getStatic
  22. |]
  23. instance Yesod MyApp where
  24. addStaticContent = embedStaticContent getStatic StaticR Right
  25. getHomeR :: Handler Html
  26. getHomeR = defaultLayout $ do
  27. toWidget [julius|console.log("Hello World");|]
  28. [whamlet|
  29. <h1>Hello
  30. <p>Check the
  31. <a href=@{StaticR sample_embed_hs}>embedded file
  32. |]
  33. main :: IO ()
  34. main = warp 3000 $ MyApp eStatic