rust_express/
init.rs

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
use std::sync::Arc;
use tokio::io::AsyncWriteExt;
use crate::utils::{request::parse_request_data, response::handle_response, router::Router};

pub struct App {
    endpoints: Option<Arc<dyn Fn(&mut Router) + Send + Sync>>,
}

impl App {
    pub fn new() -> Self {
        Self {
            endpoints: None,
        }
    }

    pub fn endpoints<F>(&mut self, endpoints: F)
    where
        F: Fn(&mut Router) + Send + Sync + 'static,
    {
        self.endpoints = Some(Arc::new(endpoints));
    }

    pub async fn run(self, port: i128) {
        let listener = tokio::net::TcpListener::bind(&format!("0.0.0.0:{port}"))
            .await
            .expect("Error while binding connection to public address");
        println!("Server is running at: http://localhost:{}", port);

        let app_ref = Arc::new(self);
        loop {
            let (mut stream, _) = listener
                .accept()
                .await
                .expect("Failed to accept the client's established connection");
            let app = Arc::clone(&app_ref);
            tokio::spawn(async move {
                app.handle_stream(&mut stream).await;
            });
        }
    }

    async fn handle_stream(&self, stream: &mut tokio::net::TcpStream) {
        let request = parse_request_data(stream).await;
        let mut router = Router::new();

        // Configure routes via callback
        if let Some(ref endpoints) = self.endpoints {
            endpoints(&mut router);
        }

        handle_response(stream, &request, &router).await;
        stream.flush().await.expect("Failed to flush stream");
    }
}