From 02e7fe0ab9fa0af06b1e2ec21cecfae405d39fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9cate=20Moonlight?= Date: Fri, 20 Dec 2024 00:18:43 +0100 Subject: [PATCH] Support middleware --- servant-effectful.cabal | 1 + src/Effectful/Servant.hs | 10 ++++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/servant-effectful.cabal b/servant-effectful.cabal index e3454ef..0eeb1f2 100644 --- a/servant-effectful.cabal +++ b/servant-effectful.cabal @@ -58,6 +58,7 @@ library , mtl >=2 , servant-server ^>=0.20 , warp >=3.3 + , wai >=3.2.0 test-suite servant-effectful-test import: common-extensions diff --git a/src/Effectful/Servant.hs b/src/Effectful/Servant.hs index 51e67b8..f324848 100644 --- a/src/Effectful/Servant.hs +++ b/src/Effectful/Servant.hs @@ -21,6 +21,7 @@ import Effectful.Dispatch.Static.Primitive (Env, cloneEnv) import Effectful.Error.Static import qualified Network.Wai.Handler.Warp as Warp import Servant hiding ((:>)) +import qualified Network.Wai as Wai -- | Deploy an effectful server. runWarpServerSettings @@ -28,9 +29,9 @@ runWarpServerSettings . (HasServer api '[], IOE :> es) => Warp.Settings -> ServerT api (Eff (Error ServerError : es)) + -> Wai.Middleware -> Eff es () -runWarpServerSettings settings = - runWarpServerSettingsContext @api settings EmptyContext +runWarpServerSettings settings = runWarpServerSettingsContext @api settings EmptyContext -- | Deploy an effectful server with a context. runWarpServerSettingsContext @@ -39,10 +40,11 @@ runWarpServerSettingsContext => Warp.Settings -> Context context -> ServerT api (Eff (Error ServerError : es)) + -> Wai.Middleware -> Eff es () -runWarpServerSettingsContext settings ctx server = do +runWarpServerSettingsContext settings context server middleware = do unsafeEff $ \es -> do - Warp.runSettings settings (serveEff @api es ctx server) + Warp.runSettings settings (middleware (serveEff @api es context server)) -- | Convert an effectful server into a wai application. serveEff