diff options
author | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-15 00:01:44 -0500 |
---|---|---|
committer | Tyler Murphy <tylermurphy534@gmail.com> | 2023-02-15 00:01:44 -0500 |
commit | aec4fdecc10be35cde5dc42308960f10bc452187 (patch) | |
tree | 67233229c6839c78d1bd3db0147467da30843f44 /src/api/mod.rs | |
parent | bug fixes (diff) | |
download | xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.tar.gz xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.tar.bz2 xssbook-aec4fdecc10be35cde5dc42308960f10bc452187.zip |
make database calls 1 conn
Diffstat (limited to 'src/api/mod.rs')
-rw-r--r-- | src/api/mod.rs | 27 |
1 files changed, 25 insertions, 2 deletions
diff --git a/src/api/mod.rs b/src/api/mod.rs index cd2190c..eeaaa0a 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -1,5 +1,15 @@ -use crate::types::extract::RouterURI; -use axum::{error_handling::HandleErrorLayer, BoxError, Extension, Router}; +use crate::{ + database, + types::extract::{DatabaseExtention, RouterURI}, +}; +use axum::{ + error_handling::HandleErrorLayer, + http::Request, + middleware::{self, Next}, + response::Response, + BoxError, Extension, Router, +}; +use tokio::sync::Mutex; use tower::ServiceBuilder; use tower_governor::{ errors::display_error, governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, @@ -13,6 +23,18 @@ pub mod users; pub use auth::RegistrationRequet; +async fn connect<B>(mut req: Request<B>, next: Next<B>) -> Response +where + B: Send, +{ + if let Ok(db) = database::Database::connect() { + let ex = DatabaseExtention(Mutex::new(db)); + req.extensions_mut().insert(ex); + } + + next.run(req).await +} + pub fn router() -> Router { let governor_conf = Box::new( GovernorConfigBuilder::default() @@ -49,4 +71,5 @@ pub fn router() -> Router { config: Box::leak(governor_conf), }), ) + .layer(middleware::from_fn(connect)) } |