yesod-static 1.0.0.3 → 1.1.0
raw patch · 4 files changed
+36/−36 lines, 4 filesdep +system-filepathdep ~conduitdep ~crypto-conduitdep ~hspec
Dependencies added: system-filepath
Dependency ranges changed: conduit, crypto-conduit, hspec, http-types, wai, wai-app-static, yesod-core
Files
- Yesod/Static.hs +21/−21
- test/YesodStaticTest.hs +3/−5
- test/tests.hs +1/−1
- yesod-static.cabal +11/−9
Yesod/Static.hs view
@@ -78,19 +78,15 @@ import Data.Conduit (($$)) import Data.Conduit.List (sourceList) import Data.Functor.Identity (runIdentity)+import qualified Filesystem.Path.CurrentOS as F import Network.Wai.Application.Static ( StaticSettings (..)- , defaultWebAppSettings , staticApp- , embeddedLookup- , toEmbedded- , toFilePath- , fromFilePath- , FilePath- , ETagLookup , webAppSettingsWithLookup+ , embeddedSettings )+import WaiAppStatic.Storage.Filesystem (ETagLookup) -- | Type used for the subsite with static contents. newtype Static = Static StaticSettings@@ -106,7 +102,7 @@ static :: Prelude.FilePath -> IO Static static dir = do hashLookup <- cachedETagLookup dir- return $ Static $ webAppSettingsWithLookup (toFilePath dir) hashLookup+ return $ Static $ webAppSettingsWithLookup (F.decodeString dir) hashLookup -- | Same as 'static', but does not assumes that the files do not -- change and checks their modification time whenever a request@@ -114,15 +110,19 @@ staticDevel :: Prelude.FilePath -> IO Static staticDevel dir = do hashLookup <- cachedETagLookupDevel dir- return $ Static $ webAppSettingsWithLookup (toFilePath dir) hashLookup+ 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.+-- 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+-- assets to @static/tmp@ which for 'static' is fine since they are served out of the +-- directory itself. With embedded static, that will not work. +-- You can easily change @addStaticContent@ to @\_ _ _ -> return Nothing@ as a workaround.+-- This will cause yesod to embed those assets into the generated HTML file itself. embed :: Prelude.FilePath -> Q Exp-embed fp =- [|Static (defaultWebAppSettings- { ssFolder = embeddedLookup (toEmbedded $(embedDir fp))- })|]+embed fp = [|Static (embeddedSettings $(embedDir fp))|] instance RenderRoute Static where -- | A route on the static subsite (see also 'staticFiles').@@ -145,10 +145,10 @@ instance Yesod master => YesodDispatch Static master where -- Need to append trailing slash to make relative links work- yesodDispatch _ _ _ _ _ _ [] _ req =+ yesodDispatch _ _ _ _ _ _ _ [] _ req = return $ responseLBS status301 [("Location", rawPathInfo req `S.append` "/")] "" - yesodDispatch _ (Static set) _ _ _ _ textPieces _ req =+ yesodDispatch _ _ (Static set) _ _ _ _ textPieces _ req = staticApp set req { pathInfo = textPieces } notHidden :: Prelude.FilePath -> Bool@@ -226,18 +226,18 @@ publicFiles dir = mkStaticFiles' dir "StaticRoute" False -mkHashMap :: Prelude.FilePath -> IO (M.Map FilePath S8.ByteString)+mkHashMap :: Prelude.FilePath -> IO (M.Map F.FilePath S8.ByteString) mkHashMap dir = do fs <- getFileListPieces dir hashAlist fs >>= return . M.fromList where- hashAlist :: [[String]] -> IO [(FilePath, S8.ByteString)]+ hashAlist :: [[String]] -> IO [(F.FilePath, S8.ByteString)] hashAlist fs = mapM hashPair fs where- hashPair :: [String] -> IO (FilePath, S8.ByteString)+ hashPair :: [String] -> IO (F.FilePath, S8.ByteString) hashPair pieces = do let file = pathFromRawPieces dir pieces h <- base64md5File file- return (toFilePath file, S8.pack h)+ return (F.decodeString file, S8.pack h) pathFromRawPieces :: Prelude.FilePath -> [String] -> Prelude.FilePath pathFromRawPieces =@@ -248,12 +248,12 @@ cachedETagLookupDevel :: Prelude.FilePath -> IO ETagLookup cachedETagLookupDevel dir = do etags <- mkHashMap dir- mtimeVar <- newIORef (M.empty :: M.Map FilePath EpochTime)+ mtimeVar <- newIORef (M.empty :: M.Map F.FilePath EpochTime) return $ \f -> case M.lookup f etags of Nothing -> return Nothing Just checksum -> do- fs <- getFileStatus $ fromFilePath f+ fs <- getFileStatus $ F.encodeString f let newt = modificationTime fs mtimes <- readIORef mtimeVar oldt <- case M.lookup f mtimes of
test/YesodStaticTest.hs view
@@ -6,11 +6,9 @@ import Yesod.Static (getFileListPieces) -specs :: Specs-specs = [- describe "get file list" [+specs :: Spec+specs = do+ describe "get file list" $ do it "pieces" $ do x <- getFileListPieces "test/fs" x @?= [["foo"], ["bar", "baz"]]- ]- ]
test/tests.hs view
@@ -4,4 +4,4 @@ import YesodStaticTest (specs) main :: IO ()-main = hspecX specs+main = hspec specs
yesod-static.cabal view
@@ -1,5 +1,5 @@ name: yesod-static-version: 1.0.0.3+version: 1.1.0 license: MIT license-file: LICENSE author: Michael Snoyman <michael@snoyman.com>@@ -19,22 +19,23 @@ build-depends: base >= 4 && < 5 , containers >= 0.2 , old-time >= 1.0- , yesod-core >= 1.0 && < 1.1+ , yesod-core >= 1.1 && < 1.2 , base64-bytestring >= 0.1.0.1 && < 0.2 , cereal >= 0.3 && < 0.4 , bytestring >= 0.9.1.4 , template-haskell , directory >= 1.0 && < 1.2 , transformers >= 0.2.2 && < 0.4- , wai-app-static >= 1.2 && < 1.3- , wai >= 1.2 && < 1.3+ , wai-app-static >= 1.3 && < 1.4+ , wai >= 1.3 && < 1.4 , text >= 0.9 && < 1.0 , file-embed >= 0.0.4.1 && < 0.5- , http-types >= 0.6.5 && < 0.7+ , http-types >= 0.7 && < 0.8 , unix-compat >= 0.2- , conduit >= 0.4 && < 0.5- , crypto-conduit >= 0.3 && < 0.4+ , conduit >= 0.5 && < 0.6+ , crypto-conduit >= 0.4 && < 0.5 , cryptohash >= 0.6.1+ , system-filepath >= 0.4.6 && < 0.5 exposed-modules: Yesod.Static ghc-options: -Wall @@ -44,7 +45,7 @@ type: exitcode-stdio-1.0 cpp-options: -DTEST_EXPORT build-depends: base- , hspec >= 1.0 && < 1.2+ , hspec >= 1.3 && < 1.4 , HUnit -- copy from above , containers >= 0.2@@ -60,11 +61,12 @@ , wai , text >= 0.9 && < 1.0 , file-embed >= 0.0.4.1 && < 0.5- , http-types >= 0.6.5 && < 0.7+ , http-types , unix-compat >= 0.2 , conduit , crypto-conduit , cryptohash >= 0.6.1+ , system-filepath ghc-options: -Wall