mnemonic-hash-checker: initial commit

This commit is contained in:
RyanSquared 2023-08-05 21:27:34 -05:00
parent 51c8b33ca0
commit b74747d443
Signed by untrusted user who does not match committer: ryan
GPG Key ID: 8E401478A3FBEF72
4 changed files with 1718 additions and 0 deletions

1
mnemonic-hash-checker/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
target

1603
mnemonic-hash-checker/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
[package]
name = "mnemonic-hash-checker"
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.6.20", features = ["headers", "macros", "tracing"] }
clap = { version = "4.3.19", features = ["derive"] }
color-eyre = "0.6.2"
deadpool-postgres = "0.10.5"
serde = { version = "1.0.181", features = ["serde_derive"] }
tokio = { version = "1.29.1", features = ["full", "fs", "tracing"] }
tokio-postgres = "0.7.8"
tower-http = { version = "0.4.3", features = ["trace", "catch-panic"] }
tracing = "0.1.37"
tracing-error = "0.2.0"
tracing-subscriber = { version = "0.3.17", features = ["env-filter", "fmt", "ansi", "json"] }

View File

@ -0,0 +1,95 @@
use std::net::SocketAddr;
use std::sync::Arc;
use axum::{extract::Path, routing::get, Extension, Json, Router};
use color_eyre::eyre::Result;
use deadpool_postgres::{Config, ManagerConfig, Pool, RecyclingMethod, Runtime};
use tokio_postgres::NoTls;
use tower_http::catch_panic::CatchPanicLayer;
use tower_http::trace::TraceLayer;
use tracing_subscriber::filter::{EnvFilter, LevelFilter};
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::prelude::*;
use tracing::{debug, info};
struct State {
pool: Pool,
}
fn setup_registry() {
let envfilter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into())
.from_env_lossy();
tracing_subscriber::registry()
.with(envfilter)
.with(tracing_subscriber::fmt::layer().with_span_events(FmtSpan::CLOSE))
.with(tracing_error::ErrorLayer::default())
.init();
}
#[tracing::instrument(skip(pool))]
async fn check_hash(hash: &str, pool: &Pool) -> Result<bool> {
let client = pool.get().await?;
let query = client
.prepare_cached("SELECT hash FROM testing WHERE hash = $1")
.await?;
let rows = client.query(&query, &[&hash]).await?;
if let Some(row) = rows.get(0) {
let retrieved_hash: String = row.try_get(0)?;
Ok(hash == retrieved_hash)
} else {
Ok(false)
}
}
// Note: Exposes *zero* information of potential errors to clients.
#[tracing::instrument(skip(hash, state))]
async fn check_hash_slug(
Path(hash): Path<String>,
Extension(state): Extension<Arc<State>>,
) -> Json<Option<bool>> {
let result = check_hash(&hash, &state.pool).await;
if let Err(e) = &result {
debug!(%e, "Error while performing lookup");
}
Json(result.ok())
}
#[tokio::main]
async fn main() -> Result<()> {
setup_registry();
color_eyre::install()?;
let addr: SocketAddr = "0.0.0.0:8000".parse()?;
let mut config = Config::new();
config.dbname = Some("postgres".to_string());
config.host = Some("localhost".to_string());
config.password = Some("sandwich".to_string());
config.port = Some(5432);
config.user = Some("postgres".to_string());
config.manager = Some(ManagerConfig {
recycling_method: RecyclingMethod::Fast,
});
let pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?;
let state = State { pool };
let app = Router::new()
.route("/check/:hash", get(check_hash_slug))
.layer(CatchPanicLayer::new())
.layer(Extension(Arc::new(state)))
.layer(TraceLayer::new_for_http());
info!("server go nyoom");
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await?;
Ok(())
}