2023-10-06 18:29:55 +02:00
|
|
|
use std::path::Path;
|
|
|
|
|
2024-12-16 21:16:02 +01:00
|
|
|
use library::user::UserManager;
|
2024-10-05 01:21:43 +02:00
|
|
|
use rocket::{http::Method, routes};
|
2024-10-07 09:24:54 +02:00
|
|
|
use tokio::sync::OnceCell;
|
2024-10-05 01:21:43 +02:00
|
|
|
|
2023-10-06 18:29:55 +02:00
|
|
|
mod library;
|
|
|
|
mod pages;
|
|
|
|
mod yt_meta;
|
|
|
|
|
2024-10-07 09:24:54 +02:00
|
|
|
pub static PG: OnceCell<sqlx::PgPool> = OnceCell::const_new();
|
2024-09-15 04:09:46 +02:00
|
|
|
|
2024-10-07 09:24:54 +02:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! get_pg {
|
|
|
|
() => {
|
|
|
|
if let Some(client) = $crate::PG.get() {
|
|
|
|
client
|
|
|
|
} else {
|
|
|
|
let client = sqlx::postgres::PgPoolOptions::new()
|
|
|
|
.max_connections(5)
|
|
|
|
.connect(&std::env::var("DATABASE_URL").unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
$crate::PG.set(client).unwrap();
|
|
|
|
$crate::PG.get().unwrap()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2024-10-05 01:21:43 +02:00
|
|
|
|
|
|
|
#[rocket::launch]
|
|
|
|
async fn launch() -> _ {
|
2023-10-06 18:29:55 +02:00
|
|
|
std::env::set_var("RUST_LOG", "info");
|
|
|
|
std::env::set_var("RUST_BACKTRACE", "1");
|
|
|
|
env_logger::init();
|
|
|
|
|
|
|
|
let args: Vec<String> = std::env::args().collect();
|
|
|
|
|
|
|
|
if args.len() != 2 {
|
|
|
|
eprintln!("Usage: {} <directory_path>", args[0]);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
let dir_path = args[1].clone();
|
|
|
|
|
2024-10-07 09:24:54 +02:00
|
|
|
let pg = get_pg!();
|
|
|
|
|
|
|
|
sqlx::migrate!("./migrations").run(pg).await.unwrap();
|
|
|
|
|
2024-10-05 01:21:43 +02:00
|
|
|
let lib = library::Library::new().await;
|
2024-12-16 21:16:02 +01:00
|
|
|
let um = UserManager::new(pg.clone());
|
|
|
|
|
|
|
|
um.create("admin", "admin", library::user::UserRole::Admin)
|
|
|
|
.await;
|
2023-10-06 18:29:55 +02:00
|
|
|
|
2024-10-05 01:21:43 +02:00
|
|
|
let library = lib.clone();
|
2023-10-06 18:29:55 +02:00
|
|
|
|
2024-12-14 21:50:49 +01:00
|
|
|
let handle = tokio::spawn(async move {
|
|
|
|
library
|
|
|
|
.scan_dir(&Path::new(&dir_path.clone()).to_path_buf())
|
|
|
|
.await;
|
|
|
|
});
|
2023-10-06 18:29:55 +02:00
|
|
|
|
2024-10-05 01:21:43 +02:00
|
|
|
let cors = rocket_cors::CorsOptions {
|
|
|
|
allowed_origins: rocket_cors::AllowedOrigins::all(),
|
|
|
|
allowed_methods: vec![Method::Get, Method::Post, Method::Options]
|
|
|
|
.into_iter()
|
|
|
|
.map(From::from)
|
|
|
|
.collect(),
|
|
|
|
allowed_headers: rocket_cors::AllowedHeaders::all(),
|
|
|
|
allow_credentials: true,
|
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
.to_cors()
|
|
|
|
.expect("error creating CORS options");
|
|
|
|
|
|
|
|
rocket::build()
|
2024-10-07 09:24:54 +02:00
|
|
|
.mount(
|
|
|
|
"/",
|
|
|
|
routes![
|
|
|
|
pages::assets::video_file,
|
|
|
|
pages::assets::video_thumbnail,
|
2024-12-15 00:31:39 +01:00
|
|
|
pages::assets::fav_icon,
|
2024-10-07 09:24:54 +02:00
|
|
|
pages::index::search,
|
|
|
|
pages::index::channel_page,
|
|
|
|
pages::yt::yt_tags,
|
|
|
|
pages::yt::yt_tag_page,
|
2024-12-14 00:24:10 +01:00
|
|
|
pages::yt::yt_channel_page,
|
|
|
|
pages::index::index_page,
|
2024-12-16 21:16:02 +01:00
|
|
|
pages::watch::watch_page,
|
|
|
|
pages::user::login,
|
|
|
|
pages::user::login_post
|
2024-10-07 09:24:54 +02:00
|
|
|
],
|
|
|
|
)
|
2024-10-05 01:21:43 +02:00
|
|
|
.attach(cors)
|
|
|
|
.manage(lib)
|
2024-12-16 21:16:02 +01:00
|
|
|
.manage(um)
|
2023-10-06 18:29:55 +02:00
|
|
|
}
|