-
-
Notifications
You must be signed in to change notification settings - Fork 419
New combinator to return routed path in response headers #1561
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
Open
nbacquey
wants to merge
3
commits into
haskell-servant:master
Choose a base branch
from
nbacquey:server-routing-header
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
synopsis: New combinator to return routed path in response headers | ||
prs: #1561 | ||
issues: #1553 | ||
|
||
description: { | ||
|
||
This commit introduces a new type-level combinator, `WithRoutingHeader`. | ||
It modifies the behaviour of the following sub-API, such that all endpoints of said API return an additional routing header in their response. | ||
|
||
A routing header is a header that specifies which endpoint the incoming request was routed to. | ||
Endpoint are designated by their path, in which `Capture'` and `CaptureAll` combinators are replaced by a capture hint. | ||
|
||
This header can be used by downstream middlewares to gather information about individual endpoints, since in most cases | ||
a routing header uniquely identifies a single endpoint. | ||
|
||
Example: | ||
|
||
```haskell | ||
type MyApi = | ||
WithRoutingHeader :> "by-id" :> Capture "id" Int :> Get '[JSON] Foo | ||
-- GET /by-id/1234 will return a response with the following header: | ||
-- ("Servant-Routed-Path", "/by-id/<id::Int>") | ||
``` | ||
|
||
To achieve this, two refactorings were necessary: | ||
|
||
* Introduce a type `RouterEnv env` to encapsulate the `env` type (as in `Router env a`), which contains a tuple-encoded list of url pieces parsed from the incoming request. This type makes it possible to pass more information throughout the routing process, and the computation of the `Delayed env c` associated with each request. | ||
* Introduce a new kind of router, which only modifies the `RouterEnv`, and doesn't affect the routing process otherwise: `EnvRouter (RouterEnv env -> RouterEnv env) (Router' env a)`. | ||
This new router is used when encountering the `WithRoutingHeader` combinator in an API, to notify the endpoints of the sub-API that they must produce a routing header (this behaviour is disabled by default). | ||
|
||
This PR also introduces `Spec` tests for the `WithRoutingHeader` combinator, which showcase some of its possible uses. | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
{-# LANGUAGE DeriveFunctor #-} | ||
nbacquey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
-- | This module contains the `RouterEnv env` type and associated functions. | ||
-- `RouterEnv env` encapsulates the `env` type (as in `Router env a`), | ||
-- which contains a tuple-encoded list of url pieces parsed from the incoming request. | ||
-- The encapsulation makes it possible to pass more information throughout | ||
-- the routing process, and ultimately to the computation of the `Delayed env c` | ||
-- associated with each request. | ||
-- The type and functions have been designed to be extensible: it should remain easy | ||
-- to add a new field to the record and manipulate it. | ||
-- | ||
-- @since 0.20 | ||
-- | ||
module Servant.Server.Internal.RouterEnv where | ||
|
||
import Data.Text | ||
(Text) | ||
import qualified Data.Text as T | ||
import Data.Typeable | ||
(TypeRep) | ||
import Network.HTTP.Types.Header | ||
(HeaderName) | ||
|
||
data RouterEnv env = RouterEnv | ||
{ routedPath :: [PathPiece] | ||
, shouldReturnRoutedPath :: Bool | ||
, routerEnv :: env | ||
} | ||
deriving Functor | ||
|
||
emptyEnv :: a -> RouterEnv a | ||
emptyEnv v = RouterEnv [] False v | ||
|
||
enableRoutingHeaders :: RouterEnv env -> RouterEnv env | ||
enableRoutingHeaders env = env { shouldReturnRoutedPath = True } | ||
|
||
routedPathRepr :: RouterEnv env -> Text | ||
routedPathRepr RouterEnv{routedPath = path} = | ||
"/" <> T.intercalate "/" (map go $ reverse path) | ||
where | ||
go (StaticPiece p) = p | ||
go (CapturePiece p) = toCaptureTags p | ||
|
||
data PathPiece | ||
= StaticPiece Text | ||
| CapturePiece [CaptureHint] | ||
|
||
appendPathPiece :: PathPiece -> RouterEnv a -> RouterEnv a | ||
appendPathPiece p env@RouterEnv{..} = env { routedPath = p:routedPath } | ||
|
||
data CaptureHint = CaptureHint | ||
{ captureName :: Text | ||
, captureType :: TypeRep | ||
} | ||
deriving (Show, Eq) | ||
|
||
toCaptureTag :: CaptureHint -> Text | ||
toCaptureTag hint = captureName hint <> "::" <> (T.pack . show) (captureType hint) | ||
|
||
toCaptureTags :: [CaptureHint] -> Text | ||
toCaptureTags hints = "<" <> T.intercalate "|" (map toCaptureTag hints) <> ">" | ||
|
||
hRoutedPathHeader :: HeaderName | ||
hRoutedPathHeader = "Servant-Routed-Path" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.