Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options Method #104

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::sync::Arc;

use crate::{
body::Body,
endpoint::{BoxedEndpoint, Endpoint},
Middleware,
};
Expand Down Expand Up @@ -180,7 +181,7 @@ struct ResourceData<Data> {
middleware: Vec<Arc<dyn Middleware<Data> + Send + Sync>>,
}

impl<'a, Data> Resource<'a, Data> {
impl<'a, Data: Send + Sync + Clone + 'static> Resource<'a, Data> {
/// "Nest" a subrouter to the path.
///
/// This method will build a fresh `Router` and give a mutable reference to it to the builder
Expand Down Expand Up @@ -208,11 +209,22 @@ impl<'a, Data> Resource<'a, Data> {
*resource = Some(new_resource);
}
let resource = resource.as_mut().unwrap();

if resource.endpoints.contains_key(&method) {
panic!("A {} endpoint already exists for this path", method)
}

if !resource.endpoints.contains_key(&http::Method::OPTIONS) {
let callback = async move || {
http::Response::builder()
.status(http::status::StatusCode::OK)
.header("Content-Type", "text/plain")
.body(Body::empty())
.unwrap()
};
resource
.endpoints
.insert(http::Method::OPTIONS, BoxedEndpoint::new(callback));
}
resource.endpoints.insert(method, BoxedEndpoint::new(ep));
}

Expand Down