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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use crate::{async_trait, Context, Result};
use std::future::Future;

/// ### Middleware
///
/// There are two kinds of middlewares,
/// the one is functional middlewares, the another is trait middlewares.
///
/// #### Functional Middlewares
///
/// A normal functional middleware is an object implements `Fn` trait:
///
/// ```rust
/// use roa_core::{Context, Next, Result, Middleware};
/// use std::future::Future;
///
/// fn is_middleware<S>(middleware: impl for<'a> Middleware<'a, S>) {
/// }
///
/// async fn middleware(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     Ok(())
/// }
///
/// is_middleware(middleware);
/// ```
///
/// Closures are also supported, but feature(async_closure) is required:
///
/// #### Trait Middlewares
///
/// A trait middleware is an object implementing trait `Middleware`.
///
/// ```rust
/// use roa_core::{Middleware, Context, Next, Result, async_trait};
/// use async_std::sync::Arc;
/// use std::time::Instant;
///
/// fn is_middleware<S>(middleware: impl for<'a> Middleware<'a, S>) {}
///
/// struct Logger;
///
/// #[async_trait(?Send)]
/// impl <'a> Middleware<'a, ()> for Logger {
///     async fn handle(&'a self, ctx: &'a mut Context<()>, next: Next<'a>) -> Result {
///         let start = Instant::now();
///         let result = next.await;
///         println!("time elapsed: {}ms", start.elapsed().as_millis());
///         result
///     }
/// }
///
/// is_middleware(Logger);
/// ```
#[cfg_attr(feature = "docs", doc(spotlight))]
#[async_trait(?Send)]
pub trait Middleware<'a, S>: 'static + Sync + Send {
    /// Handle context and next, then return a future to get status.
    async fn handle(&'a self, ctx: &'a mut Context<S>, next: Next<'a>) -> Result;
}

#[async_trait(?Send)]
impl<'a, S, T, F> Middleware<'a, S> for T
where
    S: 'a,
    T: 'static + Send + Sync + Fn(&'a mut Context<S>, Next<'a>) -> F,
    F: 'a + Future<Output = Result>,
{
    #[inline]
    async fn handle(&'a self, ctx: &'a mut Context<S>, next: Next<'a>) -> Result {
        (self)(ctx, next).await
    }
}

/// ### Endpoint
///
/// There are two kinds of endpoints,
/// the one is functional endpoints, the another is trait endpoints.
///
/// #### Functional Endpoints
///
/// A normal functional endpoint is an object implements `Fn` trait:
///
/// ```rust
/// use roa_core::{Context, Next, Result, Endpoint};
/// use std::future::Future;
///
/// fn is_endpoint<S>(endpoint: impl for<'a> Endpoint<'a, S>) {
/// }
///
/// async fn endpoint(ctx: &mut Context<()>) -> Result {
///     Ok(())
/// }
///
/// is_endpoint(endpoint);
/// ```
///
/// Closures are also supported, but feature(async_closure) is required:
///
/// #### Trait Endpoints
///
/// A trait endpoint is an object implementing trait `Endpoint`.
///
/// ```rust
/// use roa_core::{Endpoint, Context, Next, Result, async_trait};
/// use async_std::sync::Arc;
/// use std::time::Instant;
///
/// fn is_endpoint<S>(endpoint: impl for<'a> Endpoint<'a, S>) {
/// }
///
/// struct Logger;
///
/// #[async_trait(?Send)]
/// impl <'a> Endpoint<'a, ()> for Logger {
///     async fn call(&'a self, ctx: &'a mut Context<()>) -> Result {
///         Ok(())
///     }
/// }
///
/// is_endpoint(Logger);
/// ```
#[cfg_attr(feature = "docs", doc(spotlight))]
#[async_trait(?Send)]
pub trait Endpoint<'a, S>: 'static + Sync + Send {
    /// Call this endpoint.
    async fn call(&'a self, ctx: &'a mut Context<S>) -> Result;
}

#[async_trait(?Send)]
impl<'a, S, T, F> Endpoint<'a, S> for T
where
    S: 'a,
    T: 'static + Send + Sync + Fn(&'a mut Context<S>) -> F,
    F: 'a + Future<Output = Result>,
{
    #[inline]
    async fn call(&'a self, ctx: &'a mut Context<S>) -> Result {
        (self)(ctx).await
    }
}

/// Fake middleware.
#[async_trait(?Send)]
impl<'a, S> Middleware<'a, S> for () {
    #[allow(clippy::trivially_copy_pass_by_ref)]
    #[inline]
    async fn handle(&'a self, _ctx: &'a mut Context<S>, next: Next<'a>) -> Result<()> {
        next.await
    }
}

/// Fake endpoint.
#[async_trait(?Send)]
impl<'a, S> Endpoint<'a, S> for () {
    #[allow(clippy::trivially_copy_pass_by_ref)]
    #[inline]
    async fn call(&'a self, _ctx: &'a mut Context<S>) -> Result<()> {
        Ok(())
    }
}

/// Type of the second parameter in a middleware.
///
/// `Next` is usually a closure capturing the next middleware, context and the next `Next`.
///
/// Developer of middleware can jump to next middleware by calling `next.await`.
///
/// ### Example
///
/// ```rust
/// use roa_core::{App, Context, Result, Status, MiddlewareExt, Next};
/// use roa_core::http::StatusCode;
///
/// let app = App::new(())
///     .gate(first)
///     .gate(second)
///     .gate(third)
///     .end(end);
/// async fn first(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     assert!(ctx.store("id", "1").is_none());
///     next.await?;
///     assert_eq!("5", *ctx.load::<&'static str>("id").unwrap());
///     Ok(())
/// }
/// async fn second(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     assert_eq!("1", *ctx.load::<&'static str>("id").unwrap());
///     assert_eq!("1", *ctx.store("id", "2").unwrap());
///     next.await?;
///     assert_eq!("4", *ctx.store("id", "5").unwrap());
///     Ok(())
/// }
/// async fn third(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     assert_eq!("2", *ctx.store("id", "3").unwrap());
///     next.await?; // next is none; do nothing
///     assert_eq!("3", *ctx.store("id", "4").unwrap());
///     Ok(())
/// }
///
/// async fn end(ctx: &mut Context<()>) -> Result {
///     assert_eq!("3", *ctx.load::<&'static str>("id").unwrap());
///     Ok(())
/// }
/// ```
///
/// ### Error Handling
///
/// You can catch or straightly throw a Error returned by next.
///
/// ```rust
/// use roa_core::{App, Context, Result, Status, MiddlewareExt, Next, throw};
/// use roa_core::http::StatusCode;
///         
/// let app = App::new(())
///     .gate(catch)
///     .gate(gate)
///     .end(end);
///
/// async fn catch(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     // catch
///     if let Err(err) = next.await {
///         // teapot is ok
///         if err.status_code != StatusCode::IM_A_TEAPOT {
///             return Err(err);
///         }
///     }
///     Ok(())
/// }
/// async fn gate(ctx: &mut Context<()>, next: Next<'_>) -> Result {
///     next.await?; // just throw
///     unreachable!()
/// }
///
/// async fn end(ctx: &mut Context<()>) -> Result {
///     throw!(StatusCode::IM_A_TEAPOT, "I'm a teapot!")
/// }
/// ```
///
pub type Next<'a> = &'a mut (dyn Unpin + Future<Output = Result<()>>);