Skip to content

Commit

Permalink
refactor(torii-server): combine tonic and warp (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy authored Sep 26, 2023
1 parent 345bba1 commit 68b5faa
Show file tree
Hide file tree
Showing 8 changed files with 254 additions and 138 deletions.
72 changes: 48 additions & 24 deletions Cargo.lock

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

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,17 @@ toml = "0.7.4"
tracing = "0.1.34"
tracing-subscriber = { version = "0.3.16", features = [ "env-filter" ] }
url = "2.4.0"

# server
hyper = "0.14.27"
warp = "0.3"

# gRPC
prost = "0.12"
tonic = "0.10"
tonic-build = "0.10"
tonic-web = "0.10.1"

[patch."https://github.com/starkware-libs/blockifier"]
blockifier = { git = "https://github.com/dojoengine/blockifier", rev = "f7df9ba" }

Expand Down
7 changes: 3 additions & 4 deletions crates/torii/grpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ repository.workspace = true
version.workspace = true

[dependencies]
bytes = "1.0"
prost = "0.11"
prost.workspace = true
sqlx = { version = "0.6.2", features = [ "chrono", "macros", "offline", "runtime-actix-rustls", "sqlite", "uuid" ] }
tonic = "0.9"
tonic.workspace = true
warp.workspace = true

[build-dependencies]
tonic-build = "0.9"
tonic-build.workspace = true
56 changes: 55 additions & 1 deletion crates/torii/grpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
pub mod route;
use sqlx::{Pool, Sqlite};
use tonic::{Request, Response, Status};
use world::world_server::World;
use world::{MetaReply, MetaRequest};

pub mod world {
tonic::include_proto!("world");
}

#[derive(Clone, Debug)]
pub struct DojoWorld {
pool: Pool<Sqlite>,
}

impl DojoWorld {
pub fn new(pool: Pool<Sqlite>) -> Self {
Self { pool }
}
}

#[tonic::async_trait]
impl World for DojoWorld {
async fn meta(
&self,
request: Request<MetaRequest>, // Accept request of type MetaRequest
) -> Result<Response<MetaReply>, Status> {
let id = request.into_inner().id;

let (world_address, world_class_hash, executor_address, executor_class_hash): (
String,
String,
String,
String,
) = sqlx::query_as(
"SELECT world_address, world_class_hash, executor_address, executor_class_hash FROM \
worlds WHERE id = ?",
)
.bind(id)
.fetch_one(&self.pool)
.await
.map_err(|e| match e {
sqlx::Error::RowNotFound => Status::not_found("World not found"),
_ => Status::internal("Internal error"),
})?;

let reply = world::MetaReply {
world_address,
world_class_hash,
executor_address,
executor_class_hash,
};

Ok(Response::new(reply)) // Send back our formatted greeting
}
}
90 changes: 0 additions & 90 deletions crates/torii/grpc/src/route.rs

This file was deleted.

10 changes: 8 additions & 2 deletions crates/torii/server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ clap.workspace = true
ctrlc = "3.2.5"
dojo-types = { path = "../../dojo-types" }
dojo-world = { path = "../../dojo-world" }
http = "0.2.9"
hyper.workspace = true
indexmap = "1.9.3"
poem = "1.3.48"
scarb.workspace = true
Expand All @@ -27,21 +29,25 @@ starknet.workspace = true
tokio-stream = "0.1.11"
tokio-util = "0.7.7"
tokio.workspace = true
tonic-web.workspace = true
torii-client = { path = "../client" }
torii-core = { path = "../core" }
torii-graphql = { path = "../graphql" }
torii-grpc = { path = "../grpc" }
tower = "0.4.13"
tracing-subscriber.workspace = true
tracing.workspace = true
url.workspace = true
warp.workspace = true
http-body = "0.4.5"
either = "1.9.0"

[dev-dependencies]
camino.workspace = true

[features]
default = [ "sqlite" ]
sqlite = [ "sqlx/sqlite" ]
default = ["sqlite"]
sqlite = ["sqlx/sqlite"]

[[bin]]
name = "torii"
Expand Down
Loading

0 comments on commit 68b5faa

Please sign in to comment.