From 3a250230df0112097f30d7b4bfb63f810bb98f34 Mon Sep 17 00:00:00 2001 From: projectmoon Date: Sat, 2 Jan 2021 21:26:09 +0000 Subject: [PATCH] Experimenting with tonic --- Cargo.lock | 270 ++++++++++++++++++++++++++++++++++++++- Cargo.toml | 3 + build.rs | 14 +- src/main.rs | 75 +++++++++-- src/models/proto/cofd.rs | 6 +- 5 files changed, 351 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fc9f489..d2cffa6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -98,6 +98,27 @@ version = "0.5.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" +[[package]] +name = "async-stream" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22068c0c19514942eefcfd4daf8976ef1aad84e61539f95cd200c35202f80af5" +dependencies = [ + "async-stream-impl", + "futures-core", +] + +[[package]] +name = "async-stream-impl" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25f9db3b38af870bf7e5cc649167533b493928e50744e2c30ae350230b414670" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "async-trait" version = "0.1.42" @@ -156,6 +177,12 @@ version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b" +[[package]] +name = "base64" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff" + [[package]] name = "base64" version = "0.13.0" @@ -346,7 +373,7 @@ version = "0.15.0-dev" source = "git+https://github.com/SergioBenitez/cookie-rs.git?rev=1c3ca83#1c3ca838543b60a4448d279dc4b903cc7a2bc22a" dependencies = [ "aes-gcm", - "base64", + "base64 0.13.0", "hkdf", "percent-encoding", "rand 0.7.3", @@ -1668,6 +1695,7 @@ dependencies = [ "rand_chacha 0.2.2", "rand_core 0.5.1", "rand_hc 0.2.0", + "rand_pcg", ] [[package]] @@ -1738,6 +1766,15 @@ dependencies = [ "rand_core 0.6.0", ] +[[package]] +name = "rand_pcg" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16abd0c1b639e9eb4d7c50c0b8100b0d0f849be2349829c740fe8e6eb4816429" +dependencies = [ + "rand_core 0.5.1", +] + [[package]] name = "redox_syscall" version = "0.1.57" @@ -1936,7 +1973,7 @@ version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b18820d944b33caa75a71378964ac46f58517c92b6ae5f762636247c09e78fb" dependencies = [ - "base64", + "base64 0.13.0", "blake2b_simd", "constant_time_eq", "crossbeam-utils", @@ -2408,6 +2445,9 @@ dependencies = [ "sqlx", "strum", "thiserror", + "tokio", + "tonic", + "tonic-build", ] [[package]] @@ -2592,12 +2632,226 @@ dependencies = [ "serde", ] +[[package]] +name = "tonic" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74a5d6e7439ecf910463667080de772a9c7ddf26bc9fb4f3252ac3862e43337d" +dependencies = [ + "async-stream", + "async-trait", + "base64 0.12.3", + "bytes", + "futures-core", + "futures-util", + "http", + "http-body", + "hyper", + "percent-encoding", + "pin-project 0.4.27", + "prost", + "prost-derive", + "tokio", + "tokio-util", + "tower", + "tower-balance", + "tower-load", + "tower-make", + "tower-service", + "tracing", + "tracing-futures", +] + +[[package]] +name = "tonic-build" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "19970cf58f3acc820962be74c4021b8bbc8e8a1c4e3a02095d0aa60cde5f3633" +dependencies = [ + "proc-macro2", + "prost-build", + "quote", + "syn", +] + +[[package]] +name = "tower" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd3169017c090b7a28fce80abaad0ab4f5566423677c9331bb320af7e49cfe62" +dependencies = [ + "futures-core", + "tower-buffer", + "tower-discover", + "tower-layer", + "tower-limit", + "tower-load-shed", + "tower-retry", + "tower-service", + "tower-timeout", + "tower-util", +] + +[[package]] +name = "tower-balance" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a792277613b7052448851efcf98a2c433e6f1d01460832dc60bef676bc275d4c" +dependencies = [ + "futures-core", + "futures-util", + "indexmap", + "pin-project 0.4.27", + "rand 0.7.3", + "slab", + "tokio", + "tower-discover", + "tower-layer", + "tower-load", + "tower-make", + "tower-ready-cache", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-buffer" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c4887dc2a65d464c8b9b66e0e4d51c2fd6cf5b3373afc72805b0a60bce00446a" +dependencies = [ + "futures-core", + "pin-project 0.4.27", + "tokio", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-discover" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f6b5000c3c54d269cc695dff28136bb33d08cbf1df2c48129e143ab65bf3c2a" +dependencies = [ + "futures-core", + "pin-project 0.4.27", + "tower-service", +] + +[[package]] +name = "tower-layer" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a35d656f2638b288b33495d1053ea74c40dc05ec0b92084dd71ca5566c4ed1dc" + +[[package]] +name = "tower-limit" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92c3040c5dbed68abffaa0d4517ac1a454cd741044f33ab0eefab6b8d1361404" +dependencies = [ + "futures-core", + "pin-project 0.4.27", + "tokio", + "tower-layer", + "tower-load", + "tower-service", +] + +[[package]] +name = "tower-load" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cc79fc3afd07492b7966d7efa7c6c50f8ed58d768a6075dd7ae6591c5d2017b" +dependencies = [ + "futures-core", + "log", + "pin-project 0.4.27", + "tokio", + "tower-discover", + "tower-service", +] + +[[package]] +name = "tower-load-shed" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f021e23900173dc315feb4b6922510dae3e79c689b74c089112066c11f0ae4e" +dependencies = [ + "futures-core", + "pin-project 0.4.27", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-make" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce50370d644a0364bf4877ffd4f76404156a248d104e2cc234cd391ea5cdc965" +dependencies = [ + "tokio", + "tower-service", +] + +[[package]] +name = "tower-ready-cache" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4eabb6620e5481267e2ec832c780b31cad0c15dcb14ed825df5076b26b591e1f" +dependencies = [ + "futures-core", + "futures-util", + "indexmap", + "log", + "tokio", + "tower-service", +] + +[[package]] +name = "tower-retry" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6727956aaa2f8957d4d9232b308fe8e4e65d99db30f42b225646e86c9b6a952" +dependencies = [ + "futures-core", + "pin-project 0.4.27", + "tokio", + "tower-layer", + "tower-service", +] + [[package]] name = "tower-service" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" +[[package]] +name = "tower-timeout" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "127b8924b357be938823eaaec0608c482d40add25609481027b96198b2e4b31e" +dependencies = [ + "pin-project 0.4.27", + "tokio", + "tower-layer", + "tower-service", +] + +[[package]] +name = "tower-util" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1093c19826d33807c72511e68f73b4a0469a3f22c2bd5f7d5212178b4b89674" +dependencies = [ + "futures-core", + "futures-util", + "pin-project 0.4.27", + "tower-service", +] + [[package]] name = "tracing" version = "0.1.22" @@ -2607,9 +2861,21 @@ dependencies = [ "cfg-if 1.0.0", "log", "pin-project-lite 0.2.0", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.17" diff --git a/Cargo.toml b/Cargo.toml index 5eb49b6..7862f6e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,8 +16,10 @@ path = "src/main.rs" [build-dependencies] prost-build = "0.6" +tonic-build = "0.3" [dependencies] +tonic = "0.3" prost = "0.6" serde = "1.0" serde_derive = "1.0" @@ -28,6 +30,7 @@ rust-argon2 = "0.8" log = "0.4" rand = "0.7" futures = "0.3" +tokio = { version = "0.2", features = ["macros"] } strum = { version = "0.20", features = ["derive"] } sqlx = { version = "0.4.2", features = [ "offline", "sqlite", "runtime-tokio-native-tls" ] } refinery = { version = "0.3", features = ["rusqlite"]} diff --git a/build.rs b/build.rs index a9bb398..63b2cb9 100644 --- a/build.rs +++ b/build.rs @@ -3,8 +3,18 @@ fn main() { config.btree_map(&["."]); config.type_attribute(".", "#[derive(Serialize)]"); config.type_attribute(".", "#[serde(rename_all = \"camelCase\")]"); - config - .compile_protos( + // config + // .compile_protos( + // &["proto/cofd.proto", "proto/cofd_api.proto"], + // &["src/", "proto/"], + // ) + // .unwrap(); + + tonic_build::configure() + .build_server(true) + .build_client(false) + .compile_with_config( + config, &["proto/cofd.proto", "proto/cofd_api.proto"], &["src/", "proto/"], ) diff --git a/src/main.rs b/src/main.rs index 29b755c..daaa95d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,19 +20,43 @@ pub mod migrator; pub mod models; pub mod routes; -#[rocket::main] -async fn main() -> Result<(), Box> { - let args: Vec = env::args().collect(); - let db_path: &str = match &args[..] { - [_, path] => path.as_ref(), - [_, _, ..] => panic!("Expected exactly 0 or 1 argument"), - _ => "tenebrous.sqlite", - }; +async fn gin_and_tonic() -> Result<(), Box> { + use crate::models::proto::cofd::api::cofd_api_server::{CofdApi, CofdApiServer}; + use crate::models::proto::cofd::api::UpdateSkillValueRequest; + use crate::models::proto::cofd::cofd_sheet::Skill; + use tonic::{transport::Server, Request, Response, Status}; - println!("Using database: {}", db_path); + #[derive(Debug, Default)] + pub struct MyGreeter {} - migrator::migrate(db_path).await?; + #[tonic::async_trait] + impl CofdApi for MyGreeter { + async fn update_skill_value( + &self, + request: Request, // Accept request of type HelloRequest + ) -> Result, Status> { + // Return an instance of type HelloReply + println!("Got a request: {:?}", request); + let reply = Skill::default(); + + Ok(Response::new(reply)) // Send back our formatted greeting + } + } + + let addr = "[::1]:50051".parse()?; + let greeter = MyGreeter::default(); + + println!("Running tonic"); + Server::builder() + .add_service(CofdApiServer::new(greeter)) + .serve(addr) + .await?; + + Ok(()) +} + +async fn make_rocket(database: sqlx::SqlitePool) -> Result<(), Box> { let root_routes: Vec = { routes::root::routes() .into_iter() @@ -47,7 +71,7 @@ async fn main() -> Result<(), Box> { rocket::ignite() .attach(Template::fairing()) //.attach(db::TenebrousDbConn::fairing()) - .manage(crate::db::create_pool(db_path).await?) + .manage(database) .mount("/", root_routes) .mount("/characters", character_routes) .mount("/api", api_routes) @@ -64,3 +88,32 @@ async fn main() -> Result<(), Box> { .await .map_err(|e| e.into()) } + +#[rocket::main] +async fn main() -> Result<(), Box> { + let args: Vec = env::args().collect(); + let db_path: &str = match &args[..] { + [_, path] => path.as_ref(), + [_, _, ..] => panic!("Expected exactly 0 or 1 argument"), + _ => "tenebrous.sqlite", + }; + + println!("Using database: {}", db_path); + + migrator::migrate(db_path).await?; + + let db = crate::db::create_pool(db_path).await?; + // let tonic = gin_and_tonic(); + // let rocket = make_rocket(db); + + tokio::select! { + _ = make_rocket(db) => { + println!("done with rocket"); + } + _ = gin_and_tonic() => { + println!("tonic done"); + } + } + + Ok(()) +} diff --git a/src/models/proto/cofd.rs b/src/models/proto/cofd.rs index 6ad0dd5..cc98d1f 100644 --- a/src/models/proto/cofd.rs +++ b/src/models/proto/cofd.rs @@ -6,11 +6,13 @@ use crate::models::characters::CharacterDataType; use std::collections::BTreeMap; //Add the generated protobuf code into this module. -include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.rs")); +//include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.rs")); +tonic::include_proto!("models.proto.cofd"); //Add the API protobuf genreated code for the api module. pub mod api { - include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs")); + //include!(concat!(env!("OUT_DIR"), "/models.proto.cofd.api.rs")); + tonic::include_proto!("models.proto.cofd.api"); } /// Default mental skill names for a regular Chronicles of Darkness