Skip to content

Commit

Permalink
refactor: remove weirdly formated match statements
Browse files Browse the repository at this point in the history
  • Loading branch information
sargon64 committed Jan 14, 2024
1 parent ab7df6c commit 6fb9c29
Show file tree
Hide file tree
Showing 7 changed files with 393 additions and 182 deletions.
2 changes: 1 addition & 1 deletion src/cdn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use forge_lib::structs::v1::{unpack_v1_forgemod, ForgeModTypes};
use poem::{handler, http::StatusCode, web::Path, IntoResponse, Response};
use serde::Deserialize;
use sqlx::PgPool;
use tracing::{warn, error};
use tracing::{error, warn};

use crate::{models, DB_POOL};

Expand Down
57 changes: 37 additions & 20 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@ use forge_lib::structs::{
};
use rand::{distributions::Alphanumeric, Rng};
use semver::{Version, VersionReq};
use tracing::{info, error};
use tracing::{error, info};

use crate::{
mods::_upload_mod,
DB_POOL, MIGRATOR, search::{get_prefix, MeiliMigrator}, MEILI_CONN,
search::{get_prefix, MeiliMigrator},
DB_POOL, MEILI_CONN, MIGRATOR,
};

pub async fn handel_debug_flags() -> anyhow::Result<()> {
let db = DB_POOL
.get()
.ok_or(anyhow::anyhow!("Failed to get DB pool"))?;

let is_fresh = !sqlx::query!(
"SELECT * FROM _sqlx_migrations"
).fetch_all(db).await.is_ok_and(|migrations| !migrations.is_empty());
let is_fresh = !sqlx::query!("SELECT * FROM _sqlx_migrations")
.fetch_all(db)
.await
.is_ok_and(|migrations| !migrations.is_empty());

if !is_fresh {
// check to see if reset flag is set.
if let Ok(reset) = std::env::var("BF_DEBUG_FULL_RESET") {
if reset == "true" {
error!("Resetting database");
sqlx::query!("DROP SCHEMA public CASCADE").execute(db).await?;
sqlx::query!("DROP SCHEMA public CASCADE")
.execute(db)
.await?;
sqlx::query!("CREATE SCHEMA public").execute(db).await?;
MIGRATOR.run(db).await?;

let meili_index = MEILI_CONN.get().ok_or(anyhow::anyhow!("Failed to get MeiliSearch client"))?.index(format!("{}mods",get_prefix()));
let meili_index = MEILI_CONN
.get()
.ok_or(anyhow::anyhow!("Failed to get MeiliSearch client"))?
.index(format!("{}mods", get_prefix()));
meili_index.delete().await?;

MeiliMigrator::new().run(db).await?;
Expand Down Expand Up @@ -82,18 +89,24 @@ pub async fn handel_debug_flags() -> anyhow::Result<()> {
pub async fn generate_user() -> anyhow::Result<()> {
let mut rng = rand::thread_rng();

let username: String = rand::thread_rng().sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect();
let email: String = rand::thread_rng().sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>() + "@example.com";
let bio = Some(rand::thread_rng().sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>());
let username: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect();
let email: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>()
+ "@example.com";
let bio = Some(
rand::thread_rng()
.sample_iter(&Alphanumeric)
.take(16)
.map(char::from)
.collect::<String>(),
);
let avatar = Some("https://http.cat/501".to_string());
let permissions = 7;
let github_id = rng.gen::<u32>() as i32;
Expand Down Expand Up @@ -152,7 +165,11 @@ async fn random_mod(slug: Option<String>) -> anyhow::Result<ForgeModV1> {
.map(char::from)
.collect(),
),
Version::new(rand::thread_rng().gen(), rand::thread_rng().gen(), rand::thread_rng().gen()),
Version::new(
rand::thread_rng().gen(),
rand::thread_rng().gen(),
rand::thread_rng().gen(),
),
VersionReq::parse(">=1.29.0")?,
)
.build(),
Expand Down
17 changes: 12 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,19 @@ lazy_static::lazy_static! {
}

#[allow(clippy::panic)]
Arc::new(Key(match match std::fs::read("./data/secret.key"){Ok(key)=>key,Err(e)=>{error!("{}",e);panic!("Failed to read secret key")},}.try_into() {
Ok(key) => key,
Err(_) => {
error!("Secret key is not 1024 bytes long, or is corrupt");
Arc::new(Key(match std::fs::read("./data/secret.key") {
Ok(key) => {
match key.try_into() {
Ok(key) => key,
Err(_) => {
panic!("Failed to read secret key")
},
}
}
Err(e) => {
error!("{}", e);
panic!("Failed to read secret key")
},
}
}))
};
}
Expand Down
Loading

0 comments on commit 6fb9c29

Please sign in to comment.