Skip to content

Commit

Permalink
fix: persist db conn
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Jul 29, 2023
1 parent b5cdf05 commit 8af6925
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
6 changes: 2 additions & 4 deletions src/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,18 @@ use crate::Database;

#[get("/cdn/{slug}@{version}")]
async fn cdn_get(db: web::Data<Database>, 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();

if let Some(db_mod) = db_mod {
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();

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,8 @@ lazy_static::lazy_static! {

#[get("/")]
async fn index(data: web::Data<Database>) -> 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("<!DOCTYPE html><html><body style=\"background-color: #18181b; color: #ffffff\">");
Expand Down Expand Up @@ -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);

Expand Down
14 changes: 6 additions & 8 deletions src/mods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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::<Vec<_>>()[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");
}
Expand All @@ -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();

Expand All @@ -224,15 +222,15 @@ pub async fn create_mod(
} else {
Categories::find()
.filter(entity::categories::Column::Name.eq("other"))
.one(&db)
.one(&db.pool)
.await
.unwrap()
.unwrap()
};

let v_req = forgemod.manifest.game_version.clone();
let vers = BeatSaberVersions::find()
.all(&db)
.all(&db.pool)
.await
.unwrap()
.into_iter()
Expand All @@ -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;
Expand Down
10 changes: 3 additions & 7 deletions src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,6 @@ pub async fn user_auth(
data: web::Data<Database>,
info: web::Query<UserAuthReq>,
) -> impl Responder {
let db = data.pool.clone();

let code = &info.code;

let gat = minreq::post("https://github.com/login/oauth/access_token")
Expand Down Expand Up @@ -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();

Expand All @@ -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 }))
Expand Down

0 comments on commit 8af6925

Please sign in to comment.