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

View file

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

View file

@ -9,7 +9,7 @@ use axum::{
extract::{ConnectInfo, FromRequest, FromRequestParts},
http::{header::USER_AGENT, request::Parts, Request},
response::Response,
BoxError, RequestExt,
BoxError, RequestExt, middleware::Next,
};
use bytes::Bytes;
use image::{io::Reader, DynamicImage, ImageFormat};
@ -100,7 +100,7 @@ where
};
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;
@ -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>>
where
B: HttpBody + Sync + Send + 'static,