8000 Add context to prepareRoutes by jack-davies · Pull Request #121 · aesiniath/unbeliever · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add context to prepareRoutes #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core-webserver-servant/core-webserver-servant.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: core-webserver-servant
version: 0.1.0.0
version: 0.1.1.0
synopsis: Interoperability with Servant
description: This is part of a library to help build command-line programs, both tools and
longer-running daemons.
Expand Down
46 changes: 36 additions & 10 deletions core-webserver-servant/lib/Core/Webserver/Servant.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ main = do
-}
module Core.Webserver.Servant (
prepareRoutes,
prepareRoutesWithContext,
) where

import Control.Monad.Except (ExceptT (..))
import Core.Program
import Core.System (Exception (..))
import Core.Telemetry.Observability (clearMetrics)
import Core.Webserver.Warp
import Data.Proxy (Proxy)
import GHC.Base (Type)
import Network.Wai (Application)
import Servant (Handler (..), ServerT)
import Servant.Server (HasServer, hoistServer, serve)
import Core.Telemetry.Observability (clearMetrics)
import qualified Servant as Servant (
Handler (..),
ServerT,
)
import qualified Servant.Server as Servant (
Context (..),
HasServer,
ServerContext,
serveWithContextT,
)

data ContextNotFoundInRequest = ContextNotFoundInRequest deriving (Show)

Expand All @@ -65,11 +74,26 @@ logging and telemetry facilities of __core-program__ and __core-telemetry__.
-}
prepareRoutes ::
forall τ (api :: Type).
HasServer api '[] =>
Servant.HasServer api '[] 8000 =>
Proxy api ->
Servant.ServerT api (Program τ) ->
Program τ Application
prepareRoutes proxy = prepareRoutesWithContext proxy Servant.EmptyContext

{- |
Prepare routes as with 'prepareRoutes' above, but providing a __servant__
'Servant.Server.Context' in order to give detailed control of the setup.

@since 0.1.1
-}
prepareRoutesWithContext ::
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want this part of the public API we should add a Haddock documentation, even if it just has

{-| 
Prepare routes as above, but providing a Context.

@since 0.1.1
-}

in it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(I can do these things if you're busy)

forall τ (api :: Type) context.
(Servant.HasServer api context, Servant.ServerContext context) =>
Proxy api ->
ServerT api (Program τ) ->
Servant.Context context ->
Servant.ServerT api (Program τ) ->
Program τ Application
prepareRoutes proxy (routes :: ServerT api (Program τ)) =
prepareRoutesWithContext proxy sContext (routes :: Servant.ServerT api (Program τ)) =
pure application
where
application :: Application
Expand All @@ -85,17 +109,19 @@ prepareRoutes proxy (routes :: ServerT api (Program τ)) =
context <- case contextFromRequest @τ request of
Just context' -> pure context'
Nothing -> throw ContextNotFoundInRequest
serve
Servant.serveWithContextT
proxy
(hoistServer proxy (transformProgram context) routes)
sContext
(transformProgram context)
routes
request
sendResponse

transformProgram :: Context τ -> Program τ α -> Handler α
transformProgram :: Context τ -> Program τ α -> Servant.Handler α
transformProgram context program =
let output =
try $
subProgram context $ do
clearMetrics
program
in Handler (ExceptT output)
in Servant.Handler (ExceptT output)
2 changes: 1 addition & 1 deletion core-webserver-servant/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: core-webserver-servant
version: 0.1.0.0
version: 0.1.1.0
synopsis: Interoperability with Servant
description: |
This is part of a library to help build command-line programs, both tools and
Expand Down
4 changes: 3 additions & 1 deletion stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ packages:
- ./core-webserver-warp
- .
extra-deps:
- hashable-1.3.5.0
- semialign-1.2
- servant-0.19
- servant-server-0.19.1
- time-compat-1.9.6.1
- hashable-1.3.5.0
0