2020-06-30 19:23:52 +02:00
|
|
|
use crate::search::indexing::index_mods;
|
2020-05-28 13:28:58 -07:00
|
|
|
use actix_web::middleware::Logger;
|
2020-06-30 19:23:52 +02:00
|
|
|
use actix_web::{web, App, HttpServer};
|
2020-05-28 13:28:58 -07:00
|
|
|
use env_logger::Env;
|
2020-06-30 19:23:52 +02:00
|
|
|
use log::info;
|
2020-06-22 21:04:17 -07:00
|
|
|
use std::env;
|
2020-06-30 19:23:52 +02:00
|
|
|
use std::fs::File;
|
2020-05-10 22:30:28 -07:00
|
|
|
|
2020-05-14 22:53:22 -07:00
|
|
|
mod database;
|
2020-07-02 05:00:04 -07:00
|
|
|
mod file_hosting;
|
2020-06-30 19:23:52 +02:00
|
|
|
mod models;
|
2020-05-27 20:51:28 +02:00
|
|
|
mod routes;
|
2020-06-30 19:23:52 +02:00
|
|
|
mod search;
|
2020-05-09 22:11:48 -07:00
|
|
|
|
|
|
|
|
#[actix_rt::main]
|
2020-05-10 22:30:28 -07:00
|
|
|
async fn main() -> std::io::Result<()> {
|
2020-05-28 13:28:58 -07:00
|
|
|
env_logger::from_env(Env::default().default_filter_or("info")).init();
|
2020-06-22 21:04:17 -07:00
|
|
|
dotenv::dotenv().ok();
|
2020-05-28 13:28:58 -07:00
|
|
|
|
2020-07-03 12:44:39 -05:00
|
|
|
check_env_vars();
|
|
|
|
|
|
|
|
|
|
let client = database::connect()
|
|
|
|
|
.await
|
|
|
|
|
.expect("Database connection failed");
|
2020-06-30 19:23:52 +02:00
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
if env::args().any(|x| x == "regen") {
|
|
|
|
|
// User forced regen of indexing
|
|
|
|
|
info!("Forced regeneration of indexes!");
|
2020-07-03 12:44:39 -05:00
|
|
|
index_mods(client).await.expect("Mod indexing failed");
|
|
|
|
|
} else if !exe_path.exists() {
|
2020-06-30 19:23:52 +02:00
|
|
|
// The indexes were not created, or the version was upgraded
|
|
|
|
|
info!("Indexing of mods for first time...");
|
2020-07-03 12:44:39 -05:00
|
|
|
index_mods(client).await.expect("Mod indexing failed");
|
2020-06-30 19:23:52 +02:00
|
|
|
// Create the lock file
|
|
|
|
|
File::create(exe_path)?;
|
|
|
|
|
}
|
2020-05-09 22:11:48 -07:00
|
|
|
|
2020-05-28 13:28:58 -07:00
|
|
|
info!("Starting Actix HTTP server!");
|
2020-06-22 21:04:17 -07:00
|
|
|
|
2020-05-09 22:11:48 -07:00
|
|
|
//Init App
|
|
|
|
|
HttpServer::new(move || {
|
|
|
|
|
App::new()
|
2020-05-28 13:28:58 -07:00
|
|
|
.wrap(Logger::default())
|
|
|
|
|
.wrap(Logger::new("%a %{User-Agent}i"))
|
2020-05-14 22:53:22 -07:00
|
|
|
.service(routes::index_get)
|
2020-06-30 19:23:52 +02:00
|
|
|
.service(routes::mod_search)
|
|
|
|
|
.default_service(web::get().to(routes::not_found))
|
2020-05-09 22:11:48 -07:00
|
|
|
})
|
2020-07-03 12:44:39 -05:00
|
|
|
.bind(dotenv::var("BIND_ADDR").unwrap())?
|
2020-05-27 20:51:28 +02:00
|
|
|
.run()
|
|
|
|
|
.await
|
2020-05-09 22:11:48 -07:00
|
|
|
}
|
2020-07-03 12:44:39 -05:00
|
|
|
|
|
|
|
|
// This is so that env vars not used immediately don't panic at runtime
|
|
|
|
|
fn check_env_vars() {
|
|
|
|
|
fn check_var<T: std::str::FromStr>(var: &str) {
|
|
|
|
|
if dotenv::var(var)
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(|s| s.parse::<T>().ok())
|
|
|
|
|
.is_none()
|
|
|
|
|
{
|
|
|
|
|
log::warn!(
|
|
|
|
|
"Variable `{}` missing in dotenv or not of type `{}`",
|
|
|
|
|
var,
|
|
|
|
|
std::any::type_name::<T>()
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
check_var::<bool>("INDEX_CURSEFORGE");
|
|
|
|
|
check_var::<String>("MONGODB_ADDR");
|
|
|
|
|
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");
|
|
|
|
|
}
|