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

Rama web router #423

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions rama-http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ tokio = { workspace = true, features = ["macros", "fs", "io-std"] }
tokio-util = { workspace = true, features = ["io"] }
tracing = { workspace = true }
uuid = { workspace = true, features = ["v4"] }
matchit = "0.8.6"

[dev-dependencies]
brotli = { workspace = true }
Expand Down
4 changes: 4 additions & 0 deletions rama-http/src/service/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ pub use endpoint::{extract, EndpointServiceFn, IntoEndpointService};
pub mod k8s;
#[doc(inline)]
pub use k8s::{k8s_health, k8s_health_builder};

mod router;
#[doc(inline)]
pub use router::{Router};
173 changes: 173 additions & 0 deletions rama-http/src/service/web/router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
use super::{IntoEndpointService};
use crate::{Request, Response, StatusCode};
use rama_core::{service::{BoxService, Service, service_fn}, Context};
use std::{convert::Infallible, sync::Arc};
use std::collections::HashMap;
use http::{Method};

use matchit::Router as MatchitRouter;
use rama_http_types::IntoResponse;

/// A basic web router that can be used to serve HTTP requests based on path matching.
/// It will also provide extraction of path parameters and wildcards out of the box so
/// you can define your paths accordingly.

pub struct Router<State> {
routes: MatchitRouter<HashMap<Method, Arc<BoxService<State, Request, Response, Infallible>>>>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to be able to nest and merge btw, so I think this approach is a bit too simplistic

not_found: Arc<BoxService<State, Request, Response, Infallible>>,
}

impl<State> std::fmt::Debug for Router<State> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Router").finish()
}
}

impl<State> Clone for Router<State> {
fn clone(&self) -> Self {
Self {
routes: self.routes.clone(),
not_found: self.not_found.clone(),
}
}
}

/// default trait
impl<State> Default for Router<State>
where
State: Clone + Send + Sync + 'static,
{
fn default() -> Self {
Self::new()
}
}

impl<State> Router<State>
where
State: Clone + Send + Sync + 'static,
{
/// create a new web router
pub(crate) fn new() -> Self {
Self {
routes: MatchitRouter::new(),
not_found: Arc::new(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can probably be made optional so you don't need to assign it if not set, and instead just fallback to NOT_FOUND anyway but on the spot

service_fn(|| async { Ok(StatusCode::NOT_FOUND.into_response()) }).boxed(),
),
}
}

pub fn route<I, T>(mut self, method: Method, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
let boxed_service = Arc::new(BoxService::new(service.into_endpoint_service()));
match self.routes.insert(path.to_string(), HashMap::new()) {
Ok(_) => {
if let Some(entry) = self.routes.at_mut(path).ok() {
entry.value.insert(method, boxed_service);
}
},
Err(_err) => {
if let Some(existing) = self.routes.at_mut(path).ok() {
existing.value.insert(method, boxed_service);
}
}
};
self
}

pub fn get<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::GET, path, service)
}

pub fn post<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::POST, path, service)
}

pub fn put<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::PUT, path, service)
}

pub fn delete<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::DELETE, path, service)
}

pub fn patch<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::PATCH, path, service)
}

pub fn head<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::HEAD, path, service)
}

pub fn options<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::OPTIONS, path, service)
}

pub fn trace<I, T>(self, path: &str, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.route(Method::TRACE, path, service)
}

/// use the given service in case no match could be found.
pub fn not_found<I, T>(mut self, service: I) -> Self
where
I: IntoEndpointService<State, T>,
{
self.not_found = Arc::new(service.into_endpoint_service().boxed());
self
}
}

impl<State> Service<State, Request> for Router<State>
where
State: Clone + Send + Sync + 'static,
{
type Response = Response;
type Error = Infallible;

async fn serve(
&self,
mut ctx: Context<State>,
req: Request<>,
) -> Result<Self::Response, Self::Error> {
let uri_string = req.uri().to_string();
match &self.routes.at(uri_string.as_str()) {
Ok(matched) => {
let params: HashMap<String, String> = matched.params.clone().iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need to be collected as https://docs.rs/rama/latest/rama/http/matcher/struct.UriParams.html to be compatible with the existing matcher object. Feel free to expand the logic of UriParams if you miss something.

E.g. might make code cleaner here if you implement FromIterator for UriParams, but that's more of an optional thing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also in general there should be no reason to double allocate.

  1. first allocation is .clone()
  2. than you allocate again by turning the pairs into owned pairs

Probably that first clone isn't needed

ctx.insert(params);
if let Some(service) = matched.value.get(&req.method()) {
service.boxed().serve(ctx, req).await
} else {
self.not_found.serve(ctx, req).await
}
},
Err(_err) => {
self.not_found.serve(ctx, req).await
}
}
}
}