based/src/request/context.rs

30 lines
760 B
Rust
Raw Normal View History

2024-12-18 14:33:53 +01:00
use rocket::{
Request,
request::{self, FromRequest},
};
2024-12-17 23:28:43 +01:00
2024-12-18 14:33:53 +01:00
/// Represents contextual information about an HTTP request.
2024-12-17 23:28:43 +01:00
pub struct RequestContext {
2024-12-18 14:33:53 +01:00
/// A flag indicating if the request is an HTMX request.
///
/// This is determined by checking the presence of the `HX-Request` header.
pub is_htmx: bool,
2024-12-17 23:28:43 +01:00
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for RequestContext {
type Error = ();
async fn from_request(req: &'r Request<'_>) -> request::Outcome<Self, Self::Error> {
rocket::outcome::Outcome::Success(RequestContext {
2024-12-29 20:39:10 +01:00
is_htmx: req.headers().get("HX-Request").next().is_some(),
2024-12-17 23:28:43 +01:00
})
}
2024-12-18 14:33:53 +01:00
}
2024-12-29 20:03:04 +01:00
impl Default for RequestContext {
fn default() -> Self {
Self { is_htmx: false }
}
}