simple backend server

This commit is contained in:
Bryan Stitt 2022-07-11 19:16:18 +00:00
parent b61b00de3b
commit 23a545177f
9 changed files with 234 additions and 33 deletions

143
Cargo.lock generated

@ -180,9 +180,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "axum" name = "axum"
version = "0.5.11" version = "0.5.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2cc6e8e8c993cb61a005fab8c1e5093a29199b7253b05a6883999312935c1ff" checksum = "d16705af05732b7d3258ec0f7b73c03a658a28925e050d8852d5b568ee8bcf4e"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum-core", "axum-core",
@ -197,7 +197,7 @@ dependencies = [
"matchit", "matchit",
"memchr", "memchr",
"mime", "mime",
"percent-encoding", "percent-encoding 2.1.0",
"pin-project-lite", "pin-project-lite",
"serde", "serde",
"serde_json", "serde_json",
@ -224,9 +224,9 @@ dependencies = [
[[package]] [[package]]
name = "axum-core" name = "axum-core"
version = "0.2.6" version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf4d047478b986f14a13edad31a009e2e05cb241f9805d0d75e4cba4e129ad4d" checksum = "e4f44a0e6200e9d11a1cdc989e4b358f6e3d354fbf48478f345a17f4e43f8635"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"bytes", "bytes",
@ -927,7 +927,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3495912c9c1ccf2e18976439f4443f3fee0fd61f424ff99fde6a66b15ecb448f" checksum = "3495912c9c1ccf2e18976439f4443f3fee0fd61f424ff99fde6a66b15ecb448f"
dependencies = [ dependencies = [
"cfg-if 1.0.0", "cfg-if 1.0.0",
"hashbrown 0.12.1", "hashbrown 0.12.2",
"lock_api", "lock_api",
"parking_lot_core 0.9.3", "parking_lot_core 0.9.3",
] ]
@ -966,6 +966,29 @@ dependencies = [
"zeroize", "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]] [[package]]
name = "diff" name = "diff"
version = "0.1.12" version = "0.1.12"
@ -1222,7 +1245,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"syn", "syn",
"url", "url 2.2.2",
"walkdir", "walkdir",
] ]
@ -1306,7 +1329,7 @@ dependencies = [
"tokio", "tokio",
"tracing", "tracing",
"tracing-futures", "tracing-futures",
"url", "url 2.2.2",
] ]
[[package]] [[package]]
@ -1336,7 +1359,7 @@ dependencies = [
"tokio-tungstenite", "tokio-tungstenite",
"tracing", "tracing",
"tracing-futures", "tracing-futures",
"url", "url 2.2.2",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"wasm-timer", "wasm-timer",
@ -1492,7 +1515,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
dependencies = [ dependencies = [
"matches", "matches",
"percent-encoding", "percent-encoding 2.1.0",
] ]
[[package]] [[package]]
@ -1760,9 +1783,9 @@ checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.12.1" version = "0.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db0d4cf898abf0081f964436dc980e96670a0f36863e4b83aaacdb65c9d7ccc3" checksum = "607c8a29735385251a339424dd462993c0fed8fa09d378f259377df08c126022"
dependencies = [ dependencies = [
"ahash", "ahash",
] ]
@ -1782,7 +1805,7 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086" checksum = "d452c155cb93fecdfb02a73dd57b5d8e442c2063bd7aac72f1bc5e4263a43086"
dependencies = [ dependencies = [
"hashbrown 0.12.1", "hashbrown 0.12.2",
] ]
[[package]] [[package]]
@ -1920,6 +1943,17 @@ dependencies = [
"tokio-rustls", "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]] [[package]]
name = "idna" name = "idna"
version = "0.2.3" version = "0.2.3"
@ -2165,7 +2199,7 @@ name = "linkedhashmap"
version = "0.2.0" version = "0.2.0"
dependencies = [ dependencies = [
"criterion", "criterion",
"hashbrown 0.12.1", "hashbrown 0.12.2",
"hashlink", "hashlink",
"linked-hash-map", "linked-hash-map",
"slab", "slab",
@ -2305,6 +2339,16 @@ dependencies = [
"ws2_32-sys", "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]] [[package]]
name = "nanorand" name = "nanorand"
version = "0.7.0" version = "0.7.0"
@ -2616,6 +2660,12 @@ dependencies = [
"sha2 0.10.2", "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]] [[package]]
name = "percent-encoding" name = "percent-encoding"
version = "2.1.0" version = "2.1.0"
@ -2735,6 +2785,12 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "pkg-config"
version = "0.3.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1df8c4ec4b0627e53bdf214615ad287367e482558cf84b109250b37464dc03ae"
[[package]] [[package]]
name = "plotters" name = "plotters"
version = "0.3.1" version = "0.3.1"
@ -2935,11 +2991,11 @@ dependencies = [
"dtoa", "dtoa",
"futures-util", "futures-util",
"itoa 0.4.8", "itoa 0.4.8",
"percent-encoding", "percent-encoding 2.1.0",
"pin-project-lite", "pin-project-lite",
"tokio", "tokio",
"tokio-util 0.6.10", "tokio-util 0.6.10",
"url", "url 2.2.2",
] ]
[[package]] [[package]]
@ -3026,7 +3082,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"mime", "mime",
"percent-encoding", "percent-encoding 2.1.0",
"pin-project-lite", "pin-project-lite",
"rustls", "rustls",
"rustls-pemfile", "rustls-pemfile",
@ -3036,7 +3092,7 @@ dependencies = [
"tokio", "tokio",
"tokio-rustls", "tokio-rustls",
"tower-service", "tower-service",
"url", "url 2.2.2",
"wasm-bindgen", "wasm-bindgen",
"wasm-bindgen-futures", "wasm-bindgen-futures",
"web-sys", "web-sys",
@ -3239,9 +3295,9 @@ checksum = "930c0acf610d3fdb5e2ab6213019aaa04e227ebe9547b0649ba599b16d788bd7"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.138" version = "1.0.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47" checksum = "0171ebb889e45aa68b44aee0859b3eede84c6f5f5c228e6f140c0b2a0a46cad6"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
@ -3268,9 +3324,9 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.138" version = "1.0.139"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c" checksum = "dc1d3230c1de7932af58ad8ffbe1d784bd55efd5a9d84ac24f69c72d83543dfb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
@ -3542,7 +3598,7 @@ dependencies = [
"thiserror", "thiserror",
"tokio", "tokio",
"tracing", "tracing",
"url", "url 2.2.2",
] ]
[[package]] [[package]]
@ -3952,7 +4008,7 @@ dependencies = [
"rustls", "rustls",
"sha-1", "sha-1",
"thiserror", "thiserror",
"url", "url 2.2.2",
"utf-8", "utf-8",
"webpki", "webpki",
] ]
@ -4020,6 +4076,17 @@ version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a156c684c91ea7d62626509bce3cb4e1d9ed5c4d978f7b4352658f96a4c26b4a" 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]] [[package]]
name = "url" name = "url"
version = "2.2.2" version = "2.2.2"
@ -4027,9 +4094,9 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c" checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
dependencies = [ dependencies = [
"form_urlencoded", "form_urlencoded",
"idna", "idna 0.2.3",
"matches", "matches",
"percent-encoding", "percent-encoding 2.1.0",
] ]
[[package]] [[package]]
@ -4054,6 +4121,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.4" version = "0.9.4"
@ -4184,6 +4257,20 @@ dependencies = [
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]]
name = "web3-backend"
version = "0.1.0"
dependencies = [
"axum",
"diesel",
"serde",
"serde_json",
"tokio",
"toml",
"tracing",
"tracing-subscriber",
]
[[package]] [[package]]
name = "web3-proxy" name = "web3-proxy"
version = "0.1.0" version = "0.1.0"
@ -4200,7 +4287,7 @@ dependencies = [
"fdlimit", "fdlimit",
"flume", "flume",
"futures", "futures",
"hashbrown 0.12.1", "hashbrown 0.12.2",
"linkedhashmap", "linkedhashmap",
"notify", "notify",
"num", "num",
@ -4218,7 +4305,7 @@ dependencies = [
"tower", "tower",
"tracing", "tracing",
"tracing-subscriber", "tracing-subscriber",
"url", "url 2.2.2",
] ]
[[package]] [[package]]

@ -2,6 +2,7 @@
members = [ members = [
"linkedhashmap", "linkedhashmap",
"redis-cell-client", "redis-cell-client",
"web3-backend",
"web3-proxy", "web3-proxy",
] ]

@ -1,4 +1,5 @@
--- ---
# production config
version: "3.4" version: "3.4"
services: services:

@ -14,3 +14,20 @@ services:
- ./config/example.toml:/config.toml - ./config/example.toml:/config.toml
ports: ports:
- 8544:8544 - 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

@ -9,7 +9,7 @@ inline-more = [ "hashbrown" ]
[dependencies] [dependencies]
slab = "0.4.6" slab = "0.4.6"
hashbrown = { version = "0.12.1", optional = true } hashbrown = { version = "0.12.2", optional = true }
[dev-dependencies] [dev-dependencies]
criterion = "0.3.6" criterion = "0.3.6"

16
web3-backend/Cargo.toml Normal file

@ -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"] }

64
web3-backend/src/main.rs Normal file

@ -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<CreateUser>,
) -> 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,
}

@ -13,7 +13,7 @@ deadlock_detection = ["parking_lot/deadlock_detection"]
anyhow = { version = "1.0.58", features = ["backtrace"] } anyhow = { version = "1.0.58", features = ["backtrace"] }
arc-swap = "1.5.0" arc-swap = "1.5.0"
argh = "0.1.8" 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" axum-client-ip = "0.2.0"
counter = "0.5.5" counter = "0.5.5"
dashmap = "5.3.4" dashmap = "5.3.4"
@ -22,10 +22,10 @@ ethers = { git = "https://github.com/gakonst/ethers-rs", features = ["rustls", "
fdlimit = "0.2.1" fdlimit = "0.2.1"
flume = "0.10.13" flume = "0.10.13"
futures = { version = "0.3.21", features = ["thread-pool"] } futures = { version = "0.3.21", features = ["thread-pool"] }
hashbrown = "0.12.1" hashbrown = "0.12.2"
linkedhashmap = { path = "../linkedhashmap", features = ["inline-more"] } linkedhashmap = { path = "../linkedhashmap", features = ["inline-more"] }
notify = "4.0.17" notify = "4.0.17"
num = "0.4" num = "0.4.0"
redis-cell-client = { path = "../redis-cell-client" } redis-cell-client = { path = "../redis-cell-client" }
# TODO: parking_lot has an "arc_lock" feature that we might want to use # TODO: parking_lot has an "arc_lock" feature that we might want to use
parking_lot = "0.12.1" parking_lot = "0.12.1"
@ -34,7 +34,7 @@ proctitle = "0.1.1"
regex = "1.6.0" regex = "1.6.0"
reqwest = { version = "0.11.11", default-features = false, features = ["json", "tokio-rustls"] } reqwest = { version = "0.11.11", default-features = false, features = ["json", "tokio-rustls"] }
rustc-hash = "1.1.0" 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"] } serde_json = { version = "1.0.82", default-features = false, features = ["alloc", "raw_value"] }
tokio = { version = "1.19.2", features = ["full", "tracing"] } tokio = { version = "1.19.2", features = ["full", "tracing"] }
toml = "0.5.9" toml = "0.5.9"

@ -0,0 +1,15 @@
use redis_cell_client::bb8;
use tracing::warn;
#[derive(Debug, Clone)]
pub struct RedisErrorSink;
impl bb8::ErrorSink<redis_cell_client::RedisError> for RedisErrorSink {
fn sink(&self, err: redis_cell_client::RedisError) {
warn!(?err, "redis error");
}
fn boxed_clone(&self) -> Box<dyn bb8::ErrorSink<redis_cell_client::RedisError>> {
Box::new(self.clone())
}
}