From 8af6925762b1c9f49524983a4b08e25e48e5dbeb Mon Sep 17 00:00:00 2001 From: sargon64 Date: Sat, 29 Jul 2023 16:46:56 -0700 Subject: [PATCH] fix: persist db conn --- src/cdn.rs | 6 ++---- src/main.rs | 6 +++--- src/mods.rs | 14 ++++++-------- src/users.rs | 10 +++------- 4 files changed, 14 insertions(+), 22 deletions(-) diff --git a/src/cdn.rs b/src/cdn.rs index 5943aeb..8e5a81d 100644 --- a/src/cdn.rs +++ b/src/cdn.rs @@ -6,12 +6,10 @@ use crate::Database; #[get("/cdn/{slug}@{version}")] async fn cdn_get(db: web::Data, path: web::Path<(String, String)>) -> impl Responder { - let db = db.pool.clone(); - let (slug, version) = path.into_inner(); let db_mod = Mods::find() .filter(entity::mods::Column::Slug.eq(&slug)) - .one(&db) + .one(&db.pool) .await .unwrap(); @@ -19,7 +17,7 @@ async fn cdn_get(db: web::Data, path: web::Path<(String, String)>) -> let db_version = Versions::find() .filter(entity::versions::Column::ModId.eq(db_mod.id)) .filter(entity::versions::Column::Version.eq(&version)) - .one(&db) + .one(&db.pool) .await .unwrap(); diff --git a/src/main.rs b/src/main.rs index 379dbe7..250b205 100644 --- a/src/main.rs +++ b/src/main.rs @@ -66,9 +66,8 @@ lazy_static::lazy_static! { #[get("/")] async fn index(data: web::Data) -> impl Responder { - let db = data.pool.clone(); - let user_count = entity::users::Entity::find().count(&db).await.unwrap(); - let mod_count = entity::mods::Entity::find().count(&db).await.unwrap(); + let user_count = entity::users::Entity::find().count(&data.pool).await.unwrap(); + let mod_count = entity::mods::Entity::find().count(&data.pool).await.unwrap(); let mut res = String::new(); res.push_str(""); @@ -103,6 +102,7 @@ async fn main() -> io::Result<()> { let mut db_conf = sea_orm::ConnectOptions::new(std::env::var("DATABASE_URL").unwrap()); db_conf.max_connections(20); + db_conf.min_connections(5); db_conf.sqlx_logging(true); db_conf.sqlx_logging_level(log::LevelFilter::Debug); diff --git a/src/mods.rs b/src/mods.rs index fca66cd..4cba2df 100644 --- a/src/mods.rs +++ b/src/mods.rs @@ -183,8 +183,6 @@ pub async fn create_mod( mut payload: web::Payload, req: HttpRequest, ) -> impl Responder { - let db = db.pool.clone(); - let auth = req .headers() .get("Authorization") @@ -194,7 +192,7 @@ pub async fn create_mod( let auser_id; if auth.starts_with("Bearer") { let auth = Authorization::parse(Some(auth.split(" ").collect::>()[1].to_string())); - let user = auth.get_user(&db).await.unwrap(); + let user = auth.get_user(&db.pool).await.unwrap(); if !validate_permissions(&user, Permission::CREATE_MOD).await { return HttpResponse::Unauthorized().body("Unauthorized"); } @@ -214,7 +212,7 @@ pub async fn create_mod( let db_cata = Categories::find() .filter(entity::categories::Column::Name.eq(forgemod.manifest.category.clone().to_string())) - .one(&db) + .one(&db.pool) .await .unwrap(); @@ -224,7 +222,7 @@ pub async fn create_mod( } else { Categories::find() .filter(entity::categories::Column::Name.eq("other")) - .one(&db) + .one(&db.pool) .await .unwrap() .unwrap() @@ -232,7 +230,7 @@ pub async fn create_mod( let v_req = forgemod.manifest.game_version.clone(); let vers = BeatSaberVersions::find() - .all(&db) + .all(&db.pool) .await .unwrap() .into_iter() @@ -246,13 +244,13 @@ pub async fn create_mod( // see if mod exists; if it does add a new version; if it doesn't create a new mod let mby_mod = Mods::find() .filter(entity::mods::Column::Slug.eq(forgemod.manifest._id.clone())) - .one(&db) + .one(&db.pool) .await .unwrap(); let v_id; - let trans = db.begin().await.unwrap(); + let trans = db.pool.begin().await.unwrap(); if let Some(db_mod) = mby_mod { let db_mod = db_mod.id; diff --git a/src/users.rs b/src/users.rs index 991b7e1..1342881 100644 --- a/src/users.rs +++ b/src/users.rs @@ -157,8 +157,6 @@ pub async fn user_auth( data: web::Data, info: web::Query, ) -> impl Responder { - let db = data.pool.clone(); - let code = &info.code; let gat = minreq::post("https://github.com/login/oauth/access_token") @@ -187,7 +185,7 @@ pub async fn user_auth( let mby_user = Users::find() .filter(entity::users::Column::GithubId.eq(github_user.id as i32)) - .one(&db) + .one(&data.pool) .await .unwrap(); @@ -202,18 +200,16 @@ pub async fn user_auth( ..Default::default() }; - Users::insert(usr).exec(&db).await.unwrap(); + Users::insert(usr).exec(&data.pool).await.unwrap(); } let user = Users::find() .filter(entity::users::Column::GithubId.eq(github_user.id as i32)) - .one(&db) + .one(&data.pool) .await .unwrap() .unwrap(); - db.close().await.unwrap(); - let jwt = JWTAuth::new(user).encode(*KEY.clone()); HttpResponse::Ok().json(json!({ "jwt": jwt }))