Merge branch 'allow-async-handlers' into user-management
This commit is contained in:
commit
0b4fca2c69
|
@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- build time and size by [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
### Changed
|
||||
- Added route API [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
- API for adding handlers now accepts async handlers [@Alch-Emi](https://github.com/Alch-Emi)
|
||||
|
||||
## [0.3.0] - 2020-11-14
|
||||
### Added
|
||||
|
|
|
@ -21,7 +21,6 @@ tokio = { version = "0.3.1", features = ["io-util","net","time", "rt"] }
|
|||
mime = "0.3.16"
|
||||
uriparse = "0.6.3"
|
||||
percent-encoding = "2.1.0"
|
||||
futures-core = "0.3.7"
|
||||
log = "0.4.11"
|
||||
webpki = "0.21.0"
|
||||
lazy_static = "1.4.0"
|
||||
|
@ -39,5 +38,4 @@ required-features = ["user_management"]
|
|||
|
||||
[dev-dependencies]
|
||||
env_logger = "0.8.1"
|
||||
futures-util = "0.3.7"
|
||||
tokio = { version = "0.3.1", features = ["macros", "rt-multi-thread", "sync"] }
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
|
@ -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(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
async fn handle_base(req: Request) -> Result<Response> {
|
||||
let doc = generate_doc("base", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn handle_short(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
async fn handle_short(req: Request) -> Result<Response> {
|
||||
let doc = generate_doc("short", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn handle_long(req: Request) -> BoxFuture<'static, Result<Response>> {
|
||||
async fn handle_long(req: Request) -> Result<Response> {
|
||||
let doc = generate_doc("long", &req);
|
||||
async move {
|
||||
Ok(Response::document(doc))
|
||||
}.boxed()
|
||||
}
|
||||
|
||||
fn generate_doc(route_name: &str, req: &Request) -> Document {
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -7,8 +7,9 @@ use std::{
|
|||
sync::Arc,
|
||||
path::PathBuf,
|
||||
time::Duration,
|
||||
pin::Pin,
|
||||
};
|
||||
use futures_core::future::BoxFuture;
|
||||
use std::future::Future;
|
||||
use tokio::{
|
||||
prelude::*,
|
||||
io::{self, BufStream},
|
||||
|
@ -41,7 +42,7 @@ pub const REQUEST_URI_MAX_LEN: usize = 1024;
|
|||
pub const GEMINI_PORT: u16 = 1965;
|
||||
|
||||
type Handler = Arc<dyn Fn(Request) -> HandlerResponse + Send + Sync>;
|
||||
pub (crate) type HandlerResponse = BoxFuture<'static, Result<Response>>;
|
||||
pub (crate) type HandlerResponse = Pin<Box<dyn Future<Output = Result<Response>> + Send>>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Server {
|
||||
|
@ -351,11 +352,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
|
||||
}
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ use crate::types::{Document, document::HeadingLevel::*};
|
|||
use crate::types::Response;
|
||||
use std::panic::{catch_unwind, AssertUnwindSafe};
|
||||
use std::task::Poll;
|
||||
use futures_core::future::Future;
|
||||
use std::future::Future;
|
||||
use tokio::time;
|
||||
|
||||
#[cfg(feature="serve_dir")]
|
||||
|
|
Loading…
Reference in a new issue