#[cfg(test)]
mod tests;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::app::App;
use crate::application::Application;
use crate::core::New;
use crate::request::Request;
use crate::response::Response;
use crate::router::matcher::{self, Segment};
use crate::router::PathParams;
use crate::server::ConnectionInfo;
type BoxFuture<T> = Pin<Box<dyn Future<Output = T> + Send + 'static>>;
type AsyncHandlerFn<S> = Arc<
dyn Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> BoxFuture<Response> + Send + Sync,
>;
#[derive(Clone)]
struct AsyncRoute<S> {
method: String,
segments: Vec<Segment>,
handler: AsyncHandlerFn<S>,
}
#[derive(Clone)]
pub struct AsyncAppWithState<S> {
state: Arc<S>,
routes: Vec<AsyncRoute<S>>,
}
impl<S: Send + Sync + 'static> AsyncAppWithState<S> {
pub fn new(state: S) -> Self {
AsyncAppWithState { state: Arc::new(state), routes: Vec::new() }
}
pub fn state(&self) -> &S {
&self.state
}
fn add<F, Fut>(mut self, method: &str, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.routes.push(AsyncRoute {
method: method.to_string(),
segments: matcher::parse_pattern(pattern),
handler: Arc::new(move |req, params, conn, state| Box::pin(handler(req, params, conn, state))),
});
self
}
pub fn get<F, Fut>(self, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.add("GET", pattern, handler)
}
pub fn post<F, Fut>(self, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.add("POST", pattern, handler)
}
pub fn put<F, Fut>(self, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.add("PUT", pattern, handler)
}
pub fn patch<F, Fut>(self, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.add("PATCH", pattern, handler)
}
pub fn delete<F, Fut>(self, pattern: &str, handler: F) -> Self
where
F: Fn(Request, PathParams, ConnectionInfo, Arc<S>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Response> + Send + 'static,
{
self.add("DELETE", pattern, handler)
}
async fn execute_async(
&self,
request: &Request,
connection: &ConnectionInfo,
) -> Result<Response, String> {
let path = request.request_uri.split('?').next().unwrap_or(&request.request_uri);
let path_segs: Vec<&str> = path.split('/').filter(|s| !s.is_empty()).collect();
for route in &self.routes {
if route.method != request.method {
continue;
}
if let Some(params_map) = matcher::try_match(&route.segments, &path_segs) {
let params = PathParams::from_map(params_map);
let fut = (route.handler)(
request.clone(),
params,
connection.clone(),
Arc::clone(&self.state),
);
return Ok(fut.await);
}
}
App::new().execute(request, connection)
}
}
impl<S: Send + Sync + 'static> Application for AsyncAppWithState<S> {
fn execute(&self, request: &Request, connection: &ConnectionInfo) -> Result<Response, String> {
let request = request.clone();
let connection = connection.clone();
match tokio::runtime::Handle::try_current() {
Ok(_) => {
std::thread::scope(|s| {
s.spawn(|| {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(self.execute_async(&request, &connection))
})
.join()
.unwrap()
})
}
Err(_) => {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap()
.block_on(self.execute_async(&request, &connection))
}
}
}
}