nova_web/
routing.rs

1use nova_core::response::ServerResponse;
2use nova_core::types::request_type::RequestType;
3use nova_router::callable::CloneableFn;
4use nova_router::routes::Routes;
5
6pub use {
7    nova_router::route::{delete, get, patch, post, put, service},
8    nova_router::server_routing::ServerRouting,
9};
10
11use crate::server::Server;
12
13impl ServerRouting for Server {
14    fn route<F: CloneableFn<Output = ServerResponse> + 'static>(
15        &mut self,
16        r#type: RequestType,
17        path: &str,
18        f: F,
19    ) -> Self {
20        self.router.register(r#type, path, f);
21        self.clone()
22    }
23
24    fn service(&mut self, path: &str, routes: Routes) -> Self
25    where
26        Self: Sized,
27    {
28        routes.for_each(|item| {
29            let path = &format!("{path}{}", item.get_path());
30            if item.get_callable().is_some() {
31                let _ = self.route(item.get_type(), path, item.get_callable().unwrap());
32            } else {
33                let _ = self.service(path, item.get_routes());
34            }
35        });
36        self.clone()
37    }
38
39    fn fallback<F: CloneableFn<Output = ServerResponse> + 'static>(&mut self, f: F) -> Self
40    where
41        Self: Sized,
42    {
43        self.router.register_fallback(f);
44        self.clone()
45    }
46}