fix root db call

This commit is contained in:
Tyler Murphy 2023-02-15 00:47:55 -05:00
parent aec4fdecc1
commit a8b6798dfe
3 changed files with 20 additions and 26 deletions

View file

@ -1,15 +1,8 @@
use crate::{ use crate::types::extract::{RouterURI, self};
database,
types::extract::{DatabaseExtention, RouterURI},
};
use axum::{ use axum::{
error_handling::HandleErrorLayer, error_handling::HandleErrorLayer,
http::Request, BoxError, Extension, Router, middleware,
middleware::{self, Next},
response::Response,
BoxError, Extension, Router,
}; };
use tokio::sync::Mutex;
use tower::ServiceBuilder; use tower::ServiceBuilder;
use tower_governor::{ use tower_governor::{
errors::display_error, governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor, errors::display_error, governor::GovernorConfigBuilder, key_extractor::SmartIpKeyExtractor,
@ -23,18 +16,6 @@ pub mod users;
pub use auth::RegistrationRequet; 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 { pub fn router() -> Router {
let governor_conf = Box::new( let governor_conf = Box::new(
GovernorConfigBuilder::default() GovernorConfigBuilder::default()
@ -71,5 +52,5 @@ pub fn router() -> Router {
config: Box::leak(governor_conf), config: Box::leak(governor_conf),
}), }),
) )
.layer(middleware::from_fn(connect)) .layer(middleware::from_fn(extract::connect))
} }

View file

@ -1,13 +1,13 @@
use axum::{ use axum::{
response::{IntoResponse, Redirect, Response}, response::{IntoResponse, Redirect, Response},
routing::get, routing::get,
Router, Router, middleware,
}; };
use crate::{ use crate::{
public::console, public::console,
types::{ types::{
extract::{AuthorizedUser, Log, UserAgent}, extract::{AuthorizedUser, Log, UserAgent, self},
http::ResponseCode, http::ResponseCode,
}, },
}; };
@ -70,6 +70,7 @@ pub fn router() -> Router {
Router::new() Router::new()
.route("/", get(root)) .route("/", get(root))
.route("/login", get(login)) .route("/login", get(login))
.layer(middleware::from_fn(extract::connect))
.route("/home", get(home)) .route("/home", get(home))
.route("/people", get(people)) .route("/people", get(people))
.route("/profile", get(profile)) .route("/profile", get(profile))

View file

@ -9,7 +9,7 @@ use axum::{
extract::{ConnectInfo, FromRequest, FromRequestParts}, extract::{ConnectInfo, FromRequest, FromRequestParts},
http::{header::USER_AGENT, request::Parts, Request}, http::{header::USER_AGENT, request::Parts, Request},
response::Response, response::Response,
BoxError, RequestExt, BoxError, RequestExt, middleware::Next,
}; };
use bytes::Bytes; use bytes::Bytes;
use image::{io::Reader, DynamicImage, ImageFormat}; use image::{io::Reader, DynamicImage, ImageFormat};
@ -100,7 +100,7 @@ where
}; };
let Some(db) = parts.extensions.get::<DatabaseExtention>() else { let Some(db) = parts.extensions.get::<DatabaseExtention>() else {
return Err(ResponseCode::Forbidden.text("Could not connect to database")) return Err(ResponseCode::InternalServerError.text("Could not connect to database"))
}; };
let db = db.0.lock().await; let db = db.0.lock().await;
@ -288,6 +288,18 @@ where
} }
} }
pub 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
}
async fn read_body<S, B>(mut req: Request<B>, state: &S) -> Result<Vec<u8>> async fn read_body<S, B>(mut req: Request<B>, state: &S) -> Result<Vec<u8>>
where where
B: HttpBody + Sync + Send + 'static, B: HttpBody + Sync + Send + 'static,