Skip to content

Commit

Permalink
refactor: handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Nov 21, 2024
1 parent aa3c5ac commit ff49502
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion crates/torii/server/src/handlers/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl GraphQLHandler {

#[async_trait::async_trait]
impl Handler for GraphQLHandler {
fn can_handle(&self, req: &Request<Body>) -> bool {
fn should_handle(&self, req: &Request<Body>) -> bool {
req.uri().path().starts_with("/graphql")
}

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/src/handlers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl GrpcHandler {

#[async_trait::async_trait]
impl Handler for GrpcHandler {
fn can_handle(&self, req: &Request<Body>) -> bool {
fn should_handle(&self, req: &Request<Body>) -> bool {
req.headers()
.get(CONTENT_TYPE)
.and_then(|ct| ct.to_str().ok())
Expand Down
4 changes: 2 additions & 2 deletions crates/torii/server/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use hyper::{Body, Request, Response};

#[async_trait::async_trait]
pub trait Handler: Send + Sync {
// Check if this handler can handle the given request
fn can_handle(&self, req: &Request<Body>) -> bool;
// Check if this handler should handle the given request
fn should_handle(&self, req: &Request<Body>) -> bool;

// Handle the request
async fn handle(&self, req: Request<Body>) -> Response<Body>;
Expand Down
19 changes: 13 additions & 6 deletions crates/torii/server/src/handlers/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,25 @@ impl SqlHandler {
}
}

fn extract_query(&self, req: Request<Body>) -> Result<String, Response<Body>> {
async fn extract_query(&self, req: Request<Body>) -> Result<String, Response<Body>> {
match *req.method() {
Method::GET => {
// Get the query from the query params
let params = req.uri().query().unwrap_or_default();
Ok(form_urlencoded::parse(params.as_bytes())
.find(|(key, _)| key == "q")
.find(|(key, _)| key == "q" || key == "query")
.map(|(_, value)| value.to_string())
.unwrap_or_default())
}
Method::POST => {
// Note: This would need to be adjusted to handle the async body reading
Ok(String::new()) // Placeholder
// Get the query from request body
let body_bytes = hyper::body::to_bytes(req.into_body()).await.unwrap_or_default();
String::from_utf8(body_bytes.to_vec()).map_err(|_| {
Response::builder()
.status(StatusCode::BAD_REQUEST)
.body(Body::from("Invalid query"))
.unwrap()
})
}
_ => Err(Response::builder()
.status(StatusCode::METHOD_NOT_ALLOWED)
Expand All @@ -96,12 +103,12 @@ impl SqlHandler {

#[async_trait::async_trait]
impl Handler for SqlHandler {
fn can_handle(&self, req: &Request<Body>) -> bool {
fn should_handle(&self, req: &Request<Body>) -> bool {
req.uri().path().starts_with("/sql")
}

async fn handle(&self, req: Request<Body>) -> Response<Body> {
match self.extract_query(req) {
match self.extract_query(req).await {
Ok(query) => self.execute_query(query).await,
Err(response) => response,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/src/handlers/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl StaticHandler {

#[async_trait::async_trait]
impl Handler for StaticHandler {
fn can_handle(&self, req: &Request<Body>) -> bool {
fn should_handle(&self, req: &Request<Body>) -> bool {
req.uri().path().starts_with("/static")
}

Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ async fn handle(
];

for handler in handlers {
if handler.can_handle(&req) {
if handler.should_handle(&req) {
return Ok(handler.handle(req).await);
}
}
Expand Down

0 comments on commit ff49502

Please sign in to comment.