From 07185748dbc9276707acf7b9a892d56c9d9822b5 Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Fri, 12 Jan 2024 12:09:20 +0100 Subject: [PATCH 1/8] chore(notifications): WIP integrate tonic --- Cargo.lock | 153 ++++++- Cargo.toml | 2 + core/notifications/BUCK | 5 +- core/notifications/Cargo.toml | 5 + core/notifications/build.rs | 7 + core/notifications/proto/notifications.proto | 20 + core/notifications/src/lib.rs | 5 + flake.nix | 1 + third-party/rust/BUCK | 394 +++++++++++++++++- third-party/rust/Cargo.lock | 176 +++++++- third-party/rust/Cargo.toml | 4 + .../rust/fixups/prettyplease/fixups.toml | 1 + 12 files changed, 759 insertions(+), 14 deletions(-) create mode 100644 core/notifications/build.rs create mode 100644 core/notifications/proto/notifications.proto create mode 100644 third-party/rust/fixups/prettyplease/fixups.toml diff --git a/Cargo.lock b/Cargo.lock index 09677284fe..1835ed3a49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -923,6 +923,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6" +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "flume" version = "0.11.0" @@ -1642,6 +1648,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "multimap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" + [[package]] name = "nom" version = "7.1.3" @@ -1666,6 +1678,7 @@ dependencies = [ "es-entity", "jsonwebtoken", "mongodb", + "prost 0.12.3", "rand", "reqwest", "serde", @@ -1675,6 +1688,8 @@ dependencies = [ "sqlx", "thiserror", "tokio", + "tonic 0.10.2", + "tonic-build", "tracing 0.1.0", "uuid", ] @@ -1810,11 +1825,11 @@ dependencies = [ "opentelemetry-semantic-conventions", "opentelemetry_api", "opentelemetry_sdk", - "prost", + "prost 0.11.9", "reqwest", "thiserror", "tokio", - "tonic", + "tonic 0.9.2", ] [[package]] @@ -1825,8 +1840,8 @@ checksum = "b1e3f814aa9f8c905d0ee4bde026afd3b2577a97c10e1699912e3e44f0c4cbeb" dependencies = [ "opentelemetry_api", "opentelemetry_sdk", - "prost", - "tonic", + "prost 0.11.9", + "tonic 0.9.2", ] [[package]] @@ -1966,6 +1981,16 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap 2.0.2", +] + [[package]] name = "pin-project" version = "1.1.3" @@ -2037,6 +2062,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "prettyplease" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5" +dependencies = [ + "proc-macro2", + "syn 2.0.48", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -2063,7 +2098,39 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" dependencies = [ "bytes", - "prost-derive", + "prost-derive 0.11.9", +] + +[[package]] +name = "prost" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +dependencies = [ + "bytes", + "prost-derive 0.12.3", +] + +[[package]] +name = "prost-build" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c55e02e35260070b6f716a2423c2ff1c3bb1642ddca6f99e1f26d06268a0e2d2" +dependencies = [ + "bytes", + "heck", + "itertools 0.11.0", + "log", + "multimap", + "once_cell", + "petgraph", + "prettyplease", + "prost 0.12.3", + "prost-types", + "regex", + "syn 2.0.48", + "tempfile", + "which", ] [[package]] @@ -2079,6 +2146,28 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "prost-derive" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +dependencies = [ + "anyhow", + "itertools 0.11.0", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "prost-types" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e" +dependencies = [ + "prost 0.12.3", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -3194,7 +3283,34 @@ dependencies = [ "hyper-timeout", "percent-encoding", "pin-project", - "prost", + "prost 0.11.9", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing 0.1.40", +] + +[[package]] +name = "tonic" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64 0.21.5", + "bytes", + "h2", + "http", + "http-body", + "hyper", + "hyper-timeout", + "percent-encoding", + "pin-project", + "prost 0.12.3", "tokio", "tokio-stream", "tower", @@ -3203,6 +3319,19 @@ dependencies = [ "tracing 0.1.40", ] +[[package]] +name = "tonic-build" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d021fc044c18582b9a2408cd0dd05b1596e3ecdb5c4df822bb0183545683889" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "quote", + "syn 2.0.48", +] + [[package]] name = "tower" version = "0.4.13" @@ -3664,6 +3793,18 @@ version = "0.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "whoami" version = "1.4.1" diff --git a/Cargo.toml b/Cargo.toml index b19b8ae441..d6d00d2554 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -38,3 +38,5 @@ opentelemetry = { version = "0.20.0", features = ["trace", "rt-tokio"]} opentelemetry-semantic-conventions = "0.12.0" opentelemetry-http = "0.9.0" http = "0.2.9" +tonic = "0.10.2" +prost = "0.12" diff --git a/core/notifications/BUCK b/core/notifications/BUCK index fe243964c0..53d5b054b7 100644 --- a/core/notifications/BUCK +++ b/core/notifications/BUCK @@ -66,6 +66,9 @@ rust_library( "//third-party/rust:rand", "//third-party/rust:uuid", "//third-party/rust:serde_with", + "//third-party/rust:prost", + "//third-party/rust:tonic", + "//third-party/rust:tonic-build", ], srcs = glob([ "src/**/*.rs", @@ -75,7 +78,7 @@ rust_library( ]), env = { "CARGO_MANIFEST_DIR": ".", - "SQLX_OFFLINE": "true", + "SQLX_OFFLINE": "true" } ) diff --git a/core/notifications/Cargo.toml b/core/notifications/Cargo.toml index b97ed2eb82..f98cf9fe52 100644 --- a/core/notifications/Cargo.toml +++ b/core/notifications/Cargo.toml @@ -37,3 +37,8 @@ mongodb = { workspace = true } rand = { workspace = true } uuid = { workspace = true } serde_with = { workspace = true } +tonic = { workspace = true } +prost = { workspace = true } + +[build-dependencies] +tonic-build = { version = "0.10.2", features = ["prost"] } diff --git a/core/notifications/build.rs b/core/notifications/build.rs new file mode 100644 index 0000000000..2b6043a5e7 --- /dev/null +++ b/core/notifications/build.rs @@ -0,0 +1,7 @@ +fn main() -> Result<(), Box> { + println!("cargo:rerun-if-changed=migrations"); + + // std::env::set_var("PROTOC", protobuf_src::protoc()); + tonic_build::configure().compile(&["proto/notifications.proto"], &["proto"])?; + Ok(()) +} diff --git a/core/notifications/proto/notifications.proto b/core/notifications/proto/notifications.proto new file mode 100644 index 0000000000..4f49dc208e --- /dev/null +++ b/core/notifications/proto/notifications.proto @@ -0,0 +1,20 @@ +syntax = "proto3"; + +package helloworld; + +// The greeting service definition. +service Greeter { + // Sends a greeting + rpc SayHello (HelloRequest) returns (HelloReply) {} +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; +} + +// The response message containing the greetings. +message HelloReply { + string message = 1; +} + diff --git a/core/notifications/src/lib.rs b/core/notifications/src/lib.rs index 02a26eefd2..550168f62b 100644 --- a/core/notifications/src/lib.rs +++ b/core/notifications/src/lib.rs @@ -8,3 +8,8 @@ mod user_notification_settings; pub mod cli; pub mod graphql; + +#[allow(clippy::all)] +pub mod proto { + tonic::include_proto!("helloworld"); +} diff --git a/flake.nix b/flake.nix index 0da7bdda58..92f6af05e0 100644 --- a/flake.nix +++ b/flake.nix @@ -71,6 +71,7 @@ cargo-watch reindeer gitMinimal + protobuf ] ++ buck2NativeBuildInputs ++ lib.optionals pkgs.stdenv.isLinux [ diff --git a/third-party/rust/BUCK b/third-party/rust/BUCK index de0470b96d..1f57072f46 100644 --- a/third-party/rust/BUCK +++ b/third-party/rust/BUCK @@ -2108,6 +2108,23 @@ cargo.rust_library( visibility = [], ) +http_archive( + name = "fixedbitset-0.4.2.crate", + sha256 = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80", + strip_prefix = "fixedbitset-0.4.2", + urls = ["https://crates.io/api/v1/crates/fixedbitset/0.4.2/download"], + visibility = [], +) + +cargo.rust_library( + name = "fixedbitset-0.4.2", + srcs = [":fixedbitset-0.4.2.crate"], + crate = "fixedbitset", + crate_root = "fixedbitset-0.4.2.crate/src/lib.rs", + edition = "2015", + visibility = [], +) + http_archive( name = "flume-0.11.0.crate", sha256 = "55ac459de2512911e4b674ce33cf20befaba382d05b62b008afc1c8b57cbf181", @@ -3264,7 +3281,11 @@ cargo.rust_library( crate = "itertools", crate_root = "itertools-0.10.5.crate/src/lib.rs", edition = "2018", - features = ["use_alloc"], + features = [ + "default", + "use_alloc", + "use_std", + ], visibility = [], deps = [":either-1.9.0"], ) @@ -3915,6 +3936,23 @@ cargo.rust_library( ], ) +http_archive( + name = "multimap-0.8.3.crate", + sha256 = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a", + strip_prefix = "multimap-0.8.3", + urls = ["https://crates.io/api/v1/crates/multimap/0.8.3/download"], + visibility = [], +) + +cargo.rust_library( + name = "multimap-0.8.3", + srcs = [":multimap-0.8.3.crate"], + crate = "multimap", + crate_root = "multimap-0.8.3.crate/src/lib.rs", + edition = "2015", + visibility = [], +) + http_archive( name = "nom-7.1.3.crate", sha256 = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a", @@ -4771,6 +4809,27 @@ cargo.rust_library( ], ) +http_archive( + name = "petgraph-0.6.4.crate", + sha256 = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9", + strip_prefix = "petgraph-0.6.4", + urls = ["https://crates.io/api/v1/crates/petgraph/0.6.4/download"], + visibility = [], +) + +cargo.rust_library( + name = "petgraph-0.6.4", + srcs = [":petgraph-0.6.4.crate"], + crate = "petgraph", + crate_root = "petgraph-0.6.4.crate/src/lib.rs", + edition = "2018", + visibility = [], + deps = [ + ":fixedbitset-0.4.2", + ":indexmap-2.0.2", + ], +) + http_archive( name = "pin-project-1.1.3.crate", sha256 = "fda4ed1c6c173e3fc7a83629421152e01d7b1f9b7f65fb301e490e8cfc656422", @@ -4956,6 +5015,27 @@ cargo.rust_library( visibility = [], ) +http_archive( + name = "prettyplease-0.2.16.crate", + sha256 = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5", + strip_prefix = "prettyplease-0.2.16", + urls = ["https://crates.io/api/v1/crates/prettyplease/0.2.16/download"], + visibility = [], +) + +cargo.rust_library( + name = "prettyplease-0.2.16", + srcs = [":prettyplease-0.2.16.crate"], + crate = "prettyplease", + crate_root = "prettyplease-0.2.16.crate/src/lib.rs", + edition = "2021", + visibility = [], + deps = [ + ":proc-macro2-1.0.76", + ":syn-2.0.48", + ], +) + http_archive( name = "proc-macro-crate-1.3.1.crate", sha256 = "7f4c021e1093a56626774e81216a4ce732a735e5bad4868a03f3ed65ca0c3919", @@ -5024,6 +5104,38 @@ buildscript_run( version = "1.0.76", ) +alias( + name = "prost", + actual = ":prost-0.10.4", + visibility = ["PUBLIC"], +) + +http_archive( + name = "prost-0.10.4.crate", + sha256 = "71adf41db68aa0daaefc69bb30bcd68ded9b9abaad5d1fbb6304c4fb390e083e", + strip_prefix = "prost-0.10.4", + urls = ["https://crates.io/api/v1/crates/prost/0.10.4/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-0.10.4", + srcs = [":prost-0.10.4.crate"], + crate = "prost", + crate_root = "prost-0.10.4.crate/src/lib.rs", + edition = "2018", + features = [ + "default", + "prost-derive", + "std", + ], + visibility = [], + deps = [ + ":bytes-1.5.0", + ":prost-derive-0.10.1", + ], +) + http_archive( name = "prost-0.11.9.crate", sha256 = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd", @@ -5050,6 +5162,95 @@ cargo.rust_library( ], ) +http_archive( + name = "prost-0.12.3.crate", + sha256 = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a", + strip_prefix = "prost-0.12.3", + urls = ["https://crates.io/api/v1/crates/prost/0.12.3/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-0.12.3", + srcs = [":prost-0.12.3.crate"], + crate = "prost", + crate_root = "prost-0.12.3.crate/src/lib.rs", + edition = "2021", + features = [ + "prost-derive", + "std", + ], + visibility = [], + deps = [ + ":bytes-1.5.0", + ":prost-derive-0.12.3", + ], +) + +http_archive( + name = "prost-build-0.12.3.crate", + sha256 = "c55e02e35260070b6f716a2423c2ff1c3bb1642ddca6f99e1f26d06268a0e2d2", + strip_prefix = "prost-build-0.12.3", + urls = ["https://crates.io/api/v1/crates/prost-build/0.12.3/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-build-0.12.3", + srcs = [":prost-build-0.12.3.crate"], + crate = "prost_build", + crate_root = "prost-build-0.12.3.crate/src/lib.rs", + edition = "2021", + features = [ + "default", + "format", + "prettyplease", + "syn", + ], + visibility = [], + deps = [ + ":bytes-1.5.0", + ":heck-0.4.1", + ":itertools-0.11.0", + ":log-0.4.20", + ":multimap-0.8.3", + ":once_cell-1.18.0", + ":petgraph-0.6.4", + ":prettyplease-0.2.16", + ":prost-0.12.3", + ":prost-types-0.12.3", + ":regex-1.10.2", + ":syn-2.0.48", + ":tempfile-3.8.1", + ":which-4.4.2", + ], +) + +http_archive( + name = "prost-derive-0.10.1.crate", + sha256 = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc", + strip_prefix = "prost-derive-0.10.1", + urls = ["https://crates.io/api/v1/crates/prost-derive/0.10.1/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-derive-0.10.1", + srcs = [":prost-derive-0.10.1.crate"], + crate = "prost_derive", + crate_root = "prost-derive-0.10.1.crate/src/lib.rs", + edition = "2018", + proc_macro = True, + visibility = [], + deps = [ + ":anyhow-1.0.79", + ":itertools-0.10.5", + ":proc-macro2-1.0.76", + ":quote-1.0.35", + ":syn-1.0.109", + ], +) + http_archive( name = "prost-derive-0.11.9.crate", sha256 = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4", @@ -5075,6 +5276,49 @@ cargo.rust_library( ], ) +http_archive( + name = "prost-derive-0.12.3.crate", + sha256 = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e", + strip_prefix = "prost-derive-0.12.3", + urls = ["https://crates.io/api/v1/crates/prost-derive/0.12.3/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-derive-0.12.3", + srcs = [":prost-derive-0.12.3.crate"], + crate = "prost_derive", + crate_root = "prost-derive-0.12.3.crate/src/lib.rs", + edition = "2021", + proc_macro = True, + visibility = [], + deps = [ + ":anyhow-1.0.79", + ":itertools-0.11.0", + ":proc-macro2-1.0.76", + ":quote-1.0.35", + ":syn-2.0.48", + ], +) + +http_archive( + name = "prost-types-0.12.3.crate", + sha256 = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e", + strip_prefix = "prost-types-0.12.3", + urls = ["https://crates.io/api/v1/crates/prost-types/0.12.3/download"], + visibility = [], +) + +cargo.rust_library( + name = "prost-types-0.12.3", + srcs = [":prost-types-0.12.3.crate"], + crate = "prost_types", + crate_root = "prost-types-0.12.3.crate/src/lib.rs", + edition = "2021", + visibility = [], + deps = [":prost-0.12.3"], +) + http_archive( name = "quick-error-1.2.3.crate", sha256 = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0", @@ -8082,6 +8326,7 @@ cargo.rust_binary( ":opentelemetry-http-0.9.0", ":opentelemetry-otlp-0.13.0", ":opentelemetry-semantic-conventions-0.12.0", + ":prost-0.10.4", ":rand-0.8.5", ":reqwest-0.11.23", ":serde-1.0.195", @@ -8091,6 +8336,8 @@ cargo.rust_binary( ":sqlx-0.7.2", ":thiserror-1.0.50", ":tokio-1.33.0", + ":tonic-0.10.2", + ":tonic-build-0.10.2", ":tracing-0.1.40", ":tracing-opentelemetry-0.20.0", ":tracing-subscriber-0.3.18", @@ -8583,6 +8830,68 @@ cargo.rust_library( ], ) +alias( + name = "tonic", + actual = ":tonic-0.10.2", + visibility = ["PUBLIC"], +) + +http_archive( + name = "tonic-0.10.2.crate", + sha256 = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e", + strip_prefix = "tonic-0.10.2", + urls = ["https://crates.io/api/v1/crates/tonic/0.10.2/download"], + visibility = [], +) + +cargo.rust_library( + name = "tonic-0.10.2", + srcs = [":tonic-0.10.2.crate"], + crate = "tonic", + crate_root = "tonic-0.10.2.crate/src/lib.rs", + edition = "2021", + env = { + "CARGO_MANIFEST_DIR": "tonic-0.10.2.crate", + "CARGO_PKG_AUTHORS": "Lucio Franco ", + "CARGO_PKG_DESCRIPTION": "A gRPC over HTTP/2 implementation focused on high performance, interoperability, and flexibility.\n", + "CARGO_PKG_NAME": "tonic", + "CARGO_PKG_REPOSITORY": "https://github.com/hyperium/tonic", + "CARGO_PKG_VERSION": "0.10.2", + "CARGO_PKG_VERSION_MAJOR": "0", + "CARGO_PKG_VERSION_MINOR": "10", + "CARGO_PKG_VERSION_PATCH": "2", + }, + features = [ + "channel", + "codegen", + "default", + "prost", + "transport", + ], + visibility = [], + deps = [ + ":async-stream-0.3.5", + ":async-trait-0.1.74", + ":axum-0.6.20", + ":base64-0.21.5", + ":bytes-1.5.0", + ":h2-0.3.21", + ":http-0.2.9", + ":http-body-0.4.5", + ":hyper-0.14.27", + ":hyper-timeout-0.4.1", + ":percent-encoding-2.3.0", + ":pin-project-1.1.3", + ":prost-0.12.3", + ":tokio-1.33.0", + ":tokio-stream-0.1.14", + ":tower-0.4.13", + ":tower-layer-0.3.2", + ":tower-service-0.3.2", + ":tracing-0.1.40", + ], +) + http_archive( name = "tonic-0.9.2.crate", sha256 = "3082666a3a6433f7f511c7192923fa1fe07c69332d3c6a2e6bb040b569199d5a", @@ -8640,6 +8949,42 @@ cargo.rust_library( ], ) +alias( + name = "tonic-build", + actual = ":tonic-build-0.10.2", + visibility = ["PUBLIC"], +) + +http_archive( + name = "tonic-build-0.10.2.crate", + sha256 = "9d021fc044c18582b9a2408cd0dd05b1596e3ecdb5c4df822bb0183545683889", + strip_prefix = "tonic-build-0.10.2", + urls = ["https://crates.io/api/v1/crates/tonic-build/0.10.2/download"], + visibility = [], +) + +cargo.rust_library( + name = "tonic-build-0.10.2", + srcs = [":tonic-build-0.10.2.crate"], + crate = "tonic_build", + crate_root = "tonic-build-0.10.2.crate/src/lib.rs", + edition = "2021", + features = [ + "default", + "prost", + "prost-build", + "transport", + ], + visibility = [], + deps = [ + ":prettyplease-0.2.16", + ":proc-macro2-1.0.76", + ":prost-build-0.12.3", + ":quote-1.0.35", + ":syn-2.0.48", + ], +) + http_archive( name = "tower-0.4.13.crate", sha256 = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c", @@ -9584,6 +9929,53 @@ cargo.rust_library( visibility = [], ) +http_archive( + name = "which-4.4.2.crate", + sha256 = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7", + strip_prefix = "which-4.4.2", + urls = ["https://crates.io/api/v1/crates/which/4.4.2/download"], + visibility = [], +) + +cargo.rust_library( + name = "which-4.4.2", + srcs = [":which-4.4.2.crate"], + crate = "which", + crate_root = "which-4.4.2.crate/src/lib.rs", + edition = "2021", + platform = { + "linux-arm64": dict( + deps = [":home-0.5.5"], + ), + "linux-x86_64": dict( + deps = [":home-0.5.5"], + ), + "macos-arm64": dict( + deps = [":home-0.5.5"], + ), + "macos-x86_64": dict( + deps = [":home-0.5.5"], + ), + "windows-gnu": dict( + deps = [ + ":home-0.5.5", + ":once_cell-1.18.0", + ], + ), + "windows-msvc": dict( + deps = [ + ":home-0.5.5", + ":once_cell-1.18.0", + ], + ), + }, + visibility = [], + deps = [ + ":either-1.9.0", + ":rustix-0.38.21", + ], +) + http_archive( name = "whoami-1.4.1.crate", sha256 = "22fc3756b8a9133049b26c7f61ab35416c130e8c09b660f5b3958b446f52cc50", diff --git a/third-party/rust/Cargo.lock b/third-party/rust/Cargo.lock index 7c3f92e7ec..540c14ad9f 100644 --- a/third-party/rust/Cargo.lock +++ b/third-party/rust/Cargo.lock @@ -886,6 +886,12 @@ version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8fcfdc7a0362c9f4444381a9e697c79d435fe65b52a37466fc2c1184cee9edc6" +[[package]] +name = "fixedbitset" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ce7134b9999ecaf8bcd65542e436736ef32ddca1b3e06094cb6ec5755203b80" + [[package]] name = "flume" version = "0.11.0" @@ -1605,6 +1611,12 @@ dependencies = [ "version_check", ] +[[package]] +name = "multimap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5ce46fe64a9d73be07dcbe690a38ce1b293be448fd8ce1e6c1b8062c9f72c6a" + [[package]] name = "nom" version = "7.1.3" @@ -1746,11 +1758,11 @@ dependencies = [ "opentelemetry-semantic-conventions", "opentelemetry_api", "opentelemetry_sdk", - "prost", + "prost 0.11.9", "reqwest", "thiserror", "tokio", - "tonic", + "tonic 0.9.2", ] [[package]] @@ -1761,8 +1773,8 @@ checksum = "b1e3f814aa9f8c905d0ee4bde026afd3b2577a97c10e1699912e3e44f0c4cbeb" dependencies = [ "opentelemetry_api", "opentelemetry_sdk", - "prost", - "tonic", + "prost 0.11.9", + "tonic 0.9.2", ] [[package]] @@ -1902,6 +1914,16 @@ dependencies = [ "ucd-trie", ] +[[package]] +name = "petgraph" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d3afd2628e69da2be385eb6f2fd57c8ac7977ceeff6dc166ff1657b0e386a9" +dependencies = [ + "fixedbitset", + "indexmap 2.0.2", +] + [[package]] name = "pin-project" version = "1.1.3" @@ -1973,6 +1995,16 @@ version = "0.2.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" +[[package]] +name = "prettyplease" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a41cf62165e97c7f814d2221421dbb9afcbcdb0a88068e5ea206e19951c2cbb5" +dependencies = [ + "proc-macro2", + "syn 2.0.48", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -1992,6 +2024,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "prost" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71adf41db68aa0daaefc69bb30bcd68ded9b9abaad5d1fbb6304c4fb390e083e" +dependencies = [ + "bytes", + "prost-derive 0.10.1", +] + [[package]] name = "prost" version = "0.11.9" @@ -1999,7 +2041,52 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd" dependencies = [ "bytes", - "prost-derive", + "prost-derive 0.11.9", +] + +[[package]] +name = "prost" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a" +dependencies = [ + "bytes", + "prost-derive 0.12.3", +] + +[[package]] +name = "prost-build" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c55e02e35260070b6f716a2423c2ff1c3bb1642ddca6f99e1f26d06268a0e2d2" +dependencies = [ + "bytes", + "heck", + "itertools 0.11.0", + "log", + "multimap", + "once_cell", + "petgraph", + "prettyplease", + "prost 0.12.3", + "prost-types", + "regex", + "syn 2.0.48", + "tempfile", + "which", +] + +[[package]] +name = "prost-derive" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc" +dependencies = [ + "anyhow", + "itertools 0.10.5", + "proc-macro2", + "quote", + "syn 1.0.109", ] [[package]] @@ -2015,6 +2102,28 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "prost-derive" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "efb6c9a1dd1def8e2124d17e83a20af56f1570d6c2d2bd9e266ccb768df3840e" +dependencies = [ + "anyhow", + "itertools 0.11.0", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "prost-types" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "193898f59edcf43c26227dcd4c8427f00d99d61e95dcde58dabd49fa291d470e" +dependencies = [ + "prost 0.12.3", +] + [[package]] name = "quick-error" version = "1.2.3" @@ -2950,6 +3059,7 @@ dependencies = [ "opentelemetry-http", "opentelemetry-otlp", "opentelemetry-semantic-conventions", + "prost 0.10.4", "rand", "reqwest", "serde", @@ -2959,6 +3069,8 @@ dependencies = [ "sqlx", "thiserror", "tokio", + "tonic 0.10.2", + "tonic-build", "tracing", "tracing-opentelemetry", "tracing-subscriber", @@ -3164,7 +3276,34 @@ dependencies = [ "hyper-timeout", "percent-encoding", "pin-project", - "prost", + "prost 0.11.9", + "tokio", + "tokio-stream", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d560933a0de61cf715926b9cac824d4c883c2c43142f787595e48280c40a1d0e" +dependencies = [ + "async-stream", + "async-trait", + "axum", + "base64 0.21.5", + "bytes", + "h2", + "http", + "http-body", + "hyper", + "hyper-timeout", + "percent-encoding", + "pin-project", + "prost 0.12.3", "tokio", "tokio-stream", "tower", @@ -3173,6 +3312,19 @@ dependencies = [ "tracing", ] +[[package]] +name = "tonic-build" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d021fc044c18582b9a2408cd0dd05b1596e3ecdb5c4df822bb0183545683889" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "quote", + "syn 2.0.48", +] + [[package]] name = "tower" version = "0.4.13" @@ -3618,6 +3770,18 @@ version = "0.25.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14247bb57be4f377dfb94c72830b8ce8fc6beac03cf4bf7b9732eadd414123fc" +[[package]] +name = "which" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87ba24419a2078cd2b0f2ede2691b6c66d8e47836da3b6db8265ebad47afbfc7" +dependencies = [ + "either", + "home", + "once_cell", + "rustix", +] + [[package]] name = "whoami" version = "1.4.1" diff --git a/third-party/rust/Cargo.toml b/third-party/rust/Cargo.toml index c35562049e..3ead22e324 100644 --- a/third-party/rust/Cargo.toml +++ b/third-party/rust/Cargo.toml @@ -47,4 +47,8 @@ opentelemetry-semantic-conventions = "0.12.0" opentelemetry-http = "0.9.0" http = "0.2.9" +prost = "0.10.2" +tonic = "0.10.2" +tonic-build = { version = "0.10.2", features = ["prost"] } + version_check = "0.9.4" # for some reason reindeer didn't auto-add this diff --git a/third-party/rust/fixups/prettyplease/fixups.toml b/third-party/rust/fixups/prettyplease/fixups.toml new file mode 100644 index 0000000000..db40d72cb2 --- /dev/null +++ b/third-party/rust/fixups/prettyplease/fixups.toml @@ -0,0 +1 @@ +buildscript = [] From d215e22efb6adda11ce0cf69ebf257e8428a7c18 Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Fri, 12 Jan 2024 16:35:09 +0100 Subject: [PATCH 2/8] chore(notifications): build proto files in buck --- Cargo.toml | 1 + core/notifications/BUCK | 18 +++--- core/notifications/Cargo.toml | 2 +- core/notifications/build.rs | 16 ++++- third-party/proto/BUCK | 18 ++++++ third-party/proto/defs.bzl | 104 +++++++++++++++++++++++++++++++++ third-party/proto/releases.bzl | 46 +++++++++++++++ third-party/proto/update.py | 99 +++++++++++++++++++++++++++++++ third-party/rust/BUCK | 72 +++-------------------- third-party/rust/Cargo.lock | 25 +------- third-party/rust/Cargo.toml | 2 +- toolchains/BUCK | 6 ++ toolchains/rust/macros.bzl | 64 ++++++++++++++++++++ vendir.lock.yml | 10 +++- vendir.yml | 9 +++ vendir/template.yml | 9 +++ 16 files changed, 400 insertions(+), 101 deletions(-) create mode 100644 third-party/proto/BUCK create mode 100644 third-party/proto/defs.bzl create mode 100644 third-party/proto/releases.bzl create mode 100755 third-party/proto/update.py diff --git a/Cargo.toml b/Cargo.toml index d6d00d2554..81037d1013 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -39,4 +39,5 @@ opentelemetry-semantic-conventions = "0.12.0" opentelemetry-http = "0.9.0" http = "0.2.9" tonic = "0.10.2" +tonic-build = { version = "0.10.2", features = ["prost"] } prost = "0.12" diff --git a/core/notifications/BUCK b/core/notifications/BUCK index 53d5b054b7..9da2d88953 100644 --- a/core/notifications/BUCK +++ b/core/notifications/BUCK @@ -1,5 +1,5 @@ load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file") -load("@toolchains//rust:macros.bzl", "rustfmt_check", "clippy_check") +load("@toolchains//rust:macros.bzl", "rust_protobuf_library", "rustfmt_check", "clippy_check") sdl( name = "sdl", @@ -30,6 +30,7 @@ rust_binary( ], ) + rust_binary( name = "notifications", edition = "2021", @@ -41,9 +42,12 @@ rust_binary( ], ) -rust_library( +rust_protobuf_library( name = "lib-notifications", - edition = "2021", + env = { + "CARGO_MANIFEST_DIR": ".", + "SQLX_OFFLINE": "true", + }, deps = [ "//lib/tracing-rs:tracing", "//lib/es-entity-rs:es-entity", @@ -66,9 +70,7 @@ rust_library( "//third-party/rust:rand", "//third-party/rust:uuid", "//third-party/rust:serde_with", - "//third-party/rust:prost", "//third-party/rust:tonic", - "//third-party/rust:tonic-build", ], srcs = glob([ "src/**/*.rs", @@ -76,10 +78,8 @@ rust_library( "migrations/*", "notifications.yml" ]), - env = { - "CARGO_MANIFEST_DIR": ".", - "SQLX_OFFLINE": "true" - } + build_script = "build.rs", + protos = ["proto/notifications.proto"], ) rust_test( diff --git a/core/notifications/Cargo.toml b/core/notifications/Cargo.toml index f98cf9fe52..a48ef02bc3 100644 --- a/core/notifications/Cargo.toml +++ b/core/notifications/Cargo.toml @@ -41,4 +41,4 @@ tonic = { workspace = true } prost = { workspace = true } [build-dependencies] -tonic-build = { version = "0.10.2", features = ["prost"] } +tonic-build = { workspace = true } diff --git a/core/notifications/build.rs b/core/notifications/build.rs index 2b6043a5e7..cc300e0963 100644 --- a/core/notifications/build.rs +++ b/core/notifications/build.rs @@ -1,7 +1,19 @@ +use std::ffi::OsString; + +fn get_env(key: &str) -> Option { + println!("cargo:rerun-if-env-changed={}", key); + std::env::var_os(key) +} + fn main() -> Result<(), Box> { println!("cargo:rerun-if-changed=migrations"); - // std::env::set_var("PROTOC", protobuf_src::protoc()); - tonic_build::configure().compile(&["proto/notifications.proto"], &["proto"])?; + let mut tonic = tonic_build::configure(); + if get_env("OUT_DIR").is_none() { + if let Some(out) = get_env("OUT") { + tonic = tonic.out_dir(out); + } + } + tonic.compile(&["proto/notifications.proto"], &["proto"])?; Ok(()) } diff --git a/third-party/proto/BUCK b/third-party/proto/BUCK new file mode 100644 index 0000000000..14c88fc35d --- /dev/null +++ b/third-party/proto/BUCK @@ -0,0 +1,18 @@ +load(":defs.bzl", "protoc_distribution") + +protoc_distribution( + name = "distribution", + version = "21.4", +) + +alias( + name = "protoc", + actual = ":distribution[protoc]", + visibility = ["PUBLIC"], +) + +alias( + name = "google_protobuf", + actual = ":distribution[google_protobuf]", + visibility = ["PUBLIC"], +) diff --git a/third-party/proto/defs.bzl b/third-party/proto/defs.bzl new file mode 100644 index 0000000000..963b32974c --- /dev/null +++ b/third-party/proto/defs.bzl @@ -0,0 +1,104 @@ +load("@prelude//http_archive/exec_deps.bzl", "HttpArchiveExecDeps") +load(":releases.bzl", "releases") + +ProtocReleaseInfo = provider(fields = [ + "version", + "url", + "sha256", +]) + +def _get_protoc_release( + version: str, + platform: str) -> ProtocReleaseInfo: + if not version in releases: + fail("Unknown protoc release version '{}'. Available versions: {}".format( + version, + ", ".join(releases.keys()), + )) + protoc_version = releases[version] + artifact = "protoc-{}-{}.zip".format(version, platform) + if not artifact in protoc_version: + fail("Unsupported platform '{}'. Available artifacts: {}".format( + platform, + ", ".join(protoc_version.keys()), + )) + protoc_artifact = protoc_version[artifact] + return ProtocReleaseInfo( + version = version, + url = protoc_artifact["url"], + sha256 = protoc_artifact["sha256"], + ) + +def _turn_http_archive_into_protoc_distribution( + providers: ProviderCollection, + protoc_filename: str) -> list[Provider]: + downloads = providers[DefaultInfo].sub_targets + include = downloads["include"][DefaultInfo] + protoc = downloads[protoc_filename][DefaultInfo] + + return [DefaultInfo( + sub_targets = { + "google_protobuf": [include], + "protoc": [ + protoc, + RunInfo(args = protoc.default_outputs[0]), + ], + }, + )] + +def _download_protoc_distribution_impl(ctx: AnalysisContext) -> Promise: + protoc_filename = "bin/protoc" + ctx.attrs.exe_extension + + return ctx.actions.anon_target(native.http_archive, { + "exec_deps": ctx.attrs._http_archive_exec_deps, + "sha256": ctx.attrs.sha256, + "sub_targets": [ + protoc_filename, + "include", + ], + "urls": [ctx.attrs.url], + }).promise.map(lambda providers: _turn_http_archive_into_protoc_distribution( + providers = providers, + protoc_filename = protoc_filename, + )) + +download_protoc_distribution = rule( + impl = _download_protoc_distribution_impl, + attrs = { + "exe_extension": attrs.string(), + "sha256": attrs.string(), + "url": attrs.string(), + "_http_archive_exec_deps": attrs.default_only(attrs.exec_dep(providers = [HttpArchiveExecDeps], default = "prelude//http_archive/tools:exec_deps")), + }, +) + +def _host_platform(): + os = host_info().os + arch = host_info().arch + if os.is_linux and arch.is_x86_64: + return "linux-x86_64" + elif os.is_linux and arch.is_aarch64: + return "linux-aarch_64" + elif os.is_macos and arch.is_x86_64: + return "osx-x86_64" + elif os.is_macos and arch.is_aarch64: + return "osx-aarch_64" + elif os.is_windows and arch.is_x86_64: + return "win64" + else: + fail("Unknown platform: os={}, arch={}".format(os, arch)) + +def protoc_distribution( + name: str, + version: str, + platform: [None, str] = None): + if platform == None: + platform = _host_platform() + exe_extension = ".exe" if platform.startswith("win") else "" + release = _get_protoc_release(version, platform) + download_protoc_distribution( + name = name, + url = release.url, + sha256 = release.sha256, + exe_extension = exe_extension, + ) diff --git a/third-party/proto/releases.bzl b/third-party/proto/releases.bzl new file mode 100644 index 0000000000..153ca496e4 --- /dev/null +++ b/third-party/proto/releases.bzl @@ -0,0 +1,46 @@ +# @generated +# Update with ./update.py > releases.bzl +releases = { + "21.4": { + "protoc-21.4-win32.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-win32.zip", + "sha256": "09760ff98f76ac30c8bca7433715c47161fe305bf41573a7cd00b0afcc518617" + }, + "protoc-21.4-osx-x86_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-osx-x86_64.zip", + "sha256": "27ac01aee3e8b95ebec017b7b3aee55d4eb095cbd2a5148d2a20350af006072e" + }, + "protoc-21.4-osx-universal_binary.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-osx-universal_binary.zip", + "sha256": "0cf1a340d1fa1366cdf22043e3f92e5374096647037020b405acea706438c603" + }, + "protoc-21.4-osx-aarch_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-osx-aarch_64.zip", + "sha256": "6a677c88a5e5b032aaff96767461788a316408d4ed0afef3f1455390a689ec18" + }, + "protoc-21.4-linux-x86_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-x86_64.zip", + "sha256": "d51e8f030162f08823a4738ab0ac00bee537e30b583a562e6962dbb040d86736" + }, + "protoc-21.4-linux-x86_32.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-x86_32.zip", + "sha256": "2101e3a4dd490625c15d31274a599e3bfb1f8c9fdd381ca9501da17bb6f7a3d2" + }, + "protoc-21.4-linux-s390_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-s390_64.zip", + "sha256": "e0b69b4242bf409c825d787669a4683d63320d6e52280750df971a3376e0af56" + }, + "protoc-21.4-linux-ppcle_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-ppcle_64.zip", + "sha256": "58c1be8ca89b8b2712f95a9dd9aeb174f8f479be9f55e71f6ffe0b0e83ef8be9" + }, + "protoc-21.4-linux-aarch_64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-linux-aarch_64.zip", + "sha256": "5a377b505cf8c3ed29ad0b6e3827c5eb27273c00147fcfd833b9686192143e8d" + }, + "protoc-21.4-win64.zip": { + "url": "https://github.com/protocolbuffers/protobuf/releases/download/v21.4/protoc-21.4-win64.zip", + "sha256": "090af381392abaf5fd8ae3070d8fc2a4ba2d0a9f8e52915d69b439c33be72da5" + } + } +} diff --git a/third-party/proto/update.py b/third-party/proto/update.py new file mode 100755 index 0000000000..cfee489f67 --- /dev/null +++ b/third-party/proto/update.py @@ -0,0 +1,99 @@ +#!/usr/bin/env python3 +"""Query recent Github release artifacts for protobuf. + +Use this script to update the releases.bzl file that contains metadata about +protoc releases. + +This script is not executed during the regular Buck2 build. +""" + +import aiohttp +import asyncio +from contextlib import asynccontextmanager +from copy import deepcopy +from gql import gql, Client +from gql.transport.aiohttp import AIOHTTPTransport +import hashlib +import json +import os + +GITHUB_GRAPHQL_URI = "https://api.github.com/graphql" +GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN") +GITHUB_QUERY = """\ +query { + repository(owner: "protocolbuffers", name: "protobuf") { + releases(last: 1) { + nodes { + tagName + releaseAssets(first: 100) { + nodes { + name + downloadUrl + } + } + } + } + } +} +""" + +async def query_releases(): + async with aiohttp.ClientSession(raise_for_status=True) as session: + assert GITHUB_TOKEN is not None, "Provide a Github API token in $GITHUB_TOKEN" + headers = {'Authorization': f'bearer {GITHUB_TOKEN}'} + body = {"query": GITHUB_QUERY} + async with session.post(GITHUB_GRAPHQL_URI, headers=headers, json=body) as resp: + response = await resp.json() + return response["data"] + + +def format_releases(releases): + return { + release["tagName"].strip("v"): { + asset["name"]: { + "url": asset["downloadUrl"], + } + for asset in release["releaseAssets"]["nodes"] + if asset["name"].startswith("protoc-") + } + for release in releases["repository"]["releases"]["nodes"] + } + + +async def fetch_sha256(session, url): + async with session.get(url) as resp: + hasher = hashlib.sha256() + async for chunk, _ in resp.content.iter_chunks(): + hasher.update(chunk) + return hasher.hexdigest() + + +async def hash_releases(releases): + async def hash_asset(session, version, name, url): + sha256 = await fetch_sha256(session, url) + return (version, name, sha256) + + tasks = [] + async with aiohttp.ClientSession() as session: + for version, assets in releases.items(): + for name, asset in assets.items(): + tasks.append(hash_asset(session, version, name, asset["url"])) + + result = deepcopy(releases) + hashes = await asyncio.gather(*tasks) + for version, name, sha256 in hashes: + result[version][name]["sha256"] = sha256 + + return result + + +async def main(): + releases = await query_releases() + formatted = format_releases(releases) + with_sha256 = await hash_releases(formatted) + print("# @" + "generated") + print("# Update with ./update.py > releases.bzl") + print("releases = ", json.dumps(with_sha256, indent=4)) + + +asyncio.run(main()) diff --git a/third-party/rust/BUCK b/third-party/rust/BUCK index 1f57072f46..c2ad022473 100644 --- a/third-party/rust/BUCK +++ b/third-party/rust/BUCK @@ -3281,11 +3281,7 @@ cargo.rust_library( crate = "itertools", crate_root = "itertools-0.10.5.crate/src/lib.rs", edition = "2018", - features = [ - "default", - "use_alloc", - "use_std", - ], + features = ["use_alloc"], visibility = [], deps = [":either-1.9.0"], ) @@ -5104,38 +5100,6 @@ buildscript_run( version = "1.0.76", ) -alias( - name = "prost", - actual = ":prost-0.10.4", - visibility = ["PUBLIC"], -) - -http_archive( - name = "prost-0.10.4.crate", - sha256 = "71adf41db68aa0daaefc69bb30bcd68ded9b9abaad5d1fbb6304c4fb390e083e", - strip_prefix = "prost-0.10.4", - urls = ["https://crates.io/api/v1/crates/prost/0.10.4/download"], - visibility = [], -) - -cargo.rust_library( - name = "prost-0.10.4", - srcs = [":prost-0.10.4.crate"], - crate = "prost", - crate_root = "prost-0.10.4.crate/src/lib.rs", - edition = "2018", - features = [ - "default", - "prost-derive", - "std", - ], - visibility = [], - deps = [ - ":bytes-1.5.0", - ":prost-derive-0.10.1", - ], -) - http_archive( name = "prost-0.11.9.crate", sha256 = "0b82eaa1d779e9a4bc1c3217db8ffbeabaae1dca241bf70183242128d48681cd", @@ -5162,6 +5126,12 @@ cargo.rust_library( ], ) +alias( + name = "prost", + actual = ":prost-0.12.3", + visibility = ["PUBLIC"], +) + http_archive( name = "prost-0.12.3.crate", sha256 = "146c289cda302b98a28d40c8b3b90498d6e526dd24ac2ecea73e4e491685b94a", @@ -5177,6 +5147,7 @@ cargo.rust_library( crate_root = "prost-0.12.3.crate/src/lib.rs", edition = "2021", features = [ + "default", "prost-derive", "std", ], @@ -5226,31 +5197,6 @@ cargo.rust_library( ], ) -http_archive( - name = "prost-derive-0.10.1.crate", - sha256 = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc", - strip_prefix = "prost-derive-0.10.1", - urls = ["https://crates.io/api/v1/crates/prost-derive/0.10.1/download"], - visibility = [], -) - -cargo.rust_library( - name = "prost-derive-0.10.1", - srcs = [":prost-derive-0.10.1.crate"], - crate = "prost_derive", - crate_root = "prost-derive-0.10.1.crate/src/lib.rs", - edition = "2018", - proc_macro = True, - visibility = [], - deps = [ - ":anyhow-1.0.79", - ":itertools-0.10.5", - ":proc-macro2-1.0.76", - ":quote-1.0.35", - ":syn-1.0.109", - ], -) - http_archive( name = "prost-derive-0.11.9.crate", sha256 = "e5d2d8d10f3c6ded6da8b05b5fb3b8a5082514344d56c9f871412d29b4e075b4", @@ -8326,7 +8272,7 @@ cargo.rust_binary( ":opentelemetry-http-0.9.0", ":opentelemetry-otlp-0.13.0", ":opentelemetry-semantic-conventions-0.12.0", - ":prost-0.10.4", + ":prost-0.12.3", ":rand-0.8.5", ":reqwest-0.11.23", ":serde-1.0.195", diff --git a/third-party/rust/Cargo.lock b/third-party/rust/Cargo.lock index 540c14ad9f..aa06ebe665 100644 --- a/third-party/rust/Cargo.lock +++ b/third-party/rust/Cargo.lock @@ -2024,16 +2024,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "prost" -version = "0.10.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71adf41db68aa0daaefc69bb30bcd68ded9b9abaad5d1fbb6304c4fb390e083e" -dependencies = [ - "bytes", - "prost-derive 0.10.1", -] - [[package]] name = "prost" version = "0.11.9" @@ -2076,19 +2066,6 @@ dependencies = [ "which", ] -[[package]] -name = "prost-derive" -version = "0.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b670f45da57fb8542ebdbb6105a925fe571b67f9e7ed9f47a06a84e72b4e7cc" -dependencies = [ - "anyhow", - "itertools 0.10.5", - "proc-macro2", - "quote", - "syn 1.0.109", -] - [[package]] name = "prost-derive" version = "0.11.9" @@ -3059,7 +3036,7 @@ dependencies = [ "opentelemetry-http", "opentelemetry-otlp", "opentelemetry-semantic-conventions", - "prost 0.10.4", + "prost 0.12.3", "rand", "reqwest", "serde", diff --git a/third-party/rust/Cargo.toml b/third-party/rust/Cargo.toml index 3ead22e324..441d8971f4 100644 --- a/third-party/rust/Cargo.toml +++ b/third-party/rust/Cargo.toml @@ -47,7 +47,7 @@ opentelemetry-semantic-conventions = "0.12.0" opentelemetry-http = "0.9.0" http = "0.2.9" -prost = "0.10.2" +prost = "0.12" tonic = "0.10.2" tonic-build = { version = "0.10.2", features = ["prost"] } diff --git a/toolchains/BUCK b/toolchains/BUCK index 479b0a787f..743bc46eac 100644 --- a/toolchains/BUCK +++ b/toolchains/BUCK @@ -3,6 +3,7 @@ load( "system_python_bootstrap_toolchain", "system_python_toolchain" ) +load("@prelude//toolchains:genrule.bzl", "system_genrule_toolchain") load("@prelude//toolchains:cxx.bzl", "system_cxx_toolchain") load("@prelude//toolchains:rust.bzl", "system_rust_toolchain") @@ -21,6 +22,11 @@ system_cxx_toolchain( visibility = ["PUBLIC"], ) +system_genrule_toolchain( + name = "genrule", + visibility = ["PUBLIC"], +) + system_rust_toolchain( name = "rust", default_edition = "2021", diff --git a/toolchains/rust/macros.bzl b/toolchains/rust/macros.bzl index 07a1dcde38..83d9908803 100644 --- a/toolchains/rust/macros.bzl +++ b/toolchains/rust/macros.bzl @@ -14,6 +14,70 @@ load( "@prelude//tests:re_utils.bzl", "get_re_executor_from_props", ) +load( + "@prelude//decls/core_rules.bzl", + "genrule", +) + +def rust_protobuf_library( + name, + srcs, + build_script, + protos, + edition = "2021", + env = None, + build_env = None, + deps = [], + test_deps = None, + doctests = True): + + build_name = name + "-build" + proto_name = name + "-proto" + + native.rust_binary( + name = build_name, + srcs = [build_script], + crate_root = build_script, + deps = [ + "//third-party/rust:tonic-build" + ], + ) + + build_env = build_env or {} + build_env.update( + { + "PROTOC": "$(exe //third-party/proto:protoc)", + "PROTOC_INCLUDE": "$(location //third-party/proto:google_protobuf)", + }, + ) + + + native.genrule( + name = proto_name, + srcs = protos + [ + "//third-party/proto:google_protobuf", + ], + out = ".", + cmd = "$(exe :" + build_name + ")", + env = build_env, + ) + + env = env or {} + env.update({ + "OUT_DIR": "$(location :{})".format(proto_name), + }) + native.rust_library( + name = name, + env = env, + srcs = srcs, + edition = edition, + doctests = doctests, + deps = [ + "//third-party/rust:prost" + ] + (deps or []) + ) + + def clippy_check_impl(ctx: AnalysisContext) -> list[[ DefaultInfo, diff --git a/vendir.lock.yml b/vendir.lock.yml index 7973dcb86d..2c81a60733 100644 --- a/vendir.lock.yml +++ b/vendir.lock.yml @@ -21,7 +21,15 @@ directories: commitTitle: 'client: Fix "ctrl-c was pressed" bug...' sha: f3c685667ef22d0130687003012b6960abec6b3b tags: - - latest + - 2023-10-15-425-gf3c685667 path: . path: third-party/rust/fixups/ring/include +- contents: + - git: + commitTitle: 'client: Fix "ctrl-c was pressed" bug...' + sha: f3c685667ef22d0130687003012b6960abec6b3b + tags: + - 2023-10-15-425-gf3c685667 + path: . + path: third-party/proto kind: LockConfig diff --git a/vendir.yml b/vendir.yml index 9617768645..5ad30ffe33 100644 --- a/vendir.yml +++ b/vendir.yml @@ -28,3 +28,12 @@ directories: includePaths: - shim/third-party/rust/fixups/ring/include/**/* newRootPath: shim/third-party/rust/fixups/ring/include +- path: third-party/proto + contents: + - path: . + git: + url: https://github.com/facebook/buck2.git + ref: f3c685667ef22d0130687003012b6960abec6b3b + includePaths: + - shim/third-party/proto/**/* + newRootPath: shim/third-party/proto diff --git a/vendir/template.yml b/vendir/template.yml index 86da12df26..74e37674f8 100644 --- a/vendir/template.yml +++ b/vendir/template.yml @@ -31,3 +31,12 @@ directories: includePaths: - shim/third-party/rust/fixups/ring/include/**/* newRootPath: shim/third-party/rust/fixups/ring/include + - path: third-party/proto + contents: + - path: . + git: + url: https://github.com/facebook/buck2.git + ref: #@ data.values.ring_fixup_git_ref + includePaths: + - shim/third-party/proto/**/* + newRootPath: shim/third-party/proto From b3c03f372b4426fa1d821c59621c721bed2aab96 Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Fri, 12 Jan 2024 16:37:37 +0100 Subject: [PATCH 3/8] chore(toolchain): remove redundant load --- toolchains/rust/macros.bzl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/toolchains/rust/macros.bzl b/toolchains/rust/macros.bzl index 83d9908803..20a6c6b14e 100644 --- a/toolchains/rust/macros.bzl +++ b/toolchains/rust/macros.bzl @@ -14,10 +14,6 @@ load( "@prelude//tests:re_utils.bzl", "get_re_executor_from_props", ) -load( - "@prelude//decls/core_rules.bzl", - "genrule", -) def rust_protobuf_library( name, From a83ca78411f41447488b72ad685ffc424aa8c000 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Sun, 14 Jan 2024 21:39:42 +0530 Subject: [PATCH 4/8] feat(notifications): add grpc server --- Cargo.lock | 50 ++- Cargo.toml | 1 + core/notifications/BUCK | 1 + core/notifications/Cargo.toml | 1 + core/notifications/proto/notifications.proto | 31 +- core/notifications/src/app/error.rs | 2 + core/notifications/src/app/mod.rs | 14 + core/notifications/src/cli/config.rs | 6 +- core/notifications/src/cli/mod.rs | 33 +- core/notifications/src/grpc/config.rs | 19 + core/notifications/src/grpc/mod.rs | 12 + core/notifications/src/grpc/server/convert.rs | 29 ++ core/notifications/src/grpc/server/mod.rs | 62 ++++ core/notifications/src/lib.rs | 6 +- .../src/user_notification_settings/entity.rs | 44 +++ third-party/rust/BUCK | 339 ++++++++++-------- third-party/rust/Cargo.lock | 50 ++- third-party/rust/Cargo.toml | 1 + 18 files changed, 494 insertions(+), 207 deletions(-) create mode 100644 core/notifications/src/grpc/config.rs create mode 100644 core/notifications/src/grpc/mod.rs create mode 100644 core/notifications/src/grpc/server/convert.rs create mode 100644 core/notifications/src/grpc/server/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 1835ed3a49..60d1b69a81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -963,9 +963,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" dependencies = [ "futures-channel", "futures-core", @@ -978,9 +978,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", "futures-sink", @@ -988,15 +988,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" [[package]] name = "futures-executor" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" dependencies = [ "futures-core", "futures-task", @@ -1016,15 +1016,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" [[package]] name = "futures-macro" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", @@ -1033,21 +1033,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" [[package]] name = "futures-task" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" [[package]] name = "futures-util" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-channel", "futures-core", @@ -1690,6 +1690,7 @@ dependencies = [ "tokio", "tonic 0.10.2", "tonic-build", + "tonic-health", "tracing 0.1.0", "uuid", ] @@ -3332,6 +3333,19 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "tonic-health" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f80db390246dfb46553481f6024f0082ba00178ea495dbb99e70ba9a4fafb5e1" +dependencies = [ + "async-stream", + "prost 0.12.3", + "tokio", + "tokio-stream", + "tonic 0.10.2", +] + [[package]] name = "tower" version = "0.4.13" diff --git a/Cargo.toml b/Cargo.toml index 81037d1013..19338101f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -40,4 +40,5 @@ opentelemetry-http = "0.9.0" http = "0.2.9" tonic = "0.10.2" tonic-build = { version = "0.10.2", features = ["prost"] } +tonic-health = "0.10.2" prost = "0.12" diff --git a/core/notifications/BUCK b/core/notifications/BUCK index 9da2d88953..fb913fd64b 100644 --- a/core/notifications/BUCK +++ b/core/notifications/BUCK @@ -71,6 +71,7 @@ rust_protobuf_library( "//third-party/rust:uuid", "//third-party/rust:serde_with", "//third-party/rust:tonic", + "//third-party/rust:tonic-health", ], srcs = glob([ "src/**/*.rs", diff --git a/core/notifications/Cargo.toml b/core/notifications/Cargo.toml index a48ef02bc3..c145cadd29 100644 --- a/core/notifications/Cargo.toml +++ b/core/notifications/Cargo.toml @@ -38,6 +38,7 @@ rand = { workspace = true } uuid = { workspace = true } serde_with = { workspace = true } tonic = { workspace = true } +tonic-health = { workspace = true } prost = { workspace = true } [build-dependencies] diff --git a/core/notifications/proto/notifications.proto b/core/notifications/proto/notifications.proto index 4f49dc208e..e31d548d18 100644 --- a/core/notifications/proto/notifications.proto +++ b/core/notifications/proto/notifications.proto @@ -1,20 +1,29 @@ syntax = "proto3"; -package helloworld; +import "google/protobuf/struct.proto"; -// The greeting service definition. -service Greeter { - // Sends a greeting - rpc SayHello (HelloRequest) returns (HelloReply) {} +package services.notifications.v1; + +service NotificationsService { + rpc ShouldSendNotification (ShouldSendNotificationRequest) returns (ShouldSendNotificationResponse) {} +} + +enum NotificationChannel { + PUSH = 0; } -// The request message containing the user's name. -message HelloRequest { - string name = 1; +enum NotificationCategory { + CIRCLES = 0; + PAYMENTS = 1; } -// The response message containing the greetings. -message HelloReply { - string message = 1; +message ShouldSendNotificationRequest { + string user_id = 1; + NotificationChannel channel = 2; + NotificationCategory category = 3; } +message ShouldSendNotificationResponse { + string user_id = 1; + bool should_send = 2; +} diff --git a/core/notifications/src/app/error.rs b/core/notifications/src/app/error.rs index 45097a2936..3de1a76b11 100644 --- a/core/notifications/src/app/error.rs +++ b/core/notifications/src/app/error.rs @@ -6,4 +6,6 @@ use crate::user_notification_settings::error::*; pub enum ApplicationError { #[error("{0}")] UserNotificationSettingsError(#[from] UserNotificationSettingsError), + #[error("{0}")] + GrpcServerError(#[from] tonic::transport::Error), } diff --git a/core/notifications/src/app/mod.rs b/core/notifications/src/app/mod.rs index 7ef7880590..81f16af7e3 100644 --- a/core/notifications/src/app/mod.rs +++ b/core/notifications/src/app/mod.rs @@ -25,6 +25,20 @@ impl NotificationsApp { } } + pub async fn should_send_notification( + &self, + user_id: GaloyUserId, + channel: UserNotificationChannel, + category: UserNotificationCategory, + ) -> Result { + let user_settings = self + .settings + .find_for_user_id(&user_id) + .await? + .unwrap_or_else(|| UserNotificationSettings::new(user_id.clone())); + Ok(user_settings.should_send_notification(channel, category)) + } + pub async fn notification_settings_for_user( &self, user_id: GaloyUserId, diff --git a/core/notifications/src/cli/config.rs b/core/notifications/src/cli/config.rs index 91ffaf2ce6..06b597f6a3 100644 --- a/core/notifications/src/cli/config.rs +++ b/core/notifications/src/cli/config.rs @@ -5,7 +5,7 @@ use tracing::TracingConfig; use std::path::Path; use super::db::*; -use crate::{app::AppConfig, graphql::server::ServerConfig}; +use crate::{app::AppConfig, graphql::server::ServerConfig, grpc::GrpcServerConfig}; #[derive(Clone, Default, Serialize, Deserialize)] pub struct Config { @@ -15,13 +15,15 @@ pub struct Config { pub app: AppConfig, #[serde(default)] pub server: ServerConfig, + #[serde(default)] + pub grpc_server: GrpcServerConfig, #[serde(default = "default_tracing_config")] pub tracing: TracingConfig, } fn default_tracing_config() -> TracingConfig { TracingConfig { - service_name: "api-keys".to_string(), + service_name: "notifications".to_string(), } } diff --git a/core/notifications/src/cli/mod.rs b/core/notifications/src/cli/mod.rs index fa162b1989..b539cba32d 100644 --- a/core/notifications/src/cli/mod.rs +++ b/core/notifications/src/cli/mod.rs @@ -1,6 +1,7 @@ pub mod config; mod db; +use anyhow::Context; use clap::Parser; use std::path::PathBuf; @@ -33,8 +34,38 @@ pub async fn run() -> anyhow::Result<()> { async fn run_cmd(config: Config) -> anyhow::Result<()> { tracing::init_tracer(config.tracing)?; + let (send, mut receive) = tokio::sync::mpsc::channel(1); + let mut handles = vec![]; let pool = db::init_pool(&config.db).await?; let app = crate::app::NotificationsApp::new(pool, config.app); + println!("Starting notifications graphql server"); - crate::graphql::server::run_server(config.server, app).await + let graphql_send = send.clone(); + let graphql_config = config.server; + let graphql_app = app.clone(); + handles.push(tokio::spawn(async move { + let _ = graphql_send.try_send( + crate::graphql::server::run_server(graphql_config, graphql_app) + .await + .context("graphql server error"), + ); + })); + + println!("Starting notifications grpc server"); + let grpc_send = send.clone(); + let grpc_config = config.grpc_server; + handles.push(tokio::spawn(async move { + let _ = grpc_send.try_send( + crate::grpc::run_server(grpc_config, app) + .await + .context("grpc server error"), + ); + })); + + let reason = receive.recv().await.expect("Didn't receive msg"); + for handle in handles { + handle.abort(); + } + + reason } diff --git a/core/notifications/src/grpc/config.rs b/core/notifications/src/grpc/config.rs new file mode 100644 index 0000000000..61a6bf1b9d --- /dev/null +++ b/core/notifications/src/grpc/config.rs @@ -0,0 +1,19 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub struct GrpcServerConfig { + #[serde(default = "default_port")] + pub listen_port: u16, +} + +impl Default for GrpcServerConfig { + fn default() -> Self { + Self { + listen_port: default_port(), + } + } +} + +fn default_port() -> u16 { + 2478 +} diff --git a/core/notifications/src/grpc/mod.rs b/core/notifications/src/grpc/mod.rs new file mode 100644 index 0000000000..c9a1db5bd8 --- /dev/null +++ b/core/notifications/src/grpc/mod.rs @@ -0,0 +1,12 @@ +mod config; +mod server; + +use crate::app::*; + +pub use config::*; +pub use server::*; + +pub async fn run_server(config: GrpcServerConfig, app: NotificationsApp) -> anyhow::Result<()> { + server::start(config, app).await?; + Ok(()) +} diff --git a/core/notifications/src/grpc/server/convert.rs b/core/notifications/src/grpc/server/convert.rs new file mode 100644 index 0000000000..557bee9660 --- /dev/null +++ b/core/notifications/src/grpc/server/convert.rs @@ -0,0 +1,29 @@ +use crate::app::ApplicationError; +use crate::primitives::{UserNotificationCategory, UserNotificationChannel}; + +impl From for UserNotificationCategory { + fn from(category: i32) -> Self { + match category { + 0 => UserNotificationCategory::Circles, + 1 => UserNotificationCategory::Payments, + _ => unimplemented!(), + } + } +} + +impl From for UserNotificationChannel { + fn from(channel: i32) -> Self { + match channel { + 0 => UserNotificationChannel::Push, + _ => unimplemented!(), + } + } +} + +impl From for tonic::Status { + fn from(err: ApplicationError) -> Self { + match err { + _ => tonic::Status::internal(err.to_string()), + } + } +} diff --git a/core/notifications/src/grpc/server/mod.rs b/core/notifications/src/grpc/server/mod.rs new file mode 100644 index 0000000000..bd58414b1f --- /dev/null +++ b/core/notifications/src/grpc/server/mod.rs @@ -0,0 +1,62 @@ +mod convert; + +#[allow(clippy::all)] +pub mod proto { + tonic::include_proto!("services.notifications.v1"); +} + +use tonic::{transport::Server, Request, Response, Status}; + +use self::proto::{notifications_service_server::NotificationsService, *}; + +use super::config::*; +use crate::app::{ApplicationError, *}; + +pub struct Notifications { + app: NotificationsApp, +} + +#[tonic::async_trait] +impl NotificationsService for Notifications { + async fn should_send_notification( + &self, + request: Request, + ) -> Result, Status> { + let request = request.into_inner(); + let ShouldSendNotificationRequest { + user_id, + channel, + category, + } = request; + + let should_send = self + .app + .should_send_notification(user_id.clone().into(), channel.into(), category.into()) + .await?; + + Ok(Response::new(ShouldSendNotificationResponse { + user_id, + should_send, + })) + } +} + +pub(crate) async fn start( + server_config: GrpcServerConfig, + app: NotificationsApp, +) -> Result<(), ApplicationError> { + use proto::notifications_service_server::NotificationsServiceServer; + + let notifications = Notifications { app }; + println!("Starting grpc server on port {}", server_config.listen_port); + let (mut health_reporter, health_service) = tonic_health::server::health_reporter(); + health_reporter + .set_serving::>() + .await; + Server::builder() + .add_service(health_service) + .add_service(NotificationsServiceServer::new(notifications)) + .serve(([0, 0, 0, 0], server_config.listen_port).into()) + .await?; + Ok(()) +} diff --git a/core/notifications/src/lib.rs b/core/notifications/src/lib.rs index 550168f62b..72a16ca57f 100644 --- a/core/notifications/src/lib.rs +++ b/core/notifications/src/lib.rs @@ -8,8 +8,4 @@ mod user_notification_settings; pub mod cli; pub mod graphql; - -#[allow(clippy::all)] -pub mod proto { - tonic::include_proto!("helloworld"); -} +pub mod grpc; diff --git a/core/notifications/src/user_notification_settings/entity.rs b/core/notifications/src/user_notification_settings/entity.rs index 874a43d7d2..cd7098bcc9 100644 --- a/core/notifications/src/user_notification_settings/entity.rs +++ b/core/notifications/src/user_notification_settings/entity.rs @@ -129,6 +129,15 @@ impl UserNotificationSettings { acc }) } + + pub fn should_send_notification( + &self, + channel: UserNotificationChannel, + category: UserNotificationCategory, + ) -> bool { + self.is_channel_enabled(channel) + && !self.disabled_categories_for(channel).contains(&category) + } } impl TryFrom> for UserNotificationSettings { @@ -230,4 +239,39 @@ mod tests { HashSet::from([UserNotificationCategory::Payments]) ); } + + #[test] + fn should_send_notification() { + let events = initial_events(); + let settings = UserNotificationSettings::try_from(events).expect("Could not hydrate"); + assert!(settings.should_send_notification( + UserNotificationChannel::Push, + UserNotificationCategory::Circles + )); + } + + #[test] + fn should_not_send_notification_if_category_is_disabled() { + let events = initial_events(); + let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate"); + settings.disable_category( + UserNotificationChannel::Push, + UserNotificationCategory::Payments, + ); + assert!(!settings.should_send_notification( + UserNotificationChannel::Push, + UserNotificationCategory::Payments, + )); + } + + #[test] + fn should_not_send_notification_if_channel_is_disabled() { + let events = initial_events(); + let mut settings = UserNotificationSettings::try_from(events).expect("Could not hydrate"); + settings.disable_channel(UserNotificationChannel::Push); + assert!(!settings.should_send_notification( + UserNotificationChannel::Push, + UserNotificationCategory::Payments, + )); + } } diff --git a/third-party/rust/BUCK b/third-party/rust/BUCK index c2ad022473..a466ff3be4 100644 --- a/third-party/rust/BUCK +++ b/third-party/rust/BUCK @@ -319,7 +319,7 @@ cargo.rust_library( ":bytes-1.5.0", ":chrono-0.4.31", ":fnv-1.0.7", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":http-0.2.9", ":indexmap-2.0.2", ":mime-0.3.17", @@ -363,7 +363,7 @@ cargo.rust_library( ":async-trait-0.1.74", ":axum-0.6.20", ":bytes-1.5.0", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":serde_json-1.0.108", ":tokio-1.33.0", ":tokio-stream-0.1.14", @@ -464,7 +464,7 @@ cargo.rust_library( visibility = [], deps = [ ":async-stream-impl-0.3.5", - ":futures-core-0.3.29", + ":futures-core-0.3.30", ":pin-project-lite-0.2.13", ], ) @@ -596,7 +596,7 @@ cargo.rust_library( ":base64-0.21.5", ":bitflags-1.3.2", ":bytes-1.5.0", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":headers-0.3.9", ":http-0.2.9", ":http-body-0.4.5", @@ -639,7 +639,7 @@ cargo.rust_library( deps = [ ":async-trait-0.1.74", ":bytes-1.5.0", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":http-0.2.9", ":http-body-0.4.5", ":mime-0.3.17", @@ -2149,8 +2149,8 @@ cargo.rust_library( }, visibility = [], deps = [ - ":futures-core-0.3.29", - ":futures-sink-0.3.29", + ":futures-core-0.3.30", + ":futures-sink-0.3.30", ], ) @@ -2216,18 +2216,18 @@ cargo.rust_library( ) http_archive( - name = "futures-0.3.29.crate", - sha256 = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335", - strip_prefix = "futures-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures/0.3.29/download"], + name = "futures-0.3.30.crate", + sha256 = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0", + strip_prefix = "futures-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-0.3.29", - srcs = [":futures-0.3.29.crate"], + name = "futures-0.3.30", + srcs = [":futures-0.3.30.crate"], crate = "futures", - crate_root = "futures-0.3.29.crate/src/lib.rs", + crate_root = "futures-0.3.30.crate/src/lib.rs", edition = "2018", features = [ "alloc", @@ -2239,40 +2239,40 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-executor-0.3.29", - ":futures-io-0.3.29", - ":futures-sink-0.3.29", - ":futures-task-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-executor-0.3.30", + ":futures-io-0.3.30", + ":futures-sink-0.3.30", + ":futures-task-0.3.30", + ":futures-util-0.3.30", ], ) http_archive( - name = "futures-channel-0.3.29.crate", - sha256 = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb", - strip_prefix = "futures-channel-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-channel/0.3.29/download"], + name = "futures-channel-0.3.30.crate", + sha256 = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78", + strip_prefix = "futures-channel-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-channel/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-channel-0.3.29", - srcs = [":futures-channel-0.3.29.crate"], + name = "futures-channel-0.3.30", + srcs = [":futures-channel-0.3.30.crate"], crate = "futures_channel", - crate_root = "futures-channel-0.3.29.crate/src/lib.rs", + crate_root = "futures-channel-0.3.30.crate/src/lib.rs", edition = "2018", env = { - "CARGO_MANIFEST_DIR": "futures-channel-0.3.29.crate", + "CARGO_MANIFEST_DIR": "futures-channel-0.3.30.crate", "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "Channels for asynchronous communication using futures-rs.\n", "CARGO_PKG_NAME": "futures-channel", "CARGO_PKG_REPOSITORY": "https://github.com/rust-lang/futures-rs", - "CARGO_PKG_VERSION": "0.3.29", + "CARGO_PKG_VERSION": "0.3.30", "CARGO_PKG_VERSION_MAJOR": "0", "CARGO_PKG_VERSION_MINOR": "3", - "CARGO_PKG_VERSION_PATCH": "29", + "CARGO_PKG_VERSION_PATCH": "30", }, features = [ "alloc", @@ -2283,35 +2283,35 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-core-0.3.29", - ":futures-sink-0.3.29", + ":futures-core-0.3.30", + ":futures-sink-0.3.30", ], ) http_archive( - name = "futures-core-0.3.29.crate", - sha256 = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c", - strip_prefix = "futures-core-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-core/0.3.29/download"], + name = "futures-core-0.3.30.crate", + sha256 = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d", + strip_prefix = "futures-core-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-core/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-core-0.3.29", - srcs = [":futures-core-0.3.29.crate"], + name = "futures-core-0.3.30", + srcs = [":futures-core-0.3.30.crate"], crate = "futures_core", - crate_root = "futures-core-0.3.29.crate/src/lib.rs", + crate_root = "futures-core-0.3.30.crate/src/lib.rs", edition = "2018", env = { - "CARGO_MANIFEST_DIR": "futures-core-0.3.29.crate", + "CARGO_MANIFEST_DIR": "futures-core-0.3.30.crate", "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "The core traits and types in for the `futures` library.\n", "CARGO_PKG_NAME": "futures-core", "CARGO_PKG_REPOSITORY": "https://github.com/rust-lang/futures-rs", - "CARGO_PKG_VERSION": "0.3.29", + "CARGO_PKG_VERSION": "0.3.30", "CARGO_PKG_VERSION_MAJOR": "0", "CARGO_PKG_VERSION_MINOR": "3", - "CARGO_PKG_VERSION_PATCH": "29", + "CARGO_PKG_VERSION_PATCH": "30", }, features = [ "alloc", @@ -2322,18 +2322,18 @@ cargo.rust_library( ) http_archive( - name = "futures-executor-0.3.29.crate", - sha256 = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc", - strip_prefix = "futures-executor-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-executor/0.3.29/download"], + name = "futures-executor-0.3.30.crate", + sha256 = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d", + strip_prefix = "futures-executor-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-executor/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-executor-0.3.29", - srcs = [":futures-executor-0.3.29.crate"], + name = "futures-executor-0.3.30", + srcs = [":futures-executor-0.3.30.crate"], crate = "futures_executor", - crate_root = "futures-executor-0.3.29.crate/src/lib.rs", + crate_root = "futures-executor-0.3.30.crate/src/lib.rs", edition = "2018", features = [ "default", @@ -2341,9 +2341,9 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-core-0.3.29", - ":futures-task-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-task-0.3.30", + ":futures-util-0.3.30", ], ) @@ -2369,25 +2369,25 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-core-0.3.29", + ":futures-core-0.3.30", ":lock_api-0.4.11", ":parking_lot-0.12.1", ], ) http_archive( - name = "futures-io-0.3.29.crate", - sha256 = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa", - strip_prefix = "futures-io-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-io/0.3.29/download"], + name = "futures-io-0.3.30.crate", + sha256 = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1", + strip_prefix = "futures-io-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-io/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-io-0.3.29", - srcs = [":futures-io-0.3.29.crate"], + name = "futures-io-0.3.30", + srcs = [":futures-io-0.3.30.crate"], crate = "futures_io", - crate_root = "futures-io-0.3.29.crate/src/lib.rs", + crate_root = "futures-io-0.3.30.crate/src/lib.rs", edition = "2018", features = [ "default", @@ -2397,18 +2397,18 @@ cargo.rust_library( ) http_archive( - name = "futures-macro-0.3.29.crate", - sha256 = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb", - strip_prefix = "futures-macro-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-macro/0.3.29/download"], + name = "futures-macro-0.3.30.crate", + sha256 = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac", + strip_prefix = "futures-macro-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-macro/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-macro-0.3.29", - srcs = [":futures-macro-0.3.29.crate"], + name = "futures-macro-0.3.30", + srcs = [":futures-macro-0.3.30.crate"], crate = "futures_macro", - crate_root = "futures-macro-0.3.29.crate/src/lib.rs", + crate_root = "futures-macro-0.3.30.crate/src/lib.rs", edition = "2018", proc_macro = True, visibility = [], @@ -2420,18 +2420,18 @@ cargo.rust_library( ) http_archive( - name = "futures-sink-0.3.29.crate", - sha256 = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817", - strip_prefix = "futures-sink-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-sink/0.3.29/download"], + name = "futures-sink-0.3.30.crate", + sha256 = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5", + strip_prefix = "futures-sink-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-sink/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-sink-0.3.29", - srcs = [":futures-sink-0.3.29.crate"], + name = "futures-sink-0.3.30", + srcs = [":futures-sink-0.3.30.crate"], crate = "futures_sink", - crate_root = "futures-sink-0.3.29.crate/src/lib.rs", + crate_root = "futures-sink-0.3.30.crate/src/lib.rs", edition = "2018", features = [ "alloc", @@ -2442,29 +2442,29 @@ cargo.rust_library( ) http_archive( - name = "futures-task-0.3.29.crate", - sha256 = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2", - strip_prefix = "futures-task-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-task/0.3.29/download"], + name = "futures-task-0.3.30.crate", + sha256 = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004", + strip_prefix = "futures-task-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-task/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-task-0.3.29", - srcs = [":futures-task-0.3.29.crate"], + name = "futures-task-0.3.30", + srcs = [":futures-task-0.3.30.crate"], crate = "futures_task", - crate_root = "futures-task-0.3.29.crate/src/lib.rs", + crate_root = "futures-task-0.3.30.crate/src/lib.rs", edition = "2018", env = { - "CARGO_MANIFEST_DIR": "futures-task-0.3.29.crate", + "CARGO_MANIFEST_DIR": "futures-task-0.3.30.crate", "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "Tools for working with tasks.\n", "CARGO_PKG_NAME": "futures-task", "CARGO_PKG_REPOSITORY": "https://github.com/rust-lang/futures-rs", - "CARGO_PKG_VERSION": "0.3.29", + "CARGO_PKG_VERSION": "0.3.30", "CARGO_PKG_VERSION_MAJOR": "0", "CARGO_PKG_VERSION_MINOR": "3", - "CARGO_PKG_VERSION_PATCH": "29", + "CARGO_PKG_VERSION_PATCH": "30", }, features = [ "alloc", @@ -2475,29 +2475,29 @@ cargo.rust_library( ) http_archive( - name = "futures-util-0.3.29.crate", - sha256 = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104", - strip_prefix = "futures-util-0.3.29", - urls = ["https://crates.io/api/v1/crates/futures-util/0.3.29/download"], + name = "futures-util-0.3.30.crate", + sha256 = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48", + strip_prefix = "futures-util-0.3.30", + urls = ["https://crates.io/api/v1/crates/futures-util/0.3.30/download"], visibility = [], ) cargo.rust_library( - name = "futures-util-0.3.29", - srcs = [":futures-util-0.3.29.crate"], + name = "futures-util-0.3.30", + srcs = [":futures-util-0.3.30.crate"], crate = "futures_util", - crate_root = "futures-util-0.3.29.crate/src/lib.rs", + crate_root = "futures-util-0.3.30.crate/src/lib.rs", edition = "2018", env = { - "CARGO_MANIFEST_DIR": "futures-util-0.3.29.crate", + "CARGO_MANIFEST_DIR": "futures-util-0.3.30.crate", "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "Common utilities and extension traits for the futures-rs library.\n", "CARGO_PKG_NAME": "futures-util", "CARGO_PKG_REPOSITORY": "https://github.com/rust-lang/futures-rs", - "CARGO_PKG_VERSION": "0.3.29", + "CARGO_PKG_VERSION": "0.3.30", "CARGO_PKG_VERSION_MAJOR": "0", "CARGO_PKG_VERSION_MINOR": "3", - "CARGO_PKG_VERSION_PATCH": "29", + "CARGO_PKG_VERSION_PATCH": "30", }, features = [ "alloc", @@ -2517,12 +2517,12 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-io-0.3.29", - ":futures-macro-0.3.29", - ":futures-sink-0.3.29", - ":futures-task-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-io-0.3.30", + ":futures-macro-0.3.30", + ":futures-sink-0.3.30", + ":futures-task-0.3.30", ":memchr-2.6.4", ":pin-project-lite-0.2.13", ":pin-utils-0.1.0", @@ -2605,9 +2605,9 @@ cargo.rust_library( deps = [ ":bytes-1.5.0", ":fnv-1.0.7", - ":futures-core-0.3.29", - ":futures-sink-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-sink-0.3.30", + ":futures-util-0.3.30", ":http-0.2.9", ":indexmap-1.9.3", ":slab-0.4.9", @@ -2988,9 +2988,9 @@ cargo.rust_library( visibility = [], deps = [ ":bytes-1.5.0", - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-util-0.3.30", ":h2-0.3.21", ":http-0.2.9", ":http-body-0.4.5", @@ -3022,7 +3022,7 @@ cargo.rust_library( edition = "2021", visibility = [], deps = [ - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":http-0.2.9", ":hyper-0.14.27", ":rustls-0.21.8", @@ -3867,10 +3867,10 @@ cargo.rust_library( ":chrono-0.4.31", ":derivative-2.2.0", ":derive_more-0.99.17", - ":futures-core-0.3.29", - ":futures-executor-0.3.29", - ":futures-io-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-executor-0.3.30", + ":futures-io-0.3.30", + ":futures-util-0.3.30", ":hex-0.4.3", ":hmac-0.12.1", ":lazy_static-1.4.0", @@ -3921,7 +3921,7 @@ cargo.rust_library( deps = [ ":bytes-1.5.0", ":encoding_rs-0.8.33", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":http-0.2.9", ":httparse-1.8.0", ":log-0.4.20", @@ -4411,7 +4411,7 @@ cargo.rust_library( visibility = [], deps = [ ":async-trait-0.1.74", - ":futures-core-0.3.29", + ":futures-core-0.3.30", ":http-0.2.9", ":opentelemetry-http-0.9.0", ":opentelemetry-proto-0.3.0", @@ -4504,8 +4504,8 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-channel-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-util-0.3.30", ":indexmap-1.9.3", ":once_cell-1.18.0", ":pin-project-lite-0.2.13", @@ -4558,9 +4558,9 @@ cargo.rust_library( deps = [ ":async-trait-0.1.74", ":crossbeam-channel-0.5.8", - ":futures-channel-0.3.29", - ":futures-executor-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-executor-0.3.30", + ":futures-util-0.3.30", ":once_cell-1.18.0", ":opentelemetry_api-0.20.0", ":ordered-float-3.9.2", @@ -5749,8 +5749,8 @@ cargo.rust_library( deps = [ ":base64-0.21.5", ":bytes-1.5.0", - ":futures-core-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-util-0.3.30", ":http-0.2.9", ":serde-1.0.195", ":serde_json-1.0.108", @@ -7635,11 +7635,11 @@ cargo.rust_library( ":dotenvy-0.15.7", ":either-1.9.0", ":event-listener-2.5.3", - ":futures-channel-0.3.29", - ":futures-core-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", ":futures-intrusive-0.5.0", - ":futures-io-0.3.29", - ":futures-util-0.3.29", + ":futures-io-0.3.30", + ":futures-util-0.3.30", ":hashlink-0.8.4", ":hex-0.4.3", ":indexmap-2.0.2", @@ -7784,10 +7784,10 @@ cargo.rust_library( ":digest-0.10.7", ":dotenvy-0.15.7", ":either-1.9.0", - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-io-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-io-0.3.30", + ":futures-util-0.3.30", ":generic-array-0.14.7", ":hex-0.4.3", ":hkdf-0.12.3", @@ -7852,10 +7852,10 @@ cargo.rust_library( ":chrono-0.4.31", ":crc-3.0.1", ":dotenvy-0.15.7", - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-io-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-io-0.3.30", + ":futures-util-0.3.30", ":hex-0.4.3", ":hkdf-0.12.3", ":hmac-0.12.1", @@ -7908,11 +7908,11 @@ cargo.rust_library( ":atoi-2.0.0", ":chrono-0.4.31", ":flume-0.11.0", - ":futures-channel-0.3.29", - ":futures-core-0.3.29", - ":futures-executor-0.3.29", + ":futures-channel-0.3.30", + ":futures-core-0.3.30", + ":futures-executor-0.3.30", ":futures-intrusive-0.5.0", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":libsqlite3-sys-0.26.0", ":log-0.4.20", ":percent-encoding-2.3.0", @@ -8284,6 +8284,7 @@ cargo.rust_binary( ":tokio-1.33.0", ":tonic-0.10.2", ":tonic-build-0.10.2", + ":tonic-health-0.10.2", ":tracing-0.1.40", ":tracing-opentelemetry-0.20.0", ":tracing-subscriber-0.3.18", @@ -8667,7 +8668,7 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-core-0.3.29", + ":futures-core-0.3.30", ":pin-project-lite-0.2.13", ":tokio-1.33.0", ], @@ -8695,7 +8696,7 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":log-0.4.20", ":tokio-1.33.0", ":tungstenite-0.20.1", @@ -8727,9 +8728,9 @@ cargo.rust_library( visibility = [], deps = [ ":bytes-1.5.0", - ":futures-core-0.3.29", - ":futures-io-0.3.29", - ":futures-sink-0.3.29", + ":futures-core-0.3.30", + ":futures-io-0.3.30", + ":futures-sink-0.3.30", ":pin-project-lite-0.2.13", ":tokio-1.33.0", ":tracing-0.1.40", @@ -8876,8 +8877,8 @@ cargo.rust_library( ":axum-0.6.20", ":base64-0.21.5", ":bytes-1.5.0", - ":futures-core-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-util-0.3.30", ":h2-0.3.21", ":http-0.2.9", ":http-body-0.4.5", @@ -8931,6 +8932,40 @@ cargo.rust_library( ], ) +alias( + name = "tonic-health", + actual = ":tonic-health-0.10.2", + visibility = ["PUBLIC"], +) + +http_archive( + name = "tonic-health-0.10.2.crate", + sha256 = "f80db390246dfb46553481f6024f0082ba00178ea495dbb99e70ba9a4fafb5e1", + strip_prefix = "tonic-health-0.10.2", + urls = ["https://crates.io/api/v1/crates/tonic-health/0.10.2/download"], + visibility = [], +) + +cargo.rust_library( + name = "tonic-health-0.10.2", + srcs = [":tonic-health-0.10.2.crate"], + crate = "tonic_health", + crate_root = "tonic-health-0.10.2.crate/src/lib.rs", + edition = "2021", + features = [ + "default", + "transport", + ], + visibility = [], + deps = [ + ":async-stream-0.3.5", + ":prost-0.12.3", + ":tokio-1.33.0", + ":tokio-stream-0.1.14", + ":tonic-0.10.2", + ], +) + http_archive( name = "tower-0.4.13.crate", sha256 = "b8fa9be0de6cf49e536ce1851f987bd21a43b771b09473c3549a6c853db37c1c", @@ -8970,8 +9005,8 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-core-0.3.29", - ":futures-util-0.3.29", + ":futures-core-0.3.30", + ":futures-util-0.3.30", ":indexmap-1.9.3", ":pin-project-1.1.3", ":pin-project-lite-0.2.13", @@ -9127,8 +9162,8 @@ cargo.rust_library( ], visibility = [], deps = [ - ":futures-0.3.29", - ":futures-task-0.3.29", + ":futures-0.3.30", + ":futures-task-0.3.30", ":pin-project-1.1.3", ":tracing-0.1.40", ], @@ -9335,9 +9370,9 @@ cargo.rust_library( ":cfg-if-1.0.0", ":data-encoding-2.4.0", ":enum-as-inner-0.4.0", - ":futures-channel-0.3.29", - ":futures-io-0.3.29", - ":futures-util-0.3.29", + ":futures-channel-0.3.30", + ":futures-io-0.3.30", + ":futures-util-0.3.30", ":idna-0.2.3", ":ipnet-2.9.0", ":lazy_static-1.4.0", @@ -9395,7 +9430,7 @@ cargo.rust_library( visibility = [], deps = [ ":cfg-if-1.0.0", - ":futures-util-0.3.29", + ":futures-util-0.3.30", ":lazy_static-1.4.0", ":log-0.4.20", ":lru-cache-0.1.2", diff --git a/third-party/rust/Cargo.lock b/third-party/rust/Cargo.lock index aa06ebe665..e9d4e89ec3 100644 --- a/third-party/rust/Cargo.lock +++ b/third-party/rust/Cargo.lock @@ -926,9 +926,9 @@ checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" [[package]] name = "futures" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" dependencies = [ "futures-channel", "futures-core", @@ -941,9 +941,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", "futures-sink", @@ -951,15 +951,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" [[package]] name = "futures-executor" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" dependencies = [ "futures-core", "futures-task", @@ -979,15 +979,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" [[package]] name = "futures-macro" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", @@ -996,21 +996,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" [[package]] name = "futures-task" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" [[package]] name = "futures-util" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-channel", "futures-core", @@ -3048,6 +3048,7 @@ dependencies = [ "tokio", "tonic 0.10.2", "tonic-build", + "tonic-health", "tracing", "tracing-opentelemetry", "tracing-subscriber", @@ -3302,6 +3303,19 @@ dependencies = [ "syn 2.0.48", ] +[[package]] +name = "tonic-health" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f80db390246dfb46553481f6024f0082ba00178ea495dbb99e70ba9a4fafb5e1" +dependencies = [ + "async-stream", + "prost 0.12.3", + "tokio", + "tokio-stream", + "tonic 0.10.2", +] + [[package]] name = "tower" version = "0.4.13" diff --git a/third-party/rust/Cargo.toml b/third-party/rust/Cargo.toml index 441d8971f4..06afb53b55 100644 --- a/third-party/rust/Cargo.toml +++ b/third-party/rust/Cargo.toml @@ -49,6 +49,7 @@ http = "0.2.9" prost = "0.12" tonic = "0.10.2" +tonic-health = "0.10.2" tonic-build = { version = "0.10.2", features = ["prost"] } version_check = "0.9.4" # for some reason reindeer didn't auto-add this From 6b0f1faa0cd7a5b22ff64943d2b391cf45f355c3 Mon Sep 17 00:00:00 2001 From: Vaibhav Date: Mon, 15 Jan 2024 12:53:51 +0530 Subject: [PATCH 5/8] refactor(notifications): better handling of channel and category proto enums --- core/notifications/src/grpc/server/convert.rs | 18 +++++++++--------- core/notifications/src/grpc/server/mod.rs | 14 +++++++++++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/core/notifications/src/grpc/server/convert.rs b/core/notifications/src/grpc/server/convert.rs index 557bee9660..c97ffc3b97 100644 --- a/core/notifications/src/grpc/server/convert.rs +++ b/core/notifications/src/grpc/server/convert.rs @@ -1,21 +1,21 @@ use crate::app::ApplicationError; use crate::primitives::{UserNotificationCategory, UserNotificationChannel}; -impl From for UserNotificationCategory { - fn from(category: i32) -> Self { +use super::proto; + +impl From for UserNotificationCategory { + fn from(category: proto::NotificationCategory) -> Self { match category { - 0 => UserNotificationCategory::Circles, - 1 => UserNotificationCategory::Payments, - _ => unimplemented!(), + proto::NotificationCategory::Circles => UserNotificationCategory::Circles, + proto::NotificationCategory::Payments => UserNotificationCategory::Payments, } } } -impl From for UserNotificationChannel { - fn from(channel: i32) -> Self { +impl From for UserNotificationChannel { + fn from(channel: proto::NotificationChannel) -> Self { match channel { - 0 => UserNotificationChannel::Push, - _ => unimplemented!(), + proto::NotificationChannel::Push => UserNotificationChannel::Push, } } } diff --git a/core/notifications/src/grpc/server/mod.rs b/core/notifications/src/grpc/server/mod.rs index bd58414b1f..06e0680f6f 100644 --- a/core/notifications/src/grpc/server/mod.rs +++ b/core/notifications/src/grpc/server/mod.rs @@ -10,7 +10,10 @@ use tonic::{transport::Server, Request, Response, Status}; use self::proto::{notifications_service_server::NotificationsService, *}; use super::config::*; -use crate::app::{ApplicationError, *}; +use crate::{ + app::{ApplicationError, *}, + primitives::{UserNotificationCategory, UserNotificationChannel}, +}; pub struct Notifications { app: NotificationsApp, @@ -28,10 +31,15 @@ impl NotificationsService for Notifications { channel, category, } = request; - + let channel = proto::NotificationChannel::try_from(channel) + .map(UserNotificationChannel::from) + .map_err(|e| Status::invalid_argument(e.to_string()))?; + let category = proto::NotificationCategory::try_from(category) + .map(UserNotificationCategory::from) + .map_err(|e| Status::invalid_argument(e.to_string()))?; let should_send = self .app - .should_send_notification(user_id.clone().into(), channel.into(), category.into()) + .should_send_notification(user_id.clone().into(), channel, category) .await?; Ok(Response::new(ShouldSendNotificationResponse { From 26f8cebc0569ac4eb4f2724f252969e9050fe1d3 Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Mon, 15 Jan 2024 10:22:43 +0100 Subject: [PATCH 6/8] refactor(notifications): exctract higher level rust macros --- .github/workflows/buck2-test.yaml | 2 +- ci/core/pipeline.yml | 2 +- ci/core/template.lib.yml | 7 +- core/api-keys/BUCK | 79 +------------- core/notifications/BUCK | 93 ++--------------- lib/es-entity-rs/BUCK | 6 +- lib/tracing-rs/BUCK | 6 +- toolchains/rust/macros.bzl | 165 +++++++++++++++++++++++------- 8 files changed, 153 insertions(+), 207 deletions(-) diff --git a/.github/workflows/buck2-test.yaml b/.github/workflows/buck2-test.yaml index fb8f1e0e2c..a20012f8f0 100644 --- a/.github/workflows/buck2-test.yaml +++ b/.github/workflows/buck2-test.yaml @@ -38,7 +38,7 @@ jobs: ARGS+=" //core/api:test" ;; api-keys|notifications) - ARGS+=" //core/$LABEL:test" + ARGS+=" //core/$LABEL:$LABEL" ;; esac done diff --git a/ci/core/pipeline.yml b/ci/core/pipeline.yml index 33f0907f9a..f3e96db578 100644 --- a/ci/core/pipeline.yml +++ b/ci/core/pipeline.yml @@ -64,7 +64,7 @@ jobs: - #@ bump_image_in_chart(component) #@ end -- #@ buck_test("api") +- #@ buck_test("api", test_name = "test") #@ for component in core_bundle_components: - #@ build_edge_image(component) #@ end diff --git a/ci/core/template.lib.yml b/ci/core/template.lib.yml index 8d3189b65d..c3babf383b 100644 --- a/ci/core/template.lib.yml +++ b/ci/core/template.lib.yml @@ -18,7 +18,10 @@ source: #@ return "buck-test-" + component #@ end -#@ def buck_test(component): +#@ def buck_test(component, test_name = None): +#@ if test_name == None: +#@ test_name = component +#@ end name: #@ buck_test_name(component) serial: true plan: @@ -35,7 +38,7 @@ plan: - name: #@ component_src_resource_name(component) path: repo params: - BUCK_TARGET: #@ "//core/" + component + ":test" + BUCK_TARGET: #@ "//core/" + component + ":" + test_name BUCK_CMD: test run: path: pipeline-tasks/ci/apps/tasks/buck-task.sh diff --git a/core/api-keys/BUCK b/core/api-keys/BUCK index 231531ef43..d1f20aa948 100644 --- a/core/api-keys/BUCK +++ b/core/api-keys/BUCK @@ -1,5 +1,5 @@ load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file") -load("@toolchains//rust:macros.bzl", "rustfmt_check", "clippy_check") +load("@toolchains//rust:macros.bzl", "galoy_rust_bin", "rustfmt_check", "clippy_check") sdl( name = "sdl", @@ -30,20 +30,8 @@ rust_binary( ], ) -rust_binary( +galoy_rust_bin( name = "api-keys", - edition = "2021", - srcs = glob(["src/**/*.rs"]), - deps = [ - ":lib-api-keys", - "//third-party/rust:tokio", - "//third-party/rust:anyhow", - ], -) - -rust_library( - name = "lib-api-keys", - edition = "2021", deps = [ "//lib/tracing-rs:tracing", "//lib/es-entity-rs:es-entity", @@ -65,6 +53,7 @@ rust_library( "//third-party/rust:uuid", "//third-party/rust:serde_with", ], + extra_tests = ["//lib/tracing-rs:tracing"], srcs = glob([ "src/**/*.rs", ".sqlx/*", @@ -76,65 +65,3 @@ rust_library( "SQLX_OFFLINE": "true", } ) - -rust_test( - name = "test-unit", - edition = "2021", - srcs = glob([ - "src/**/*.rs", - ".sqlx/*", - "migrations/*", - "api-keys.yml" - ]), - crate_root = "src/lib.rs", - deps = [ - "//lib/tracing-rs:tracing", - "//lib/es-entity-rs:es-entity", - "//third-party/rust:tokio", - "//third-party/rust:anyhow", - "//third-party/rust:async-graphql", - "//third-party/rust:async-graphql-axum", - "//third-party/rust:axum", - "//third-party/rust:jsonwebtoken", - "//third-party/rust:clap", - "//third-party/rust:reqwest", - "//third-party/rust:serde", - "//third-party/rust:serde_yaml", - "//third-party/rust:serde_json", - "//third-party/rust:thiserror", - "//third-party/rust:chrono", - "//third-party/rust:sqlx", - "//third-party/rust:rand", - "//third-party/rust:uuid", - "//third-party/rust:serde_with", - ], - env = { - "CARGO_MANIFEST_DIR": ".", - "SQLX_OFFLINE": "true", - } -) - -rustfmt_check( - name = "check-format-rust", - srcs = glob([ - "src/**/*.rs", - ".sqlx/*", - "migrations/*", - "api-keys.yml" - ]), - crate_root = "src/lib.rs", -) - -clippy_check( - name = "check-lint-rust-lib", - clippy_txt_dep = ":lib-api-keys[clippy.txt]", -) - -test_suite( - name = "test", - tests = [ - ":check-format-rust", - ":check-lint-rust-lib", - ":test-unit" - ], -) diff --git a/core/notifications/BUCK b/core/notifications/BUCK index fb913fd64b..acc5071319 100644 --- a/core/notifications/BUCK +++ b/core/notifications/BUCK @@ -1,5 +1,5 @@ load("@toolchains//rover:macros.bzl", "sdl", "diff_check", "dev_update_file") -load("@toolchains//rust:macros.bzl", "rust_protobuf_library", "rustfmt_check", "clippy_check") +load("@toolchains//rust:macros.bzl", "galoy_rust_bin", "rustfmt_check", "clippy_check") sdl( name = "sdl", @@ -31,68 +31,14 @@ rust_binary( ) -rust_binary( +galoy_rust_bin( name = "notifications", - edition = "2021", - srcs = glob(["src/**/*.rs"]), - deps = [ - ":lib-notifications", - "//third-party/rust:tokio", - "//third-party/rust:anyhow", - ], -) - -rust_protobuf_library( - name = "lib-notifications", - env = { - "CARGO_MANIFEST_DIR": ".", - "SQLX_OFFLINE": "true", - }, - deps = [ - "//lib/tracing-rs:tracing", - "//lib/es-entity-rs:es-entity", - "//third-party/rust:tokio", - "//third-party/rust:anyhow", - "//third-party/rust:async-graphql", - "//third-party/rust:async-graphql-axum", - "//third-party/rust:axum", - "//third-party/rust:jsonwebtoken", - "//third-party/rust:clap", - "//third-party/rust:derive_builder", - "//third-party/rust:reqwest", - "//third-party/rust:serde", - "//third-party/rust:serde_yaml", - "//third-party/rust:serde_json", - "//third-party/rust:thiserror", - "//third-party/rust:chrono", - "//third-party/rust:sqlx", - "//third-party/rust:mongodb", - "//third-party/rust:rand", - "//third-party/rust:uuid", - "//third-party/rust:serde_with", - "//third-party/rust:tonic", - "//third-party/rust:tonic-health", - ], srcs = glob([ "src/**/*.rs", ".sqlx/*", "migrations/*", "notifications.yml" ]), - build_script = "build.rs", - protos = ["proto/notifications.proto"], -) - -rust_test( - name = "test-unit", - edition = "2021", - srcs = glob([ - "src/**/*.rs", - ".sqlx/*", - "migrations/*", - "api-keys.yml" - ]), - crate_root = "src/lib.rs", deps = [ "//lib/tracing-rs:tracing", "//lib/es-entity-rs:es-entity", @@ -115,34 +61,17 @@ rust_test( "//third-party/rust:rand", "//third-party/rust:uuid", "//third-party/rust:serde_with", + "//third-party/rust:tonic", + "//third-party/rust:tonic-health", + "//third-party/rust:prost", ], + extra_tests = [ + "//lib/tracing-rs:tracing", + "//lib/es-entity-rs:es-entity", + ], + protos = ["proto/notifications.proto"], env = { "CARGO_MANIFEST_DIR": ".", "SQLX_OFFLINE": "true", - } -) - -rustfmt_check( - name = "check-format-rust", - srcs = glob([ - "src/**/*.rs", - ".sqlx/*", - "migrations/*", - "api-keys.yml" - ]), - crate_root = "src/lib.rs", -) - -clippy_check( - name = "check-lint-rust-lib", - clippy_txt_dep = ":lib-notifications[clippy.txt]", -) - -test_suite( - name = "test", - tests = [ - ":check-format-rust", - ":check-lint-rust-lib", - ":test-unit" - ], + }, ) diff --git a/lib/es-entity-rs/BUCK b/lib/es-entity-rs/BUCK index ff2e51090b..6eebc8f655 100644 --- a/lib/es-entity-rs/BUCK +++ b/lib/es-entity-rs/BUCK @@ -1,6 +1,7 @@ -rust_library( +load("@toolchains//rust:macros.bzl", "galoy_rust_lib") + +galoy_rust_lib( name = "es-entity", - edition = "2021", deps = [ "//third-party/rust:thiserror", "//third-party/rust:derive_builder", @@ -14,5 +15,4 @@ rust_library( "CARGO_PKG_NAME": "es-entity" , "CARGO_PKG_VERSION": "0.1.0" , }, - visibility = ["PUBLIC"], ) diff --git a/lib/tracing-rs/BUCK b/lib/tracing-rs/BUCK index f26d549c5d..c4a2535fc6 100644 --- a/lib/tracing-rs/BUCK +++ b/lib/tracing-rs/BUCK @@ -1,6 +1,7 @@ -rust_library( +load("@toolchains//rust:macros.bzl", "galoy_rust_lib") + +galoy_rust_lib( name = "tracing", - edition = "2021", deps = [ "//third-party/rust:anyhow", "//third-party/rust:serde", @@ -18,5 +19,4 @@ rust_library( "CARGO_PKG_NAME": "tracing" , "CARGO_PKG_VERSION": "0.1.0" , }, - visibility = ["PUBLIC"], ) diff --git a/toolchains/rust/macros.bzl b/toolchains/rust/macros.bzl index 20a6c6b14e..e15810df09 100644 --- a/toolchains/rust/macros.bzl +++ b/toolchains/rust/macros.bzl @@ -15,66 +15,153 @@ load( "get_re_executor_from_props", ) -def rust_protobuf_library( +def galoy_rust_bin( name, srcs, - build_script, - protos, + deps, + lib_root = "src/lib.rs", + env = {}, edition = "2021", - env = None, - build_env = None, - deps = [], - test_deps = None, - doctests = True): + visibility = ["PUBLIC"], + extra_tests = [], + protos = None): + lib_name = "lib-{}".format(name) - build_name = name + "-build" - proto_name = name + "-proto" + galoy_rust_lib(name = lib_name, + srcs = srcs, + deps = deps, + lib_root = lib_root, + env = env, + edition = edition, + visibility = visibility, + extra_tests = extra_tests, + protos = protos) native.rust_binary( - name = build_name, - srcs = [build_script], - crate_root = build_script, + name = name, deps = [ - "//third-party/rust:tonic-build" + ":lib-{}".format(name), + "//third-party/rust:tokio", + "//third-party/rust:anyhow", + ], + srcs = srcs, + env = env, + edition = edition, + visibility = visibility, + tests = [ + ":{}".format(lib_name), + ":check-format-rust-bin", + ":check-lint-rust-bin", ], ) - build_env = build_env or {} - build_env.update( - { - "PROTOC": "$(exe //third-party/proto:protoc)", - "PROTOC_INCLUDE": "$(location //third-party/proto:google_protobuf)", - }, + rustfmt_check( + name = "check-format-rust-bin", + srcs = srcs, + crate_root = "src/main.rs", ) + clippy_check( + name = "check-lint-rust-bin", + clippy_txt_dep = ":{}[clippy.txt]".format(name), + ) - native.genrule( - name = proto_name, - srcs = protos + [ - "//third-party/proto:google_protobuf", - ], - out = ".", - cmd = "$(exe :" + build_name + ")", - env = build_env, + +def galoy_rust_lib( + name, + srcs, + deps, + lib_root = "src/lib.rs", + build_script = "build.rs", + env = {}, + edition = "2021", + visibility = ["PUBLIC"], + extra_tests = [], + protos = None, + build_env = None): + + rustfmt_check( + name = "check-format-rust-lib", + srcs = srcs, + crate_root = lib_root, + ) + + clippy_check( + name = "check-lint-rust-lib", + clippy_txt_dep = ":{}[clippy.txt]".format(name), + ) + + native.test_suite( + name = "test", + tests = [ + ":check-format-rust-lib", + ":check-lint-rust-lib", + ":test-unit-rust-lib" + ], + ) + + if protos != None: + build_name = name + "-build" + proto_name = name + "-proto" + + native.rust_binary( + name = build_name, + srcs = [build_script], + crate_root = build_script, + deps = [ + "//third-party/rust:tonic-build" + ], + ) + + build_env = build_env or {} + build_env.update( + { + "PROTOC": "$(exe //third-party/proto:protoc)", + "PROTOC_INCLUDE": "$(location //third-party/proto:google_protobuf)", + }, + ) + + + native.genrule( + name = proto_name, + srcs = protos + [ + "//third-party/proto:google_protobuf", + ], + out = ".", + cmd = "$(exe :" + build_name + ")", + env = build_env, + ) + + env = env or {} + env.update({ + "OUT_DIR": "$(location :{})".format(proto_name), + }) + + native.rust_test( + name = "test-unit-rust-lib", + edition = edition, + srcs = srcs, + crate_root = lib_root, + deps = deps, + env = env ) - env = env or {} - env.update({ - "OUT_DIR": "$(location :{})".format(proto_name), - }) native.rust_library( name = name, - env = env, + deps = deps, srcs = srcs, + env = env, + crate_root = lib_root, edition = edition, - doctests = doctests, - deps = [ - "//third-party/rust:prost" - ] + (deps or []) + visibility = visibility, + tests = [ + ":check-format-rust-lib", + ":check-lint-rust-lib", + ":test-unit-rust-lib" + ] + extra_tests, + features = ["fail-on-warnings"], ) - - def clippy_check_impl(ctx: AnalysisContext) -> list[[ DefaultInfo, RunInfo, From d2f84bdb675b499570a17b47c1a5bd3ad8cb979e Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Mon, 15 Jan 2024 10:30:39 +0100 Subject: [PATCH 7/8] fix(notifications): clippy --- core/notifications/src/grpc/server/convert.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/core/notifications/src/grpc/server/convert.rs b/core/notifications/src/grpc/server/convert.rs index c97ffc3b97..4b67f556d9 100644 --- a/core/notifications/src/grpc/server/convert.rs +++ b/core/notifications/src/grpc/server/convert.rs @@ -22,8 +22,6 @@ impl From for UserNotificationChannel { impl From for tonic::Status { fn from(err: ApplicationError) -> Self { - match err { - _ => tonic::Status::internal(err.to_string()), - } + tonic::Status::internal(err.to_string()) } } From 513059bc8481e9ed4ef392673672a80f65e8bc23 Mon Sep 17 00:00:00 2001 From: bodymindarts Date: Mon, 15 Jan 2024 10:50:30 +0100 Subject: [PATCH 8/8] refactor(notifications): remove dependency app -> tonic --- core/notifications/src/app/error.rs | 2 -- core/notifications/src/app/mod.rs | 4 ++-- core/notifications/src/grpc/server/convert.rs | 2 +- core/notifications/src/grpc/server/mod.rs | 4 ++-- 4 files changed, 5 insertions(+), 7 deletions(-) diff --git a/core/notifications/src/app/error.rs b/core/notifications/src/app/error.rs index 3de1a76b11..45097a2936 100644 --- a/core/notifications/src/app/error.rs +++ b/core/notifications/src/app/error.rs @@ -6,6 +6,4 @@ use crate::user_notification_settings::error::*; pub enum ApplicationError { #[error("{0}")] UserNotificationSettingsError(#[from] UserNotificationSettingsError), - #[error("{0}")] - GrpcServerError(#[from] tonic::transport::Error), } diff --git a/core/notifications/src/app/mod.rs b/core/notifications/src/app/mod.rs index 81f16af7e3..959a4e52d6 100644 --- a/core/notifications/src/app/mod.rs +++ b/core/notifications/src/app/mod.rs @@ -1,12 +1,12 @@ mod config; -mod error; +pub mod error; use sqlx::{Pool, Postgres}; use crate::{primitives::*, user_notification_settings::*}; pub use config::*; -pub use error::*; +use error::*; #[derive(Clone)] pub struct NotificationsApp { diff --git a/core/notifications/src/grpc/server/convert.rs b/core/notifications/src/grpc/server/convert.rs index 4b67f556d9..9356f0844b 100644 --- a/core/notifications/src/grpc/server/convert.rs +++ b/core/notifications/src/grpc/server/convert.rs @@ -1,4 +1,4 @@ -use crate::app::ApplicationError; +use crate::app::error::ApplicationError; use crate::primitives::{UserNotificationCategory, UserNotificationChannel}; use super::proto; diff --git a/core/notifications/src/grpc/server/mod.rs b/core/notifications/src/grpc/server/mod.rs index 06e0680f6f..45dc6da96d 100644 --- a/core/notifications/src/grpc/server/mod.rs +++ b/core/notifications/src/grpc/server/mod.rs @@ -11,7 +11,7 @@ use self::proto::{notifications_service_server::NotificationsService, *}; use super::config::*; use crate::{ - app::{ApplicationError, *}, + app::*, primitives::{UserNotificationCategory, UserNotificationChannel}, }; @@ -52,7 +52,7 @@ impl NotificationsService for Notifications { pub(crate) async fn start( server_config: GrpcServerConfig, app: NotificationsApp, -) -> Result<(), ApplicationError> { +) -> Result<(), tonic::transport::Error> { use proto::notifications_service_server::NotificationsServiceServer; let notifications = Notifications { app };