packages feed

di-wai 0.1 → 0.2

raw patch · 3 files changed

+47/−45 lines, 3 filesdep −vaultPVP ok

version bump matches the API change (PVP)

Dependencies removed: vault

API changes (from Hackage documentation)

+ Di.Wai: middleware' :: Df1 -> (Df1 -> Application) -> Application
- Di.Wai: middleware :: MonadIO m => Df1 -> m (Middleware, Request -> Maybe Df1)
+ Di.Wai: middleware :: Df1 -> Middleware

Files

CHANGELOG.md view
@@ -1,3 +1,11 @@+# Version 0.2++* The type of `middleware` is now `Df1 -> Middleware`.++* Introduced a new function `middleware'` to replace the previous+  *lookup* functionality.++ # Version 0.1  * Initial version.
di-wai.cabal view
@@ -1,6 +1,6 @@ cabal-version: 1.18 name: di-wai-version: 0.1+version: 0.2 author: Renzo Carbonara maintainer: renλren.zone copyright: Renzo Carbonara 2024@@ -23,7 +23,6 @@     clock,     df1-wai,     di-df1,-    vault,     wai   ghc-options: -Wall -O2 
lib/Di/Wai.hs view
@@ -6,56 +6,51 @@ -- @ -- import qualified "Di.Wai" -- @-module Di.Wai (middleware) where+module Di.Wai (middleware, middleware') where -import Control.Monad.IO.Class import Data.Foldable import Data.IORef-import Data.Vault.Lazy qualified as V import Data.Word import Df1.Wai qualified-import Di.Df1 qualified+import Di.Df1 qualified as Di import Network.Wai qualified as Wai import System.Clock qualified as Clock+import System.IO.Unsafe +refReqId :: IORef Word64+refReqId = unsafePerformIO $ newIORef 0+{-# NOINLINE refReqId #-}++-- | Like 'middleware', but it exposes the 'Di.Df1' that includes 'Df1.Path'+-- data about 'Wai.Request' to the underlying 'Wai.Application'.+middleware'+   :: Di.Df1+   -> (Di.Df1 -> Wai.Application)+   -> Wai.Application+middleware' di0 = \fapp req respond -> do+   t0 <- Clock.getTime Clock.Monotonic+   reqId <- atomicModifyIORef' refReqId $ \ol -> (ol + 1, ol)+   let di1 =+         foldl'+            (\di (k, v) -> Di.attr k v di)+            (Di.push "http" di0)+            (("request", Di.value reqId) : Df1.Wai.request req)+   Di.info_ di1 "Request coming in"+   fapp di1 req $ \res -> do+      t1 <- Clock.getTime Clock.Monotonic+      let td = Clock.toNanoSecs t1 - Clock.toNanoSecs t0+          di2 =+            foldl'+               (\di (k, v) -> Di.attr k v di)+               di1+               (("nanoseconds", Di.value td) : Df1.Wai.response res)+      Di.info_ di2 "Response going out"+      respond res+ -- | Obtain a 'Wai.Middleware' that will log incomming 'Wai.Request's--- and outgoing 'Wai.Response's.------ @--- do (__middleware__, __lookup__) <- "Di.Wai".'middleware' di--- @------ * The obtained @__middleware__@ shall be applied to your 'Wai.Application'.+-- and outgoing 'Wai.Response's through the given 'Di.Df1'. ----- * The obtained @__lookup__@ function can be used to obtain the 'Di.Df1.Df1'--- that includes 'Df1.Path' data about 'Wai.Request'. It returns 'Nothing' if--- this particular @__middleware__@ was not used on the given 'Wai.Request'.-middleware-   :: (MonadIO m)-   => Di.Df1.Df1-   -> m (Wai.Middleware, Wai.Request -> Maybe Di.Df1.Df1)-middleware di0 = liftIO $ do-   ref :: IORef Word64 <- newIORef 0-   vk :: V.Key Di.Df1.Df1 <- V.newKey-   pure-      ( \app req respond -> do-         t0 <- Clock.getTime Clock.Monotonic-         reqId <- atomicModifyIORef' ref $ \ol -> (ol + 1, ol)-         let di1 =-               foldl'-                  (\di (k, v) -> Di.Df1.attr k v di)-                  (Di.Df1.push "http" di0)-                  (("request", Di.Df1.value reqId) : Df1.Wai.request req)-         Di.Df1.info_ di1 "Request coming in"-         app (req{Wai.vault = V.insert vk di1 (Wai.vault req)}) $ \res -> do-            t1 <- Clock.getTime Clock.Monotonic-            let td = Clock.toNanoSecs t1 - Clock.toNanoSecs t0-                di2 =-                  foldl'-                     (\di (k, v) -> Di.Df1.attr k v di)-                     di1-                     (("nanoseconds", Di.Df1.value td) : Df1.Wai.response res)-            Di.Df1.info_ di2 "Response going out"-            respond res-      , \req -> V.lookup vk (Wai.vault req)-      )+-- If your 'Wai.Application' needs access to the 'Di.Df1' uniquely tied+-- to the current 'Wai.Request', then use 'middleware'' instead.+middleware :: Di.Df1 -> Wai.Middleware+middleware di = middleware' di . const