Writing a Middleware
RoadRunner provides a flexible and extensible architecture that allows developers to build custom middleware for http
and custom interceptors for grpc
and temporal
plugins. Moving highly loaded parts of an application, such as authentication, to middleware written in Go can provide a significant performance boost. By leveraging the speed and efficiency of Go, developers can improve the overall performance of their application and handle spikes in traffic more effectively.
Middleware architecture allows developers to create custom middleware for their specific needs. The HTTP middleware can be used to intercept and modify HTTP requests and responses, while the gRPC interceptor can be used to intercept and modify gRPC requests and responses. This allows developers to add additional functionality to their applications without having to modify the core application logic.
HTTP
The HTTP middleware intercepts incoming HTTP requests and can be used to perform additional processing, such as authentication, rate limiting, and logging.
To create custom middleware for HTTP requests in RoadRunner, follow these steps:
Define a struct that implements the
Init()
,Middleware()
, andName()
methods. TheInit()
method is called when the plugin is initialized, theMiddleware()
method is called for each incoming HTTP request, and theName()
method returns the name of the middleware/plugin.In the
Middleware()
method, perform any necessary processing on the incoming HTTP request, and then call the next middleware in the pipeline using thenext.ServeHTTP()
method.
Here is an example:
gRPC
The interceptor intercepts incoming gRPC requests and can be used to perform additional processing, such as authentication, rate limiting, and logging.
To create a custom interceptor for gRPC requests in RoadRunner, follow these steps:
Define a struct that implements the
Init()
,Interceptor()
, andName()
methods. TheInit()
method is called when the plugin is initialized, theInterceptor()
method is called for each incoming gRPC request, and theName()
method returns the name of the middleware/plugin.In the
Interceptor()
method, perform any necessary processing on the incoming gRPC request, and then call the next interceptor in the pipeline using thehandler(ctx, req)
method.
RoadRunner supports gRPC
interceptors since v2023.2.0
version.
Here is an example:
PSR7 Attributes
PSR7 attributes are a way of attaching metadata to an incoming HTTP request or response. The PSR7 specification defines a standard interface for HTTP messages, which includes the ability to set and retrieve attributes on both requests and responses.
Attributes can be used to store any kind of metadata that might be useful for processing the request or response. For example, you might use attributes to store information about the authenticated user, the user's IP address, or any other custom data that you want to attach to the request.
The Psr\Http\Message\ServerRequestInterface->getAttributes()
method can be used to retrieve attributes from an incoming HTTP request, while the ResponseInterface->withAttribute()
method can be used to set attributes on an outgoing HTTP response.
Registering middleware
Video tutorial
Writing a middleware for HTTP
Last updated
Was this helpful?