tower-request-id

This commit is contained in:
Bryan Stitt 2022-08-16 00:33:26 +00:00
parent e42737a7d2
commit 3b23ed1f8e
8 changed files with 265 additions and 59 deletions

249
Cargo.lock generated
View File

@ -327,6 +327,15 @@ dependencies = [
"syn",
]
[[package]]
name = "autocfg"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0dde43e75fd43e8a1bf86103336bc699aa8d17ad1be60c76c0bdfd4828e19b78"
dependencies = [
"autocfg 1.1.0",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -762,11 +771,13 @@ version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
dependencies = [
"js-sys",
"libc",
"num-integer",
"num-traits",
"serde",
"time 0.1.43",
"wasm-bindgen",
"winapi 0.3.9",
]
@ -829,6 +840,15 @@ dependencies = [
"os_str_bytes",
]
[[package]]
name = "cloudabi"
version = "0.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f"
dependencies = [
"bitflags",
]
[[package]]
name = "coins-bip32"
version = "0.7.0"
@ -860,7 +880,7 @@ dependencies = [
"hex",
"hmac",
"pbkdf2 0.11.0",
"rand",
"rand 0.8.5",
"sha2 0.10.2",
"thiserror",
]
@ -1083,7 +1103,7 @@ version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1145cf131a2c6ba0615079ab6a638f7e1973ac9c2634fcbeaaad6114246efe8c"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"cfg-if 1.0.0",
"crossbeam-utils",
"lazy_static",
@ -1134,7 +1154,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f2b443d17d49dad5ef0ede301c3179cc923b8822f3393b4d2c28c269dd4a122"
dependencies = [
"generic-array 0.14.5",
"rand_core",
"rand_core 0.6.3",
"subtle",
"zeroize",
]
@ -1360,7 +1380,7 @@ dependencies = [
"generic-array 0.14.5",
"group",
"pkcs8 0.9.0",
"rand_core",
"rand_core 0.6.3",
"sec1",
"subtle",
"zeroize",
@ -1411,7 +1431,7 @@ dependencies = [
"hex",
"hmac",
"pbkdf2 0.10.1",
"rand",
"rand 0.8.5",
"scrypt",
"serde",
"serde_json",
@ -1553,7 +1573,7 @@ dependencies = [
"k256",
"once_cell",
"proc-macro2",
"rand",
"rand 0.8.5",
"rlp",
"rlp-derive",
"rust_decimal",
@ -1658,7 +1678,7 @@ dependencies = [
"eth-keystore",
"ethers-core",
"hex",
"rand",
"rand 0.8.5",
"sha2 0.10.2",
"thiserror",
]
@ -1772,7 +1792,7 @@ version = "0.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df689201f395c6b90dfe87127685f8dbfc083a5e779e613575d8bd7314300c3e"
dependencies = [
"rand_core",
"rand_core 0.6.3",
"subtle",
]
@ -1802,7 +1822,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcf0ed7fe52a17a03854ec54a9f76d6d84508d1c0e66bc1793301c73fc8493c"
dependencies = [
"byteorder",
"rand",
"rand 0.8.5",
"rustc-hex",
"static_assertions",
]
@ -1891,6 +1911,12 @@ dependencies = [
"libc",
]
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
@ -2118,7 +2144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7391856def869c1c81063a03457c676fbcd419709c3dfb33d8d319de484b154d"
dependencies = [
"ff",
"rand_core",
"rand_core 0.6.3",
"subtle",
]
@ -2267,9 +2293,9 @@ dependencies = [
[[package]]
name = "http"
version = "0.2.7"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff8670570af52249509a86f5e3e18a08c60b177071826898fde8997cf5f6bfbb"
checksum = "75f43d41e26995c17e71ee126451dd3941010b0514a81a9d11f3b341debc2399"
dependencies = [
"bytes",
"fnv",
@ -2403,7 +2429,7 @@ version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10a35a97730320ffe8e2d410b5d3b69279b98d2c14bdb8b70ea89ecf7888d41e"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"hashbrown",
]
@ -2634,7 +2660,7 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327fa5b6a6940e4699ec49a9beae1ea4845c6bab9314e4f84ac68742139d8c53"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"scopeguard",
]
@ -2690,7 +2716,7 @@ version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
dependencies = [
"autocfg",
"autocfg 1.1.0",
]
[[package]]
@ -2857,7 +2883,7 @@ version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"num-integer",
"num-traits",
]
@ -2874,7 +2900,7 @@ dependencies = [
"num-integer",
"num-iter",
"num-traits",
"rand",
"rand 0.8.5",
"smallvec",
"zeroize",
]
@ -2894,7 +2920,7 @@ version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"num-traits",
]
@ -2904,7 +2930,7 @@ version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"num-integer",
"num-traits",
]
@ -2915,7 +2941,7 @@ version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0638a1c9d0a3c0914158145bc76cff373a75a627e6ecbfb71cbe6f453a5a19b0"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"num-bigint",
"num-integer",
"num-traits",
@ -2927,7 +2953,7 @@ version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"libm",
]
@ -3118,7 +3144,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d791538a6dcc1e7cb7fe6f6b58aca40e7f79403c45b2bc274008b5e647af1d8"
dependencies = [
"base64ct",
"rand_core",
"rand_core 0.6.3",
"subtle",
]
@ -3129,7 +3155,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7676374caaee8a325c9e7a2ae557f216c5563a171d6997b0ef8a65af35147700"
dependencies = [
"base64ct",
"rand_core",
"rand_core 0.6.3",
"subtle",
]
@ -3266,7 +3292,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d5285893bb5eb82e6aaf5d59ee909a06a16737a8970984dd7746ba9283498d6"
dependencies = [
"phf_shared",
"rand",
"rand 0.8.5",
]
[[package]]
@ -3422,7 +3448,7 @@ dependencies = [
"hmac",
"md-5",
"memchr",
"rand",
"rand 0.8.5",
"sha2 0.10.2",
"stringprep",
]
@ -3572,6 +3598,25 @@ version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09"
[[package]]
name = "rand"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d71dacdc3c88c1fde3885a3be3fbab9f35724e6ce99467f7d9c5026132184ca"
dependencies = [
"autocfg 0.1.8",
"libc",
"rand_chacha 0.1.1",
"rand_core 0.4.2",
"rand_hc",
"rand_isaac",
"rand_jitter",
"rand_os",
"rand_pcg",
"rand_xorshift",
"winapi 0.3.9",
]
[[package]]
name = "rand"
version = "0.8.5"
@ -3579,8 +3624,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [
"libc",
"rand_chacha",
"rand_core",
"rand_chacha 0.3.1",
"rand_core 0.6.3",
]
[[package]]
name = "rand_chacha"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "556d3a1ca6600bfcbab7c7c91ccb085ac7fbbcd70e008a98742e7847f4f7bcef"
dependencies = [
"autocfg 0.1.8",
"rand_core 0.3.1",
]
[[package]]
@ -3590,9 +3645,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.6.3"
@ -3602,13 +3672,76 @@ dependencies = [
"getrandom",
]
[[package]]
name = "rand_hc"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b40677c7be09ae76218dc623efbf7b18e34bced3f38883af07bb75630a21bc4"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_isaac"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ded997c9d5f13925be2a6fd7e66bf1872597f759fd9dd93513dd7e92e5a5ee08"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rand_jitter"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1166d5c91dc97b88d1decc3285bb0a99ed84b05cfd0bc2341bdf2d43fc41e39b"
dependencies = [
"libc",
"rand_core 0.4.2",
"winapi 0.3.9",
]
[[package]]
name = "rand_os"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b75f676a1e053fc562eafbb47838d67c84801e38fc1ba459e8f180deabd5071"
dependencies = [
"cloudabi",
"fuchsia-cprng",
"libc",
"rand_core 0.4.2",
"rdrand",
"wasm-bindgen",
"winapi 0.3.9",
]
[[package]]
name = "rand_pcg"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abf9b09b01790cfe0364f52bf32995ea3c39f4d2dd011eac241d2914146d0b44"
dependencies = [
"autocfg 0.1.8",
"rand_core 0.4.2",
]
[[package]]
name = "rand_xorshift"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbf7e9e623549b0e21f6e97cf8ecf247c1a8fd2e8a992ae265314300b2455d5c"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "rayon"
version = "1.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd99e5772ead8baa5215278c9b15bf92087709e9c1b2d1f97cdb5a183c933a7d"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"crossbeam-deque",
"either",
"rayon-core",
@ -3626,6 +3759,15 @@ dependencies = [
"num_cpus",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
name = "redis"
version = "0.21.5"
@ -3818,7 +3960,7 @@ dependencies = [
"num-traits",
"pkcs1",
"pkcs8 0.8.0",
"rand_core",
"rand_core 0.6.3",
"smallvec",
"subtle",
"zeroize",
@ -4297,7 +4439,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f054c6c1a6e95179d6f23ed974060dcefb2d9388bb7256900badad682c499de4"
dependencies = [
"digest 0.10.3",
"rand_core",
"rand_core 0.6.3",
]
[[package]]
@ -4316,7 +4458,7 @@ dependencies = [
"http",
"iri-string",
"k256",
"rand",
"rand 0.8.5",
"sha3 0.9.1",
"thiserror",
"time 0.3.11",
@ -4328,7 +4470,7 @@ version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4614a76b2a8be0058caa9dbbaf66d988527d86d003c11a94fbd335d7661edcef"
dependencies = [
"autocfg",
"autocfg 1.1.0",
]
[[package]]
@ -4449,7 +4591,7 @@ dependencies = [
"once_cell",
"paste",
"percent-encoding",
"rand",
"rand 0.8.5",
"rsa",
"rust_decimal",
"rustls",
@ -4587,7 +4729,7 @@ dependencies = [
"indicatif",
"itertools",
"once_cell",
"rand",
"rand 0.8.5",
"reqwest",
"semver",
"serde",
@ -4791,7 +4933,7 @@ version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a8325f63a7d4774dd041e363b2409ed1c5cbbd0f867795e661df066b2b0a581"
dependencies = [
"autocfg",
"autocfg 1.1.0",
"bytes",
"libc",
"memchr",
@ -4912,9 +5054,9 @@ dependencies = [
[[package]]
name = "tower-http"
version = "0.3.3"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d342c6d58709c0a6d48d48dabbb62d4ef955cf5f0f3bbfd845838e7ae88dbae"
checksum = "3c530c8675c1dbf98facee631536fa116b5fb6382d7dd6dc1b118d970eafe3ba"
dependencies = [
"bitflags",
"bytes",
@ -4927,6 +5069,7 @@ dependencies = [
"tower",
"tower-layer",
"tower-service",
"tracing",
]
[[package]]
@ -4935,6 +5078,18 @@ version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "343bc9466d3fe6b0f960ef45960509f84480bf4fd96f92901afe7ff3df9d3a62"
[[package]]
name = "tower-request-id"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "10b702a9ce17a8bd4c2f0e6acdcf75debaf14e4125cee76d4c5ac730f3ed9520"
dependencies = [
"http",
"tower-layer",
"tower-service",
"ulid",
]
[[package]]
name = "tower-service"
version = "0.3.1"
@ -5033,7 +5188,7 @@ dependencies = [
"http",
"httparse",
"log",
"rand",
"rand 0.8.5",
"rustls",
"sha-1",
"thiserror",
@ -5066,6 +5221,17 @@ dependencies = [
"static_assertions",
]
[[package]]
name = "ulid"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7e95a59b292ca0cf9b45be2e52294d1ca6cb24eb11b08ef4376f73f1a00c549"
dependencies = [
"chrono",
"lazy_static",
"rand 0.6.5",
]
[[package]]
name = "unicode-bidi"
version = "0.3.8"
@ -5328,6 +5494,7 @@ dependencies = [
"futures",
"handlebars",
"hashbrown",
"http",
"indexmap",
"migration",
"notify",
@ -5336,7 +5503,7 @@ dependencies = [
"petgraph",
"proctitle",
"prometheus-client",
"rand",
"rand 0.8.5",
"redis-rate-limit",
"regex",
"reqwest",
@ -5349,6 +5516,8 @@ dependencies = [
"tokio-stream",
"toml",
"tower",
"tower-http",
"tower-request-id",
"tracing",
"tracing-subscriber",
"url",

View File

@ -75,6 +75,8 @@
- [x] drop redis-cell in favor of a simpler (and faster) implementation.
- redis-cell was giving me weird errors and it isn't worth debugging it right now.
- [x] create user script should allow setting the api key
- [x] attach a request id to every web request
- [ ] attach user id (not IP!) to each request
- [-] basic request method stats
- [ ] use siwe messages and signatures for sign up and login
- [ ] fantom_1 | 2022-08-10T22:19:43.522465Z WARN web3_proxy::jsonrpc: forwarding error err=missing field `jsonrpc` at line 1 column 60

View File

@ -30,6 +30,7 @@ fdlimit = "0.2.1"
flume = "0.10.14"
futures = { version = "0.3.23", features = ["thread-pool"] }
hashbrown = { version = "0.12.3", features = ["serde"] }
http = "0.2.8"
indexmap = "1.9.1"
fifomap = { path = "../fifomap" }
notify = "4.0.17"
@ -50,11 +51,15 @@ serde = { version = "1.0.143", features = [] }
serde_json = { version = "1.0.83", default-features = false, features = ["alloc", "raw_value"] }
tokio = { version = "1.20.1", features = ["full", "tracing"] }
# TODO: make sure this uuid version matches what is in sea orm. PR on sea orm to put builder into prelude
uuid = "1.1.2"
tokio-stream = { version = "0.1.9", features = ["sync"] }
toml = "0.5.9"
tower = "0.4.13"
tower-request-id = "0.2.0"
tower-http = { version = "0.3.4", features = ["trace"] }
tracing = "0.1.36"
# TODO: tracing-subscriber has serde and serde_json features that we might want to use
tracing-subscriber = { version = "0.3.15", features = ["env-filter", "parking_lot"] }
url = "2.2.2"
tower = "0.4.13"
tokio-stream = { version = "0.1.9", features = ["sync"] }
uuid = "1.1.2"
# TODO: https://github.com/ulid/spec instead of uuid

View File

@ -520,7 +520,6 @@ impl Web3ProxyApp {
pub async fn proxy_web3_rpc(
&self,
request: JsonRpcRequestEnum,
user_id: u64,
) -> anyhow::Result<JsonRpcForwardedResponseEnum> {
trace!(?request, "proxy_web3_rpc");
@ -533,10 +532,10 @@ impl Web3ProxyApp {
let response = match request {
JsonRpcRequestEnum::Single(request) => JsonRpcForwardedResponseEnum::Single(
timeout(max_time, self.proxy_web3_rpc_request(request, user_id)).await??,
timeout(max_time, self.proxy_web3_rpc_request(request)).await??,
),
JsonRpcRequestEnum::Batch(requests) => JsonRpcForwardedResponseEnum::Batch(
timeout(max_time, self.proxy_web3_rpc_requests(requests, user_id)).await??,
timeout(max_time, self.proxy_web3_rpc_requests(requests)).await??,
),
};
@ -549,7 +548,6 @@ impl Web3ProxyApp {
async fn proxy_web3_rpc_requests(
&self,
requests: Vec<JsonRpcRequest>,
user_id: u64,
) -> anyhow::Result<Vec<JsonRpcForwardedResponse>> {
// TODO: we should probably change ethers-rs to support this directly
// we cut up the request and send to potentually different servers. this could be a problem.
@ -559,7 +557,7 @@ impl Web3ProxyApp {
let responses = join_all(
requests
.into_iter()
.map(|request| self.proxy_web3_rpc_request(request, user_id))
.map(|request| self.proxy_web3_rpc_request(request))
.collect::<Vec<_>>(),
)
.await;
@ -621,7 +619,6 @@ impl Web3ProxyApp {
async fn proxy_web3_rpc_request(
&self,
mut request: JsonRpcRequest,
user_id: u64,
) -> anyhow::Result<JsonRpcForwardedResponse> {
trace!("Received request: {:?}", request);

View File

@ -3,6 +3,7 @@ use axum::response::Response;
use axum::{http::StatusCode, response::IntoResponse, Extension, Json};
use axum_client_ip::ClientIp;
use std::sync::Arc;
use tracing::{error_span, Instrument};
use uuid::Uuid;
use super::errors::anyhow_error_into_response;
@ -21,15 +22,15 @@ pub async fn public_proxy_web3_rpc(
Err(err_response) => return err_response,
_ => unimplemented!(),
},
Err(err) => return anyhow_error_into_response(None, None, err).into_response(),
Err(err) => return anyhow_error_into_response(None, None, err),
};
let user_id = 0;
let protocol = Protocol::HTTP;
let user_id = 0;
match &payload {
JsonRpcRequestEnum::Batch(batch) => {
// TODO: use inc_by if possible?
// TODO: use inc_by if possible? need to group them by rpc_method
for single in batch {
let rpc_method = single.method.clone();
@ -37,8 +38,8 @@ pub async fn public_proxy_web3_rpc(
.stats
.proxy_requests
.get_or_create(&ProxyRequestLabels {
protocol: protocol.clone(),
rpc_method,
protocol: protocol.clone(),
user_id,
})
.inc();
@ -59,7 +60,11 @@ pub async fn public_proxy_web3_rpc(
}
};
match app.proxy_web3_rpc(payload, user_id).await {
let user_id = 0;
let user_span = error_span!("user", user_id);
match app.proxy_web3_rpc(payload).instrument(user_span).await {
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
Err(err) => anyhow_error_into_response(None, None, err).into_response(),
}
@ -79,7 +84,7 @@ pub async fn user_proxy_web3_rpc(
Err(err) => return anyhow_error_into_response(None, None, err).into_response(),
};
match app.proxy_web3_rpc(payload, user_id).await {
match app.proxy_web3_rpc(payload).await {
Ok(response) => (StatusCode::OK, Json(&response)).into_response(),
Err(err) => anyhow_error_into_response(None, None, err),
}

View File

@ -6,19 +6,42 @@ mod rate_limit;
mod users;
mod ws_proxy;
use ::http::Request;
use axum::{
body::Body,
handler::Handler,
routing::{get, post},
Extension, Router,
};
use std::net::SocketAddr;
use std::sync::Arc;
use tracing::info;
use tower_http::trace::TraceLayer;
use tower_request_id::{RequestId, RequestIdLayer};
use tracing::{error_span, info};
use crate::app::Web3ProxyApp;
///
/// http and websocket frontend for customers
pub async fn serve(port: u16, proxy_app: Arc<Web3ProxyApp>) -> anyhow::Result<()> {
// create a tracing span for each request
let request_tracing_layer =
TraceLayer::new_for_http().make_span_with(|request: &Request<Body>| {
// We get the request id from the extensions
let request_id = request
.extensions()
.get::<RequestId>()
.map(ToString::to_string)
.unwrap_or_else(|| "unknown".into());
// And then we put it along with other information into the `request` span
error_span!(
"http_request",
id = %request_id,
// TODO: do we want these?
method = %request.method(),
// uri = %request.uri(),
)
});
// build our application with a route
// order most to least common
let app = Router::new()
@ -30,11 +53,14 @@ pub async fn serve(port: u16, proxy_app: Arc<Web3ProxyApp>) -> anyhow::Result<()
.route("/status", get(http::status))
.route("/users", post(users::create_user))
.layer(Extension(proxy_app))
// create a unique id for each request and add it to our tracing logs
.layer(request_tracing_layer)
.layer(RequestIdLayer)
// 404 for any unknown routes
.fallback(errors::handler_404.into_service());
// run our app with hyper
// TODO: allow only listening on localhost?
// TODO: allow only listening on localhost? top_config.app.host.parse()?
let addr = SocketAddr::from(([0, 0, 0, 0], port));
info!("listening on port {}", port);
// TODO: into_make_service is enough if we always run behind a proxy. make into_make_service_with_connect_info optional?

View File

@ -149,7 +149,7 @@ async fn handle_socket_payload(
Ok(response.into())
}
_ => app.proxy_web3_rpc(payload.into(), user_id).await,
_ => app.proxy_web3_rpc(payload.into()).await,
};
(id, response)

View File

@ -13,7 +13,9 @@ use tracing::info;
#[derive(Clone, Hash, PartialEq, Eq, Encode)]
pub struct ProxyRequestLabels {
/// GET is websocket, POST is http
pub protocol: Protocol,
/// jsonrpc 2.0 method
pub rpc_method: String,
/// anonymous is user 0
pub user_id: u64,