rambl_rs 0.1.10

An HTTP server framework
Documentation
#![allow(dead_code)]

use crate::Response;
use tokio::{io::{self, AsyncRead, AsyncWriteExt}, net::TcpStream, sync::Mutex};

/// Responder is the only way to respond to a request.
/// You respond by using the respond method.
pub struct Responder {
    stream: Mutex<TcpStream>,
}

impl Responder {
    /// Creates a new instanc eof Responder
    pub fn new(stream: TcpStream) -> Self {
        Responder { stream: Mutex::new(stream) }
    }

    /// Sends a response down the stream.
    /// Many types implement Response
    pub async fn respond(&self, res: impl Response) -> io::Result<()> {
        let mut stream = self.stream.lock().await;
        
        stream.write_all(&res.as_response()[..]).await?;
        stream.flush().await?;

        Ok(())
    }
}