hashira_tide/
lib.rs

1pub mod core;
2use hashira::{adapter::Adapter, app::AppService};
3use std::net::SocketAddr;
4
5/// An adapter for `tide`.
6#[derive(Default)]
7pub struct HashiraTide<S = ()>(Option<tide::Server<S>>);
8
9#[hashira::async_trait]
10impl<S> Adapter for HashiraTide<S>
11where
12    S: Clone + Send + Sync + 'static,
13{
14    /// Starts the server.
15    async fn serve(mut self, app: AppService) -> Result<(), hashira::error::BoxError> {
16        let host = hashira::env::get_host().unwrap_or_else(|| String::from("127.0.0.1"));
17        let port = hashira::env::get_port().unwrap_or(5000);
18        let addr: SocketAddr = format!("{host}:{port}").as_str().parse().unwrap();
19
20        println!("Server started at: http://{addr}");
21
22        match self.0.take() {
23            Some(router) => {
24                let tide = crate::core::with_router(router, app);
25                tide.listen(addr).await?;
26            }
27            None => {
28                let tide = crate::core::router(app);
29                tide.listen(addr).await?;
30            }
31        }
32
33        Ok(())
34    }
35}
36
37impl<S> HashiraTide<S> {
38    /// Constructs a default hashira adapter.
39    pub fn new() -> Self {
40        HashiraTide(None)
41    }
42}
43
44impl<S> From<tide::Server<S>> for HashiraTide<S> {
45    fn from(value: tide::Server<S>) -> Self {
46        HashiraTide(Some(value))
47    }
48}