Fix examples (& also bugs with args in lib.rs)

I thought I was clever with Into<Handler> :(
This commit is contained in:
Emi Tatsuo 2020-11-19 22:33:44 -05:00
parent 4a0d07c2ca
commit dc18bf2d1c
Signed by: Emi
GPG key ID: 68FAB2E2E6DFC98B
5 changed files with 15 additions and 9 deletions

View file

@ -19,7 +19,8 @@ async fn main() -> Result<()> {
let users = Arc::<RwLock::<HashMap<CertBytes, String>>>::default(); let users = Arc::<RwLock::<HashMap<CertBytes, String>>>::default();
Server::bind(("0.0.0.0", GEMINI_PORT)) Server::bind(("0.0.0.0", GEMINI_PORT))
.serve(move|req| handle_request(users.clone(), req)) .add_route("/", move|req| handle_request(users.clone(), req))
.serve()
.await .await
} }

View file

@ -12,7 +12,8 @@ async fn main() -> Result<()> {
.init(); .init();
Server::bind(("localhost", GEMINI_PORT)) Server::bind(("localhost", GEMINI_PORT))
.serve(handle_request) .add_route("/",handle_request)
.serve()
.await .await
} }

View file

@ -11,7 +11,8 @@ async fn main() -> Result<()> {
.init(); .init();
Server::bind(("localhost", GEMINI_PORT)) Server::bind(("localhost", GEMINI_PORT))
.serve(handle_request) .add_route("/", handle_request)
.serve()
.await .await
} }

View file

@ -290,12 +290,15 @@ impl<A: ToSocketAddrs> Builder<A> {
/// "endpoint". Entering a relative or malformed path will result in a panic. /// "endpoint". Entering a relative or malformed path will result in a panic.
/// ///
/// For more information about routing mechanics, see the docs for [`RoutingNode`]. /// For more information about routing mechanics, see the docs for [`RoutingNode`].
pub fn add_route(mut self, path: &'static str, handler: impl Into<Handler>) -> Self { pub fn add_route<H>(mut self, path: &'static str, handler: H) -> Self
self.routes.add_route(path, handler); where
H: Fn(Request) -> HandlerResponse + Send + Sync + 'static,
{
self.routes.add_route(path, Arc::new(handler));
self self
} }
pub async fn serve<F>(mut self) -> Result<()> { pub async fn serve(mut self) -> Result<()> {
let config = tls_config(&self.cert_path, &self.key_path) let config = tls_config(&self.cert_path, &self.key_path)
.context("Failed to create TLS config")?; .context("Failed to create TLS config")?;

View file

@ -76,7 +76,7 @@ impl RoutingNode {
/// static strings. If you would like to add a string dynamically, please use /// static strings. If you would like to add a string dynamically, please use
/// [`RoutingNode::add_route_by_path()`] in order to appropriately deal with any /// [`RoutingNode::add_route_by_path()`] in order to appropriately deal with any
/// errors that might arise. /// errors that might arise.
pub fn add_route(&mut self, path: &'static str, handler: impl Into<Handler>) { pub fn add_route(&mut self, path: &'static str, handler: Handler) {
let path: Path = path.try_into().expect("Malformed path route received"); let path: Path = path.try_into().expect("Malformed path route received");
self.add_route_by_path(path, handler).unwrap(); self.add_route_by_path(path, handler).unwrap();
} }
@ -87,7 +87,7 @@ impl RoutingNode {
/// this method. /// this method.
/// ///
/// For information about how routes work, see [`RoutingNode::match_path()`] /// For information about how routes work, see [`RoutingNode::match_path()`]
pub fn add_route_by_path(&mut self, mut path: Path, handler: impl Into<Handler>) -> Result<(), ConflictingRouteError>{ pub fn add_route_by_path(&mut self, mut path: Path, handler: Handler) -> Result<(), ConflictingRouteError>{
debug_assert!(path.is_absolute()); debug_assert!(path.is_absolute());
path.normalize(false); path.normalize(false);
@ -99,7 +99,7 @@ impl RoutingNode {
if node.0.is_some() { if node.0.is_some() {
Err(ConflictingRouteError()) Err(ConflictingRouteError())
} else { } else {
node.0 = Some(handler.into()); node.0 = Some(handler);
Ok(()) Ok(())
} }
} }