nutt_web/modules/router/
route.rs

1use crate::http::method::Method;
2use crate::http::request::Request;
3use crate::http::response::responder::Responder;
4use crate::http::response::Response;
5use std::future::Future;
6use std::pin::Pin;
7use std::sync::Arc;
8use tokio::net::TcpStream;
9
10type FuncPointer = fn(Request) -> Pin<Box<dyn Future<Output = Response> + Send + Sync>>;
11pub struct Route {
12    method: Method,
13    path: String,
14    fabric: Arc<FuncPointer>,
15}
16
17impl Route {
18    pub fn run_fabric(&self, stream: TcpStream, req: Request) {
19        let fabric = self.fabric.clone();
20        tokio::spawn(async move {
21            let resp = fabric(req).await;
22            stream
23                .try_write(resp.into_response().to_string().as_bytes())
24                .unwrap()
25        });
26    }
27}
28
29impl Route {
30    pub fn new(method: Method, path: &str, fabric: FuncPointer) -> Self {
31        Self {
32            method,
33            path: path.to_string(),
34            fabric: Arc::new(fabric),
35        }
36    }
37
38    #[inline]
39    pub fn get(&self) -> (Method, String) {
40        (self.method.clone(), self.path.clone())
41    }
42}