From 23a545177f668e7a33fdd4922cf8a24bcaf41292 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 11 Jul 2022 19:16:18 +0000 Subject: [PATCH] simple backend server --- Cargo.lock | 143 +++++++++++++++++++++++++++------- Cargo.toml | 1 + docker-compose.prod.yml | 1 + docker-compose.yml | 17 ++++ linkedhashmap/Cargo.toml | 2 +- web3-backend/Cargo.toml | 16 ++++ web3-backend/src/main.rs | 64 +++++++++++++++ web3-proxy/Cargo.toml | 8 +- web3-proxy/src/bb8_helpers.rs | 15 ++++ 9 files changed, 234 insertions(+), 33 deletions(-) create mode 100644 web3-backend/Cargo.toml create mode 100644 web3-backend/src/main.rs create mode 100644 web3-proxy/src/bb8_helpers.rs diff --git a/Cargo.lock b/Cargo.lock index 4bb646ee..b7d20f82 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -180,9 +180,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" [[package]] name = "axum" -version = "0.5.11" +version = "0.5.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2cc6e8e8c993cb61a005fab8c1e5093a29199b7253b05a6883999312935c1ff" +checksum = "d16705af05732b7d3258ec0f7b73c03a658a28925e050d8852d5b568ee8bcf4e" dependencies = [ "async-trait", "axum-core", @@ -197,7 +197,7 @@ dependencies = [ "matchit", "memchr", "mime", - "percent-encoding", + "percent-encoding 2.1.0", "pin-project-lite", "serde", "serde_json", @@ -224,9 +224,9 @@ dependencies = [ [[package]] name = "axum-core" -version = "0.2.6" +version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4d047478b986f14a13edad31a009e2e05cb241f9805d0d75e4cba4e129ad4d" +checksum = "e4f44a0e6200e9d11a1cdc989e4b358f6e3d354fbf48478f345a17f4e43f8635" dependencies = [ "async-trait", "bytes", @@ -927,7 +927,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3495912c9c1ccf2e18976439f4443f3fee0fd61f424ff99fde6a66b15ecb448f" dependencies = [ "cfg-if 1.0.0", - "hashbrown 0.12.1", + "hashbrown 0.12.2", "lock_api", "parking_lot_core 0.9.3", ] @@ -966,6 +966,29 @@ dependencies = [ "zeroize", ] +[[package]] +name = "diesel" +version = "1.4.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b28135ecf6b7d446b43e27e225622a038cc4e2930a1022f51cdb97ada19b8e4d" +dependencies = [ + "byteorder", + "diesel_derives", + "mysqlclient-sys", + "url 1.7.2", +] + +[[package]] +name = "diesel_derives" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "diff" version = "0.1.12" @@ -1222,7 +1245,7 @@ dependencies = [ "serde", "serde_json", "syn", - "url", + "url 2.2.2", "walkdir", ] @@ -1306,7 +1329,7 @@ dependencies = [ "tokio", "tracing", "tracing-futures", - "url", + "url 2.2.2", ] [[package]] @@ -1336,7 +1359,7 @@ dependencies = [ "tokio-tungstenite", "tracing", "tracing-futures", - "url", + "url 2.2.2", "wasm-bindgen", "wasm-bindgen-futures", "wasm-timer", @@ -1492,7 +1515,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" dependencies = [ "matches", - "percent-encoding", + "percent-encoding 2.1.0", ] [[package]] @@ -1760,9 +1783,9 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" [[package]] name = "hashbrown" -version = "0.12.1" +version = "0.12.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3" +checksum = "607c8a29735385251a339424dd462993c0fed8fa09d378f259377df08c126022" dependencies = [ "ahash", ] @@ -1782,7 +1805,7 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086" dependencies = [ - "hashbrown 0.12.1", + "hashbrown 0.12.2", ] [[package]] @@ -1920,6 +1943,17 @@ dependencies = [ "tokio-rustls", ] +[[package]] +name = "idna" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38f09e0f0b1fb55fdee1f17470ad800da77af5186a1a76c026b679358b7e844e" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "idna" version = "0.2.3" @@ -2165,7 +2199,7 @@ name = "linkedhashmap" version = "0.2.0" dependencies = [ "criterion", - "hashbrown 0.12.1", + "hashbrown 0.12.2", "hashlink", "linked-hash-map", "slab", @@ -2305,6 +2339,16 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "mysqlclient-sys" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f61b381528ba293005c42a409dd73d034508e273bf90481f17ec2e964a6e969b" +dependencies = [ + "pkg-config", + "vcpkg", +] + [[package]] name = "nanorand" version = "0.7.0" @@ -2616,6 +2660,12 @@ dependencies = [ "sha2 0.10.2", ] +[[package]] +name = "percent-encoding" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31010dd2e1ac33d5b46a5b413495239882813e0369f8ed8a5e266f173602f831" + [[package]] name = "percent-encoding" version = "2.1.0" @@ -2735,6 +2785,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "pkg-config" +version = "0.3.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae" + [[package]] name = "plotters" version = "0.3.1" @@ -2935,11 +2991,11 @@ dependencies = [ "dtoa", "futures-util", "itoa 0.4.8", - "percent-encoding", + "percent-encoding 2.1.0", "pin-project-lite", "tokio", "tokio-util 0.6.10", - "url", + "url 2.2.2", ] [[package]] @@ -3026,7 +3082,7 @@ dependencies = [ "lazy_static", "log", "mime", - "percent-encoding", + "percent-encoding 2.1.0", "pin-project-lite", "rustls", "rustls-pemfile", @@ -3036,7 +3092,7 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "url", + "url 2.2.2", "wasm-bindgen", "wasm-bindgen-futures", "web-sys", @@ -3239,9 +3295,9 @@ checksum = "930c0acf610d3fdb5e2ab6213019aaa04e227ebe9547b0649ba599b16d788bd7" [[package]] name = "serde" -version = "1.0.138" +version = "1.0.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47" +checksum = "0171ebb889e45aa68b44aee0859b3eede84c6f5f5c228e6f140c0b2a0a46cad6" dependencies = [ "serde_derive", ] @@ -3268,9 +3324,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.138" +version = "1.0.139" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c" +checksum = "dc1d3230c1de7932af58ad8ffbe1d784bd55efd5a9d84ac24f69c72d83543dfb" dependencies = [ "proc-macro2", "quote", @@ -3542,7 +3598,7 @@ dependencies = [ "thiserror", "tokio", "tracing", - "url", + "url 2.2.2", ] [[package]] @@ -3952,7 +4008,7 @@ dependencies = [ "rustls", "sha-1", "thiserror", - "url", + "url 2.2.2", "utf-8", "webpki", ] @@ -4020,6 +4076,17 @@ version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" +[[package]] +name = "url" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a" +dependencies = [ + "idna 0.1.5", + "matches", + "percent-encoding 1.0.1", +] + [[package]] name = "url" version = "2.2.2" @@ -4027,9 +4094,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" dependencies = [ "form_urlencoded", - "idna", + "idna 0.2.3", "matches", - "percent-encoding", + "percent-encoding 2.1.0", ] [[package]] @@ -4054,6 +4121,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.4" @@ -4184,6 +4257,20 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "web3-backend" +version = "0.1.0" +dependencies = [ + "axum", + "diesel", + "serde", + "serde_json", + "tokio", + "toml", + "tracing", + "tracing-subscriber", +] + [[package]] name = "web3-proxy" version = "0.1.0" @@ -4200,7 +4287,7 @@ dependencies = [ "fdlimit", "flume", "futures", - "hashbrown 0.12.1", + "hashbrown 0.12.2", "linkedhashmap", "notify", "num", @@ -4218,7 +4305,7 @@ dependencies = [ "tower", "tracing", "tracing-subscriber", - "url", + "url 2.2.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index a3721e3b..5af76878 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "linkedhashmap", "redis-cell-client", + "web3-backend", "web3-proxy", ] diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 232ad287..16499092 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -1,4 +1,5 @@ --- +# production config version: "3.4" services: diff --git a/docker-compose.yml b/docker-compose.yml index e6ef53e0..3c68d3c8 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,3 +14,20 @@ services: - ./config/example.toml:/config.toml ports: - 8544:8544 + + dev-user-portal: + + dev-db: + image: mysql + # NOTE: use of "mysql_native_password" is not recommended: https://dev.mysql.com/doc/refman/8.0/en/upgrading-from-previous-series.html#upgrade-caching-sha2-password + # (this is just an example, not intended to be a production configuration) + command: --default-authentication-plugin=mysql_native_password + environment: + MYSQL_ROOT_PASSWORD: example + + dev-adminer: + image: adminer + ports: + - 8080:8080 + environment: + ADMINER_DEFAULT_SERVER: dev-db diff --git a/linkedhashmap/Cargo.toml b/linkedhashmap/Cargo.toml index 9add0ee6..5afff8b4 100644 --- a/linkedhashmap/Cargo.toml +++ b/linkedhashmap/Cargo.toml @@ -9,7 +9,7 @@ inline-more = [ "hashbrown" ] [dependencies] slab = "0.4.6" -hashbrown = { version = "0.12.1", optional = true } +hashbrown = { version = "0.12.2", optional = true } [dev-dependencies] criterion = "0.3.6" diff --git a/web3-backend/Cargo.toml b/web3-backend/Cargo.toml new file mode 100644 index 00000000..81856113 --- /dev/null +++ b/web3-backend/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "web3-backend" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +axum = { version = "0.5.12", features = ["serde_json"] } +diesel = { version = "1.4.8", features = ["mysql"] } +serde = { version = "1.0.139", features = ["derive"] } +serde_json = "1.0.82" +tokio = { version = "1.19.2", features = ["full", "tracing"] } +toml = "0.5.9" +tracing = "0.1.35" +tracing-subscriber = { version = "0.3.14", features = ["env-filter"] } diff --git a/web3-backend/src/main.rs b/web3-backend/src/main.rs new file mode 100644 index 00000000..3fe179b3 --- /dev/null +++ b/web3-backend/src/main.rs @@ -0,0 +1,64 @@ +use axum::{ + http::StatusCode, + response::IntoResponse, + routing::{get, post}, + Json, Router, +}; +use serde::{Deserialize, Serialize}; +use std::net::SocketAddr; + +#[tokio::main] +async fn main() { + // initialize tracing + tracing_subscriber::fmt::init(); + + // build our application with a route + let app = Router::new() + // `GET /` goes to `root` + .route("/", get(root)) + // `POST /users` goes to `create_user` + .route("/users", post(create_user)); + + // run our app with hyper + // `axum::Server` is a re-export of `hyper::Server` + let addr = SocketAddr::from(([127, 0, 0, 1], 3000)); + tracing::debug!("listening on {}", addr); + axum::Server::bind(&addr) + .serve(app.into_make_service()) + .await + .unwrap(); +} + +// basic handler that responds with a static string +async fn root() -> &'static str { + "Hello, World!" +} + +async fn create_user( + // this argument tells axum to parse the request body + // as JSON into a `CreateUser` type + Json(payload): Json, +) -> impl IntoResponse { + // insert your application logic here + let user = User { + id: 1337, + username: payload.username, + }; + + // this will be converted into a JSON response + // with a status code of `201 Created` + (StatusCode::CREATED, Json(user)) +} + +// the input to our `create_user` handler +#[derive(Deserialize)] +struct CreateUser { + username: String, +} + +// the output to our `create_user` handler +#[derive(Serialize)] +struct User { + id: u64, + username: String, +} diff --git a/web3-proxy/Cargo.toml b/web3-proxy/Cargo.toml index 6eee34db..2c75b8f1 100644 --- a/web3-proxy/Cargo.toml +++ b/web3-proxy/Cargo.toml @@ -13,7 +13,7 @@ deadlock_detection = ["parking_lot/deadlock_detection"] anyhow = { version = "1.0.58", features = ["backtrace"] } arc-swap = "1.5.0" argh = "0.1.8" -axum = { version = "0.5.11", features = ["serde_json", "tokio-tungstenite", "ws"] } +axum = { version = "0.5.12", features = ["serde_json", "tokio-tungstenite", "ws"] } axum-client-ip = "0.2.0" counter = "0.5.5" dashmap = "5.3.4" @@ -22,10 +22,10 @@ ethers = { git = "https://github.com/gakonst/ethers-rs", features = ["rustls", " fdlimit = "0.2.1" flume = "0.10.13" futures = { version = "0.3.21", features = ["thread-pool"] } -hashbrown = "0.12.1" +hashbrown = "0.12.2" linkedhashmap = { path = "../linkedhashmap", features = ["inline-more"] } notify = "4.0.17" -num = "0.4" +num = "0.4.0" redis-cell-client = { path = "../redis-cell-client" } # TODO: parking_lot has an "arc_lock" feature that we might want to use parking_lot = "0.12.1" @@ -34,7 +34,7 @@ proctitle = "0.1.1" regex = "1.6.0" reqwest = { version = "0.11.11", default-features = false, features = ["json", "tokio-rustls"] } rustc-hash = "1.1.0" -serde = { version = "1.0.138", features = [] } +serde = { version = "1.0.139", features = [] } serde_json = { version = "1.0.82", default-features = false, features = ["alloc", "raw_value"] } tokio = { version = "1.19.2", features = ["full", "tracing"] } toml = "0.5.9" diff --git a/web3-proxy/src/bb8_helpers.rs b/web3-proxy/src/bb8_helpers.rs new file mode 100644 index 00000000..1826359c --- /dev/null +++ b/web3-proxy/src/bb8_helpers.rs @@ -0,0 +1,15 @@ +use redis_cell_client::bb8; +use tracing::warn; + +#[derive(Debug, Clone)] +pub struct RedisErrorSink; + +impl bb8::ErrorSink for RedisErrorSink { + fn sink(&self, err: redis_cell_client::RedisError) { + warn!(?err, "redis error"); + } + + fn boxed_clone(&self) -> Box> { + Box::new(self.clone()) + } +}