From 7bd633f1ae0985405a16770fa108b4841dfd0a75 Mon Sep 17 00:00:00 2001 From: Profpatsch Date: Tue, 13 Jul 2021 11:00:06 +0200 Subject: [PATCH] [haskell][server] Export the Wai Application for the server (#9801) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For tests it’s useful to have direct access to the Wai `Application`, which is the plain function from `Request` to `Response`. Then you don’t need to use a full-blown http server to run requests. --- .../src/main/resources/haskell-servant/API.mustache | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/modules/openapi-generator/src/main/resources/haskell-servant/API.mustache b/modules/openapi-generator/src/main/resources/haskell-servant/API.mustache index 87e7c874aee5..f352eba419e5 100644 --- a/modules/openapi-generator/src/main/resources/haskell-servant/API.mustache +++ b/modules/openapi-generator/src/main/resources/haskell-servant/API.mustache @@ -27,6 +27,8 @@ module {{title}}.API , {{title}}ClientError(..) -- ** Servant , {{title}}API + -- ** Plain WAI Application + , serverWaiApplication{{title}} ) where import {{title}}.Types @@ -232,7 +234,13 @@ run{{title}}MiddlewareServer Config{..} middleware backend = do let warpSettings = Warp.defaultSettings & Warp.setPort (baseUrlPort url) & Warp.setHost (fromString $ baseUrlHost url) - liftIO $ Warp.runSettings warpSettings $ middleware $ serve (Proxy :: Proxy {{title}}API) (serverFromBackend backend) + liftIO $ Warp.runSettings warpSettings $ middleware $ serverWaiApplication{{title}} backend + +-- | Plain "Network.Wai" Application for the {{title}} server. +-- +-- Can be used to implement e.g. tests that call the API without a full webserver. +serverWaiApplication{{title}} :: {{title}}Backend (ExceptT ServerError IO) -> Application +serverWaiApplication{{title}} backend = serve (Proxy :: Proxy {{title}}API) (serverFromBackend backend) where serverFromBackend {{title}}Backend{..} = ({{#apis}}{{#operations}}{{#operation}}coerce {{operationId}}{{^-last}} :<|>