Changed the add_route API to allow the use of simpler, async handlers

This commit is contained in:
Emi Tatsuo 2020-11-19 23:39:07 -05:00
parent 54816e1f67
commit 29c831649d
Signed by: Emi
GPG Key ID: 68FAB2E2E6DFC98B
6 changed files with 73 additions and 94 deletions

View File

@ -28,5 +28,4 @@ mime_guess = { version = "2.0.3", optional = true }
[dev-dependencies]
env_logger = "0.8.1"
futures-util = "0.3.7"
tokio = { version = "0.3.1", features = ["macros", "rt-multi-thread", "sync"] }

View File

@ -1,6 +1,4 @@
use anyhow::*;
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use log::LevelFilter;
use tokio::sync::RwLock;
use northstar::{Certificate, GEMINI_MIME, GEMINI_PORT, Request, Response, Server};
@ -31,8 +29,7 @@ async fn main() -> Result<()> {
/// selecting a username. They'll then get a message confirming their account creation.
/// Any time this user visits the site in the future, they'll get a personalized welcome
/// message.
fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Request) -> BoxFuture<'static, Result<Response>> {
async move {
async fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Request) -> Result<Response> {
if let Some(Certificate(cert_bytes)) = request.certificate() {
// The user provided a certificate
let users_read = users.read().await;
@ -70,5 +67,4 @@ fn handle_request(users: Arc<RwLock<HashMap<CertBytes, String>>>, request: Reque
// The user didn't provide a certificate
Ok(Response::client_certificate_required())
}
}.boxed()
}

View File

@ -1,6 +1,4 @@
use anyhow::*;
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use log::LevelFilter;
use northstar::{Server, Request, Response, GEMINI_PORT, Document};
use northstar::document::HeadingLevel::*;
@ -17,8 +15,7 @@ async fn main() -> Result<()> {
.await
}
fn handle_request(_request: Request) -> BoxFuture<'static, Result<Response>> {
async move {
async fn handle_request(_request: Request) -> Result<Response> {
let mut document = Document::new();
document
@ -48,5 +45,3 @@ fn handle_request(_request: Request) -> BoxFuture<'static, Result<Response>> {
Ok(Response::document(document))
}
.boxed()
}

View File

@ -1,6 +1,4 @@
use anyhow::*;
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use log::LevelFilter;
use northstar::{Document, document::HeadingLevel, Request, Response, GEMINI_PORT};
@ -18,25 +16,19 @@ async fn main() -> Result<()> {
.await
}
fn handle_base(_: Request) -> BoxFuture<'static, Result<Response>> {
async fn handle_base(_: Request) -> Result<Response> {
let doc = generate_doc("base");
async move {
Ok(Response::document(doc))
}.boxed()
}
fn handle_short(_: Request) -> BoxFuture<'static, Result<Response>> {
async fn handle_short(_: Request) -> Result<Response> {
let doc = generate_doc("short");
async move {
Ok(Response::document(doc))
}.boxed()
}
fn handle_long(_: Request) -> BoxFuture<'static, Result<Response>> {
async fn handle_long(_: Request) -> Result<Response> {
let doc = generate_doc("long");
async move {
Ok(Response::document(doc))
}.boxed()
}
fn generate_doc(route_name: &str) -> Document {

View File

@ -1,6 +1,4 @@
use anyhow::*;
use futures_core::future::BoxFuture;
use futures_util::FutureExt;
use log::LevelFilter;
use northstar::{Server, Request, Response, GEMINI_PORT};
@ -16,12 +14,9 @@ async fn main() -> Result<()> {
.await
}
fn handle_request(request: Request) -> BoxFuture<'static, Result<Response>> {
async move {
async fn handle_request(request: Request) -> Result<Response> {
let path = request.path_segments();
let response = northstar::util::serve_dir("public", &path).await?;
Ok(response)
}
.boxed()
}

View File

@ -8,7 +8,7 @@ use std::{
path::PathBuf,
time::Duration,
};
use futures_core::future::BoxFuture;
use futures_core::future::{BoxFuture, Future};
use tokio::{
prelude::*,
io::{self, BufStream},
@ -290,11 +290,13 @@ impl<A: ToSocketAddrs> Builder<A> {
/// "endpoint". Entering a relative or malformed path will result in a panic.
///
/// For more information about routing mechanics, see the docs for [`RoutingNode`].
pub fn add_route<H>(mut self, path: &'static str, handler: H) -> Self
pub fn add_route<F, H>(mut self, path: &'static str, handler: H) -> Self
where
H: Fn(Request) -> HandlerResponse + Send + Sync + 'static,
H: Send + Sync + 'static + Fn(Request) -> F,
F: Send + Sync + 'static + Future<Output = Result<Response>>
{
self.routes.add_route(path, Arc::new(handler));
let wrapped = Arc::new(move|req| Box::pin((handler)(req)) as HandlerResponse);
self.routes.add_route(path, wrapped);
self
}