Skip to main content

sova_core/
raw.rs

1use crate::handler::BoxFuture;
2use crate::response::ResponseBody;
3use hyper::body::Incoming;
4use hyper::{Request as HyperRequest, Response as HyperResponse};
5use std::sync::Arc;
6
7/// Escape-hatch handler: full Hyper request in, Hyper response out.
8///
9/// **Last resort.** Prefer normal routes + [`crate::Request::on_upgrade`] for
10/// WebSockets. Raw handlers skip middleware, auth, rate-limit, and session.
11pub type RawHandler =
12    Arc<dyn Fn(HyperRequest<Incoming>) -> BoxFuture<HyperResponse<ResponseBody>> + Send + Sync>;
13
14pub trait IntoRawHandler {
15    fn into_raw_handler(self) -> RawHandler;
16}
17
18impl<F, Fut> IntoRawHandler for F
19where
20    F: Fn(HyperRequest<Incoming>) -> Fut + Send + Sync + 'static,
21    Fut: std::future::Future<Output = HyperResponse<ResponseBody>> + Send + 'static,
22{
23    fn into_raw_handler(self) -> RawHandler {
24        Arc::new(move |req| Box::pin(self(req)))
25    }
26}
27
28impl IntoRawHandler for RawHandler {
29    fn into_raw_handler(self) -> RawHandler {
30        self
31    }
32}