mnemonic-hash-checker: use state over extension

This commit is contained in:
Ryan Heywood 2023-08-05 21:34:17 -05:00
parent 7d9e50c3ef
commit 0625c2672a
Signed by: ryan
GPG Key ID: 8E401478A3FBEF72
1 changed files with 5 additions and 11 deletions

View File

@ -1,7 +1,7 @@
use std::net::SocketAddr; use std::net::SocketAddr;
use std::sync::Arc; use std::sync::Arc;
use axum::{extract::Path, routing::get, Extension, Json, Router}; use axum::{extract::{Path, State}, routing::get, Json, Router};
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
@ -17,10 +17,6 @@ use tracing_subscriber::prelude::*;
use tracing::{debug, info}; use tracing::{debug, info};
struct State {
pool: Pool,
}
fn setup_registry() { fn setup_registry() {
let envfilter = EnvFilter::builder() let envfilter = EnvFilter::builder()
.with_default_directive(LevelFilter::DEBUG.into()) .with_default_directive(LevelFilter::DEBUG.into())
@ -48,12 +44,12 @@ async fn check_hash(hash: &str, pool: &Pool) -> Result<bool> {
} }
// Note: Exposes *zero* information of potential errors to clients. // Note: Exposes *zero* information of potential errors to clients.
#[tracing::instrument(skip(hash, state))] #[tracing::instrument(skip(hash, pool))]
async fn check_hash_slug( async fn check_hash_slug(
Path(hash): Path<String>, Path(hash): Path<String>,
Extension(state): Extension<Arc<State>>, State(pool): State<Arc<Pool>>,
) -> Json<Option<bool>> { ) -> Json<Option<bool>> {
let result = check_hash(&hash, &state.pool).await; let result = check_hash(&hash, &pool).await;
if let Err(e) = &result { if let Err(e) = &result {
debug!(%e, "Error while performing lookup"); debug!(%e, "Error while performing lookup");
} }
@ -77,12 +73,10 @@ async fn main() -> Result<()> {
}); });
let pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?; let pool = config.create_pool(Some(Runtime::Tokio1), NoTls)?;
let state = State { pool };
let app = Router::new() let app = Router::new()
.route("/check/:hash", get(check_hash_slug)) .route("/check/:hash", get(check_hash_slug))
.with_state(Arc::new(pool))
.layer(CatchPanicLayer::new()) .layer(CatchPanicLayer::new())
.layer(Extension(Arc::new(state)))
.layer(TraceLayer::new_for_http()); .layer(TraceLayer::new_for_http());
info!("server go nyoom"); info!("server go nyoom");