Attribute Macro post_process
#[post_process]
Expand description
Define a post-processing middleware.
§Example
Log the status code of the HTTP response returned to the caller:
use pavex::{post_process, Response};
use pavex_tracing::{
RootSpan,
fields::{http_response_status_code, HTTP_RESPONSE_STATUS_CODE}
};
#[post_process]
pub fn response_logger(response: Response, root_span: &RootSpan) -> Response
{
root_span.record(
HTTP_RESPONSE_STATUS_CODE,
http_response_status_code(&response),
);
response
}
§Guide
Check out the “Middlewares” section of Pavex’s guide for a thorough introduction to middlewares in Pavex applications.
§Registration
Use Blueprint::post_process
to register the middleware with your Blueprint
.
The #[post_process]
macro generates a constant that you can use to refer to the
middleware when invoking Blueprint::post_process
.
§Arguments
The sections below provide an exhaustive list of all the arguments and flags supported by the post_process
macro:
Name | Kind | Required |
---|---|---|
id | Argument | No |
§id
By default, Pavex generates a constant named after your function (converted to UPPER_SNAKE_CASE) that you use when registering the middleware.
The id
argument allows you to customize the name of the generated constant.
§Example
Using the default generated identifier:
use pavex::{post_process, Blueprint, Response};
use pavex_tracing::RootSpan;
#[post_process]
pub fn response_logger(response: Response, root_span: &RootSpan) -> Response
{
// [...]
}
let mut bp = Blueprint::new();
// The generated constant is named `RESPONSE_LOGGER`
bp.post_process(RESPONSE_LOGGER);
Using a custom identifier:
use pavex::{post_process, Blueprint, Response};
use pavex_tracing::RootSpan;
#[post_process(id = "LOG_RESPONSE")]
// 👆 Custom identifier
pub fn response_logger(response: Response, root_span: &RootSpan) -> Response
{
// [...]
}
let mut bp = Blueprint::new();
// Use the custom identifier when registering
bp.post_process(LOG_RESPONSE);