1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
use crate::AsyncStdTransport;
use async_std::{
    net::{TcpListener, TcpStream},
    os::unix::net::{UnixListener, UnixStream},
    stream::StreamExt,
    task::{block_on, spawn},
};
use std::{env, future::Future, io::Result, pin::Pin};
use trillium::{log_error, Info};
use trillium_server_common::{
    Binding::{self, *},
    Server, Stopper,
};

/// Tcp/Unix Trillium server adapter for Async-Std
#[derive(Debug)]
pub struct AsyncStdServer(Binding<TcpListener, UnixListener>);
impl From<TcpListener> for AsyncStdServer {
    fn from(value: TcpListener) -> Self {
        Self(Tcp(value))
    }
}

impl From<UnixListener> for AsyncStdServer {
    fn from(value: UnixListener) -> Self {
        Self(Unix(value))
    }
}
impl From<std::net::TcpListener> for AsyncStdServer {
    fn from(value: std::net::TcpListener) -> Self {
        TcpListener::from(value).into()
    }
}
impl From<std::os::unix::net::UnixListener> for AsyncStdServer {
    fn from(value: std::os::unix::net::UnixListener) -> Self {
        UnixListener::from(value).into()
    }
}

#[cfg(unix)]
impl Server for AsyncStdServer {
    type Transport = Binding<AsyncStdTransport<TcpStream>, AsyncStdTransport<UnixStream>>;
    const DESCRIPTION: &'static str = concat!(
        " (",
        env!("CARGO_PKG_NAME"),
        " v",
        env!("CARGO_PKG_VERSION"),
        ")"
    );

    fn handle_signals(stop: Stopper) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
        Box::pin(async move {
            use signal_hook::consts::signal::*;
            use signal_hook_async_std::Signals;

            let signals = Signals::new([SIGINT, SIGTERM, SIGQUIT]).unwrap();
            let mut signals = signals.fuse();
            while signals.next().await.is_some() {
                if stop.is_stopped() {
                    eprintln!("\nSecond interrupt, shutting down harshly");
                    std::process::exit(1);
                } else {
                    println!("\nShutting down gracefully.\nControl-C again to force.");
                    stop.stop();
                }
            }
        })
    }

    fn accept(&mut self) -> Pin<Box<dyn Future<Output = Result<Self::Transport>> + Send + '_>> {
        Box::pin(async move {
            match &self.0 {
                Tcp(t) => t
                    .accept()
                    .await
                    .map(|(t, _)| Tcp(AsyncStdTransport::from(t))),

                Unix(u) => u
                    .accept()
                    .await
                    .map(|(u, _)| Unix(AsyncStdTransport::from(u))),
            }
        })
    }

    fn listener_from_tcp(tcp: std::net::TcpListener) -> Self {
        Self(Tcp(tcp.into()))
    }

    fn listener_from_unix(tcp: std::os::unix::net::UnixListener) -> Self {
        Self(Unix(tcp.into()))
    }

    fn info(&self) -> Info {
        match &self.0 {
            Tcp(t) => t.local_addr().unwrap().into(),
            Unix(u) => u.local_addr().unwrap().into(),
        }
    }

    fn spawn(fut: impl Future<Output = ()> + Send + 'static) {
        spawn(fut);
    }

    fn block_on(fut: impl Future<Output = ()> + 'static) {
        block_on(fut);
    }

    fn clean_up(self) -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> {
        Box::pin(async move {
            if let Unix(u) = &self.0 {
                if let Ok(local) = u.local_addr() {
                    if let Some(path) = local.as_pathname() {
                        log::info!("deleting {:?}", &path);
                        log_error!(async_std::fs::remove_file(path).await);
                    }
                }
            }
        })
    }
}