You've already forked AstralRinth
forked from didirus/AstralRinth
Switch to Postgres (#39)
* WIP Switch to Postgres * feat(postgres): more work on porting to postgres, now compiles * feat(docker-compose): Changed the docker-compose.yml file to use postgres. * Update docker, documentation, gh actions... * Remove bson dependency * Remove bson import * feat: move mock filehost to trait rather than cargo feature * feat(postgres): transactions for mod creation, multipart refactor * fix: Add Cargo.lock so that sqlx functions * Update sqlx offline build data * fix: Use SQLX_OFFLINE to force sqlx into offline mode for CI * Default release channels * feat(postgres): refactor database models to fit postgres models * fix: Fix sqlx prepare, fix double allocation in indexing * Add dockerfile (#40) Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com> Co-authored-by: Aeledfyr <aeledfyr@gmail.com> Co-authored-by: redblueflame <contact@redblueflame.com> Co-authored-by: Jai A <jai.a@tuta.io> Co-authored-by: Valentin Ricard <redblueflame1@gmail.Com> Co-authored-by: Charalampos Fanoulis <charalampos.fanoulis@gmail.com>
This commit is contained in:
62
src/main.rs
62
src/main.rs
@@ -5,6 +5,7 @@ use env_logger::Env;
|
||||
use log::info;
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::sync::Arc;
|
||||
|
||||
mod database;
|
||||
mod file_hosting;
|
||||
@@ -19,54 +20,57 @@ async fn main() -> std::io::Result<()> {
|
||||
|
||||
check_env_vars();
|
||||
|
||||
//Database Connecter
|
||||
let client = database::connect()
|
||||
// Database Connector
|
||||
let pool = database::connect()
|
||||
.await
|
||||
.expect("Database connection failed");
|
||||
let client_ref = client.clone();
|
||||
let client_ref = pool.clone();
|
||||
|
||||
//File Hosting Initializer
|
||||
let authorization_data = file_hosting::authorize_account(
|
||||
dotenv::var("BACKBLAZE_KEY_ID").unwrap(),
|
||||
dotenv::var("BACKBLAZE_KEY").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let upload_url_data = file_hosting::get_upload_url(
|
||||
authorization_data.clone(),
|
||||
dotenv::var("BACKBLAZE_BUCKET_ID").unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
let backblaze_enabled = dotenv::var("BACKBLAZE_ENABLED")
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<bool>().ok())
|
||||
.unwrap_or(false);
|
||||
|
||||
let file_host: Arc<dyn file_hosting::FileHost + Send + Sync> = if backblaze_enabled {
|
||||
Arc::new(
|
||||
file_hosting::BackblazeHost::new(
|
||||
&dotenv::var("BACKBLAZE_KEY_ID").unwrap(),
|
||||
&dotenv::var("BACKBLAZE_KEY").unwrap(),
|
||||
&dotenv::var("BACKBLAZE_BUCKET_ID").unwrap(),
|
||||
)
|
||||
.await,
|
||||
)
|
||||
} else {
|
||||
Arc::new(file_hosting::MockHost::new())
|
||||
};
|
||||
|
||||
// Get executable path
|
||||
let mut exe_path = env::current_exe()?.parent().unwrap().to_path_buf();
|
||||
// Create the path to the index lock file
|
||||
exe_path.push("index.v1.lock");
|
||||
|
||||
//Indexing mods if not already done
|
||||
// Indexing mods if not already done
|
||||
if env::args().any(|x| x == "regen") {
|
||||
// User forced regen of indexing
|
||||
info!("Forced regeneration of indexes!");
|
||||
index_mods(client).await.expect("Mod indexing failed");
|
||||
index_mods(pool).await.expect("Mod indexing failed");
|
||||
} else if !exe_path.exists() {
|
||||
// The indexes were not created, or the version was upgraded
|
||||
info!("Indexing of mods for first time...");
|
||||
index_mods(client).await.expect("Mod indexing failed");
|
||||
index_mods(pool).await.expect("Mod indexing failed");
|
||||
// Create the lock file
|
||||
File::create(exe_path)?;
|
||||
}
|
||||
|
||||
info!("Starting Actix HTTP server!");
|
||||
|
||||
//Init App
|
||||
// Init App
|
||||
HttpServer::new(move || {
|
||||
App::new()
|
||||
.wrap(Logger::default())
|
||||
.wrap(Logger::new("%a %{User-Agent}i"))
|
||||
.data(client_ref.clone())
|
||||
.data(authorization_data.clone())
|
||||
.data(upload_url_data.clone())
|
||||
.data(file_host.clone())
|
||||
.service(routes::index_get)
|
||||
.service(routes::mod_search)
|
||||
.service(routes::mod_create)
|
||||
@@ -93,11 +97,17 @@ fn check_env_vars() {
|
||||
}
|
||||
}
|
||||
check_var::<bool>("INDEX_CURSEFORGE");
|
||||
check_var::<String>("MONGODB_ADDR");
|
||||
check_var::<String>("DATABASE_URL");
|
||||
check_var::<String>("MEILISEARCH_ADDR");
|
||||
check_var::<String>("BIND_ADDR");
|
||||
|
||||
check_var::<String>("BACKBLAZE_KEY_ID");
|
||||
check_var::<String>("BACKBLAZE_KEY");
|
||||
check_var::<String>("BACKBLAZE_BUCKET_ID");
|
||||
if dotenv::var("BACKBLAZE_ENABLED")
|
||||
.ok()
|
||||
.and_then(|s| s.parse::<bool>().ok())
|
||||
.unwrap_or(false)
|
||||
{
|
||||
check_var::<String>("BACKBLAZE_KEY_ID");
|
||||
check_var::<String>("BACKBLAZE_KEY");
|
||||
check_var::<String>("BACKBLAZE_BUCKET_ID");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user