//! Server startup and pre-request logging.

use log::*;
use trillium::Handler;

struct StartupLog {}

pub fn startup_log() -> impl Handler {
    StartupLog {}
}

#[trillium::async_trait]
impl Handler for StartupLog {
    async fn run(&self, conn: trillium::Conn) -> trillium::Conn {
        debug!(
            "receiving request {} {} (client: {:?})",
            conn.method(),
            conn.path(),
            conn.peer_ip()
        );
        conn
    }

    async fn init(&mut self, info: &mut trillium::Info) -> () {
        info!("started {}", info.server_description());
        if let Some(addr) = info.tcp_socket_addr() {
            info!("listening on {}", addr);
        } else {
            info!("listening on {}", info.listener_description());
        }
    }
}
