Skip to content

Commit

Permalink
refactorC(torii-server): initializing handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkooo committed Mar 3, 2025
1 parent 429f238 commit 2d1a6f3
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/torii/server/src/handlers/graphql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::Handler;

pub(crate) const LOG_TARGET: &str = "torii::server::handlers::graphql";

#[derive(Debug)]
pub struct GraphQLHandler {
client_ip: IpAddr,
graphql_addr: Option<SocketAddr>,
Expand Down
1 change: 1 addition & 0 deletions crates/torii/server/src/handlers/grpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::Handler;

pub(crate) const LOG_TARGET: &str = "torii::server::handlers::grpc";

#[derive(Debug)]
pub struct GrpcHandler {
client_ip: IpAddr,
grpc_addr: Option<SocketAddr>,
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/src/handlers/mcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ struct ResourceCapabilities {
list_changed: bool,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct McpHandler {
pool: Arc<SqlitePool>,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/torii/server/src/handlers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod static_files;
use hyper::{Body, Request, Response};

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

Expand Down
1 change: 1 addition & 0 deletions crates/torii/server/src/handlers/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use sqlx::{Column, Row, SqlitePool, TypeInfo};

use super::Handler;

#[derive(Debug)]
pub struct SqlHandler {
pool: Arc<SqlitePool>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/torii/server/src/handlers/static_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use super::Handler;

pub(crate) const LOG_TARGET: &str = "torii::server::handlers::static";

#[derive(Debug)]
pub struct StaticHandler {
client_ip: IpAddr,
artifacts_addr: Option<SocketAddr>,
Expand Down
12 changes: 4 additions & 8 deletions crates/torii/server/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct Proxy {
artifacts_addr: Option<SocketAddr>,
graphql_addr: Arc<RwLock<Option<SocketAddr>>>,
pool: Arc<SqlitePool>,
handlers: Option<Vec<Box<dyn Handler>>>,
}

impl Proxy {
Expand All @@ -86,6 +87,7 @@ impl Proxy {
graphql_addr: Arc::new(RwLock::new(graphql_addr)),
artifacts_addr,
pool,
handlers: None
}
}

Expand All @@ -107,6 +109,8 @@ impl Proxy {

let make_svc = make_service_fn(move |conn: &AddrStream| {
let remote_addr = conn.remote_addr().ip();
let handlers = self.handlers.clone().unwrap_or_default();

let cors = CorsLayer::new()
.max_age(DEFAULT_MAX_AGE)
.allow_methods([Method::GET, Method::POST])
Expand Down Expand Up @@ -172,14 +176,6 @@ async fn handle(
pool: Arc<SqlitePool>,
req: Request<Body>,
) -> Result<Response<Body>, Infallible> {
let handlers: Vec<Box<dyn Handler>> = vec![
Box::new(SqlHandler::new(pool.clone())),
Box::new(GraphQLHandler::new(client_ip, graphql_addr)),
Box::new(GrpcHandler::new(client_ip, grpc_addr)),
Box::new(StaticHandler::new(client_ip, artifacts_addr)),
Box::new(McpHandler::new(pool.clone())),
];

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

0 comments on commit 2d1a6f3

Please sign in to comment.