packages feed

wai-middleware-static-embedded (empty) → 0.1.0.0

raw patch · 6 files changed

+150/−0 lines, 6 filesdep +basedep +bytestringdep +cryptonitesetup-changed

Dependencies added: base, bytestring, cryptonite, http-types, memory, mime-types, text, wai, wai-extra

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Adam Sandberg Eriksson (c) 2017++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Adam Sandberg Eriksson nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,20 @@+# Static embedded middleware++A `middleware` to serve static files from memory. Works particularly+well with `file-embed`.++## Example usage++```haskell+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}++import Network.Wai.Middleware.StaticEmbedded -- This package+import Data.FileEmbed -- file-embed+import Web.Scotty -- scotty++main :: IO ()+main = scotty 1337 $ do+  middleware (static $(embedDir ".")) -- serves the source directory+  notFound (text "404: Not found!")+```
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ example/Main.hs view
@@ -0,0 +1,11 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE TemplateHaskell #-}++import Network.Wai.Middleware.StaticEmbedded -- This package+import Data.FileEmbed -- file-embed+import Web.Scotty -- scotty++main :: IO ()+main = scotty 1337 $ do+  middleware (static $(embedDir ".")) -- serves the source directory+  notFound (text "404: Not found!")
+ src/Network/Wai/Middleware/StaticEmbedded.hs view
@@ -0,0 +1,41 @@+{-# LANGUAGE OverloadedStrings #-}+-- |++module Network.Wai.Middleware.StaticEmbedded (static) where++import           Crypto.Hash+import           Data.ByteArray.Encoding+import qualified Data.ByteString as B+import qualified Data.ByteString.Lazy as BL+import           Data.Maybe+import qualified Data.Text as T+import           Network.HTTP.Types (status200, status304)+import           Network.Mime (defaultMimeLookup)+import           Network.Wai++static :: [(FilePath, B.ByteString)] -> Middleware+static files =+  let files' = map computeEtag files+  in \app req callback ->+       fromMaybe (app req callback) $ do+         let fileName = T.unpack . T.intercalate "/" $ pathInfo req+         (bs, etag) <- lookup fileName files'+         let mime = defaultMimeLookup (T.pack fileName)+         let hdrs = computeHeaders etag+         return . callback $+           if Just etag == lookup "If-None-Match" (requestHeaders req)+             then responseLBS status304 hdrs BL.empty+             else responseLBS status200 (("Content-Type", mime) : hdrs) bs+  where+    computeHeaders etag =+      [ ("Cache-Control", "no-transform,public,max-age=300,s-maxage=900")+      , ("ETag", etag)+      , ("Vary", "Accept-Encoding")+      ]++computeEtag+  :: (FilePath, B.ByteString)+  -> (FilePath, (BL.ByteString, B.ByteString))+computeEtag (fp, bs) =+  let bs' = BL.fromStrict bs+  in (fp, (bs', convertToBase Base16 (hashlazy bs' :: Digest SHA1)))
+ wai-middleware-static-embedded.cabal view
@@ -0,0 +1,46 @@+name:                wai-middleware-static-embedded+version:             0.1.0.0+synopsis:            Serve embedded static files as a Wai middleware+description:         Please see README.md+homepage:            https://github.com/adamse/network-wai-static-embedded#readme+license:             BSD3+license-file:        LICENSE+author:              Adam Sandberg Eriksson+maintainer:          adam@sandbergericsson.se+copyright:           Copyright: (c) 2017 Adam Sandberg Eriksson+category:            Web+build-type:          Simple+extra-source-files:  README.md+cabal-version:       >=1.10++extra-source-files:+                   README.md+                   example/Main.hs++library+  hs-source-dirs:      src+  exposed-modules:     Network.Wai.Middleware.StaticEmbedded+  build-depends:       base >= 4.7 && < 5+                     , bytestring+                     , cryptonite+                     , memory+                     , mime-types+                     , wai+                     , wai-extra+                     , http-types+                     , text++  default-language:    Haskell2010++-- executable example+--   hs-source-dirs:      example+--   main-is:             Main.hs+--   build-depends:       base >= 4.7 && < 5+--                      , wai-middleware-static-embedded+--                      , scotty+--                      , file-embed+--   default-language:    Haskell2010++source-repository head+  type:     git+  location: https://github.com/adamse/wai-middleware-static-embedded