mirror of https://github.com/dapr/docs.git
Updated docs for authoring middleware components in 1.10
Fixes #2993 Signed-off-by: ItalyPaleAle <43508+ItalyPaleAle@users.noreply.github.com>
This commit is contained in:
parent
a547209996
commit
6415f00c55
|
@ -11,27 +11,36 @@ aliases:
|
||||||
|
|
||||||
Dapr allows custom processing pipelines to be defined by chaining a series of middleware components. In this guide, you'll learn how to create a middleware component. To learn how to configure an existing middleware component, see [Configure middleware components]({{< ref middleware.md >}})
|
Dapr allows custom processing pipelines to be defined by chaining a series of middleware components. In this guide, you'll learn how to create a middleware component. To learn how to configure an existing middleware component, see [Configure middleware components]({{< ref middleware.md >}})
|
||||||
|
|
||||||
## Writing a custom middleware
|
## Writing a custom HTTP middleware
|
||||||
|
|
||||||
Dapr uses [FastHTTP](https://github.com/valyala/fasthttp) to implement its HTTP server. Hence, your HTTP middleware needs to be written as a FastHTTP handler. Your middleware needs to implement a middleware interface, which defines a **GetHandler** method that returns **fasthttp.RequestHandler** and **error**:
|
HTTP middlewares in Dapr wrap standard Go [net/http](https://pkg.go.dev/net/http) handler functions.
|
||||||
|
|
||||||
|
Your middleware needs to implement a middleware interface, which defines a **GetHandler** method that returns a [**http.Handler**](https://pkg.go.dev/net/http#Handler) callback and an **error**:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Middleware interface {
|
type Middleware interface {
|
||||||
GetHandler(metadata Metadata) (func(h fasthttp.RequestHandler) fasthttp.RequestHandler, error)
|
GetHandler(metadata middleware.Metadata) (func(next http.Handler) http.Handler, error)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Your handler implementation can include any inbound logic, outbound logic, or both:
|
The handler receives a `next` callback that should be invoked to continue processing the request.
|
||||||
|
|
||||||
|
Your handler implementation can include an inbound logic, outbound logic, or both:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
func (m *customMiddleware) GetHandler(metadata Metadata) (func(fasthttp.RequestHandler) fasthttp.RequestHandler, error) {
|
func (m *customMiddleware) GetHandler(metadata middleware.Metadata) (func(next http.Handler) http.Handler, error) {
|
||||||
var err error
|
var err error
|
||||||
return func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
|
return func(next http.Handler) http.Handler {
|
||||||
return func(ctx *fasthttp.RequestCtx) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// inbound logic
|
// Inbound logic
|
||||||
h(ctx) // call the downstream handler
|
// ...
|
||||||
// outbound logic
|
|
||||||
|
// Call the next handler
|
||||||
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
|
// Outbound logic
|
||||||
|
// ...
|
||||||
}
|
}
|
||||||
}, err
|
}, err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,22 +6,22 @@ weight: 2000
|
||||||
description: "Customize processing pipelines by adding middleware components"
|
description: "Customize processing pipelines by adding middleware components"
|
||||||
---
|
---
|
||||||
|
|
||||||
Dapr allows custom processing pipelines to be defined by chaining a series of middleware components. There are two places that you can use a middleware pipeline;
|
Dapr allows custom processing pipelines to be defined by chaining a series of middleware components. There are two places that you can use a middleware pipeline:
|
||||||
|
|
||||||
1) Building block APIs - HTTP middleware components are executed when invoking any Dapr HTTP APIs.
|
1. Building block APIs - HTTP middleware components are executed when invoking any Dapr HTTP APIs.
|
||||||
2) Service-to-Service invocation - HTTP middleware components are applied to service-to-service invocation calls.
|
2. Service-to-Service invocation - HTTP middleware components are applied to service-to-service invocation calls.
|
||||||
|
|
||||||
## Configure API middleware pipelines
|
## Configure API middleware pipelines
|
||||||
|
|
||||||
When launched, a Dapr sidecar constructs a middleware processing pipeline for incoming HTTP calls. By default, the pipeline consists of [tracing middleware]({{< ref tracing-overview.md >}}) and CORS middleware. Additional middleware, configured by a Dapr [configuration]({{< ref configuration-concept.md >}}), can be added to the pipeline in the order they are defined. The pipeline applies to all Dapr API endpoints, including state, pub/sub, service invocation, bindings, secrets, configuration, distributed lock, and others.
|
When launched, a Dapr sidecar constructs a middleware processing pipeline for incoming HTTP calls. By default, the pipeline consists of the [tracing]({{< ref tracing-overview.md >}}) and CORS middlewares. Additional middlewares, configured by a Dapr [Configuration]({{< ref configuration-concept.md >}}), can be added to the pipeline in the order they are defined. The pipeline applies to all Dapr API endpoints, including state, pubsub, service invocation, bindings, secrets, configuration, distributed lock, etc.
|
||||||
|
|
||||||
A request goes through all the defined middleware components before it's routed to user code, and then goes through the defined middleware, in reverse order, before it's returned to the client, as shown in the following diagram.
|
A request goes through all the defined middleware components before it's routed to user code, and then goes through the defined middleware, in reverse order, before it's returned to the client, as shown in the following diagram.
|
||||||
|
|
||||||
<img src="/images/middleware.png" width=800>
|
<img src="/images/middleware.png" width="800" alt="Diagram showing the flow of a request and a response through the middlewares, as described in the paragraph above" />
|
||||||
|
|
||||||
HTTP middleware components are executed when invoking Dapr HTTP APIs using the `httpPipeline` configuration.
|
HTTP middleware components are executed when invoking Dapr HTTP APIs using the `httpPipeline` configuration.
|
||||||
|
|
||||||
The following configuration example defines a custom pipeline that uses a [OAuth 2.0 middleware]({{< ref middleware-oauth2.md >}}) and an [uppercase middleware component]({{< ref middleware-uppercase.md >}}). In this case, all requests are authorized through the OAuth 2.0 protocol, and transformed to uppercase text, before they are forwarded to user code.
|
The following configuration example defines a custom pipeline that uses an [OAuth 2.0 middleware]({{< ref middleware-oauth2.md >}}) and an [uppercase middleware component]({{< ref middleware-uppercase.md >}}). In this case, all requests are authorized through the OAuth 2.0 protocol, and transformed to uppercase text, before they are forwarded to user code.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: dapr.io/v1alpha1
|
apiVersion: dapr.io/v1alpha1
|
||||||
|
@ -38,19 +38,19 @@ spec:
|
||||||
type: middleware.http.uppercase
|
type: middleware.http.uppercase
|
||||||
```
|
```
|
||||||
|
|
||||||
As with other components, middleware components can be found in the [supported Middleware reference]({{< ref supported-middleware >}}) and in the [components-contrib repo](https://github.com/dapr/components-contrib/tree/master/middleware/http).
|
As with other components, middleware components can be found in the [supported Middleware reference]({{< ref supported-middleware >}}) and in the [`dapr/components-contrib` repo](https://github.com/dapr/components-contrib/tree/master/middleware/http).
|
||||||
|
|
||||||
{{< button page="supported-middleware" text="See all middleware components">}}
|
{{< button page="supported-middleware" text="See all middleware components">}}
|
||||||
|
|
||||||
## Configure app middleware pipelines
|
## Configure app middleware pipelines
|
||||||
|
|
||||||
You can also use any middleware components when making service-to-service invocation calls. For example, for token validation in a zero-trust environment, a request transformation for a specific app endpoint, or to apply OAuth policies.
|
You can also use any middleware component when making service-to-service invocation calls. For example, to add token validation in a zero-trust environment, to transform a request for a specific app endpoint, or to apply OAuth policies.
|
||||||
|
|
||||||
Service-to-service invocation middleware components apply to all outgoing calls from Dapr sidecar to the receiving application (service) as shown in the diagram below.
|
Service-to-service invocation middleware components apply to all **outgoing** calls from a Dapr sidecar to the receiving application (service), as shown in the diagram below.
|
||||||
|
|
||||||
<img src="/images/app-middleware.png" width=800>
|
<img src="/images/app-middleware.png" width="800" alt="Diagram showing the flow of a service invocation request. Requests from the callee Dapr sidecar to the callee application go through the app middleware pipeline as described in the paragraph above." />
|
||||||
|
|
||||||
Any middleware component that can be applied to HTTP middleware can also be applied to service-to-service invocation calls as a middleware component using `appHttpPipeline` configuration. The example below adds the `uppercase` middleware component for all outgoing calls from the Dapr sidecar to the application that this configuration is applied to.
|
Any middleware component that can be used as HTTP middleware can also be applied to service-to-service invocation calls as a middleware component using the `appHttpPipeline` configuration. The example below adds the `uppercase` middleware component for all outgoing calls from the Dapr sidecar (target of service invocation) to the application that this configuration is applied to.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
apiVersion: dapr.io/v1alpha1
|
apiVersion: dapr.io/v1alpha1
|
||||||
|
|
Loading…
Reference in New Issue