titan/
endpoint.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
use std::{collections::HashMap, future::Future, pin::Pin, task::Poll};

use crate::prelude::*;
use pin_project_lite::pin_project;
use titan_core::{FromRequest, Handler, Respondable, Service};
use titan_http::{body::Body, Method, Request, Response, StatusCode};
use titan_utils::BoxedSendFuture;

/// Represents a web path with a specific HTTP method.
///
/// Guards can be attached to a Route, which are functions that must return true for the route to be matched.
/// Guards are checked in the order they are added.
#[derive(Default, Clone)]
pub struct Endpoint {
  pub(crate) methods:
    HashMap<Method, BoxCloneService<Request, Response, Response>>,
}

impl Endpoint {
  pub fn at(
    &self,
    method: &Method,
  ) -> Option<&BoxCloneService<Request, Response, Response>> {
    self.methods.get(method)
  }
  pub fn at_mut(
    &mut self,
    method: &Method,
  ) -> Option<&mut BoxCloneService<Request, Response, Response>> {
    self.methods.get_mut(method)
  }
}

impl Service<Request> for Endpoint {
  type Response = Response;
  type Error = Response;
  type Future = BoxedSendFuture<Result<Self::Response, Self::Error>>;

  fn poll_ready(
    &mut self,
    _cx: &mut std::task::Context<'_>,
  ) -> Poll<Result<(), Self::Error>> {
    Poll::Ready(Ok(()))
  }
  fn call(&mut self, req: Request) -> Self::Future {
    let (parts, body) = req.into_parts();

    let Some(route) = self.at_mut(&parts.method) else {
      let mut response = Response::new(Body::from(()));

      *response.status_mut() = StatusCode::METHOD_NOT_ALLOWED;
      return Box::pin(EndpointFuture { fut: async move { Err(response) } });
    };
    let req = Request::from_parts(parts, body);
    Box::pin(EndpointFuture { fut: route.call(req) })
  }
}

pin_project! {
    struct EndpointFuture<Fut>
    where
      Fut: Future,
    {
      #[pin]
      fut: Fut,
    }
}

impl<Fut> Future for EndpointFuture<Fut>
where
  Fut: Future,
{
  type Output = Fut::Output;
  fn poll(
    self: Pin<&mut Self>,
    cx: &mut std::task::Context<'_>,
  ) -> Poll<Self::Output> {
    let this = self.project();
    this.fut.poll(cx)
  }
}

macro_rules! impl_methodrouter {
  ($( $method_name:ident $method:ident ),*) => {
        impl Endpoint {
            $(
                pub fn $method_name<H, Args>(mut self, route: H) -> Self
                where
                  H: Handler<Args> + Sync + Clone,
                  H::Future: Future<Output = H::Output> + Send,
                  H::Output: Respondable,
                  Args: FromRequest + Send + Sync + 'static,
                  Args::Error: Send
                {
                    let route = $crate::route::Route::new(route);
                    self.methods.insert(titan_http::Method::$method, BoxCloneService::new(route));
                    self
                }
            )*
        }
    };
}

impl_methodrouter!(get GET, post POST, put PUT, delete DELETE, patch PATCH);