hashira_warp/
lib.rs

1pub mod core;
2
3use hashira::adapter::Adapter;
4use std::net::SocketAddr;
5
6// A placeholder for an empty filter.
7type Empty = warp::filters::BoxedFilter<(Box<dyn warp::Reply>,)>;
8
9/// An adapter for `warp`.
10pub struct HashiraWarp<F>(Option<F>);
11
12impl HashiraWarp<Empty> {
13    /// Constructs an adapter without any configuration.
14    pub fn new() -> HashiraWarp<Empty> {
15        HashiraWarp(None)
16    }
17}
18
19impl Default for HashiraWarp<Empty> {
20    fn default() -> Self {
21        Self::new()
22    }
23}
24
25#[hashira::async_trait]
26impl<F> Adapter for HashiraWarp<F>
27where
28    F: warp::Filter<Error = warp::Rejection> + Send + Sync + Clone + 'static,
29    F::Extract: warp::reply::Reply,
30{
31    async fn serve(mut self, app: hashira::app::AppService) -> Result<(), hashira::error::BoxError> {
32        let host = hashira::env::get_host().unwrap_or_else(|| String::from("127.0.0.1"));
33        let port = hashira::env::get_port().unwrap_or(5000);
34        let addr: SocketAddr = format!("{host}:{port}").as_str().parse().unwrap();
35
36        println!("Server started at: http://{addr}");
37
38        let filter = crate::core::router(app);
39
40        match self.0.take() {
41            Some(this) => {
42                let routes = this.or(filter);
43                warp::serve(routes).run(addr).await;
44            }
45            None => {
46                warp::serve(filter).run(addr).await;
47            }
48        }
49
50        Ok(())
51    }
52}
53
54impl<F> From<F> for HashiraWarp<F>
55where
56    F: warp::Filter<Error = warp::Rejection> + Send + Sync + Clone + 'static,
57    F::Extract: warp::reply::Reply,
58{
59    fn from(value: F) -> Self {
60        HashiraWarp(Some(value))
61    }
62}