Skip to content

Commit

Permalink
[haskell][server] Export the Wai Application for the server (#9801)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Profpatsch committed Jul 13, 2021
1 parent aa88d5b commit 7bd633f
Showing 1 changed file with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ module {{title}}.API
, {{title}}ClientError(..)
-- ** Servant
, {{title}}API
-- ** Plain WAI Application
, serverWaiApplication{{title}}
) where

import {{title}}.Types
Expand Down Expand Up @@ -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}} :<|>
Expand Down

0 comments on commit 7bd633f

Please sign in to comment.