watchdogs/src/main.rs

76 lines
2 KiB
Rust
Raw Normal View History

2024-12-18 19:24:38 +01:00
use based::{auth::User, get_pg};
2024-10-05 01:21:43 +02:00
use rocket::{http::Method, routes};
2024-12-18 19:24:38 +01:00
use std::path::Path;
2023-10-06 18:29:55 +02:00
mod library;
2024-12-16 23:45:15 +01:00
mod meta;
2023-10-06 18:29:55 +02:00
mod pages;
mod yt_meta;
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
2024-12-18 19:24:38 +01:00
User::create("admin", "admin", based::auth::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![
2024-12-22 19:48:45 +01:00
based::htmx::htmx_script_route,
2024-10-07 09:24:54 +02:00
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,
2024-12-22 20:19:52 +01:00
pages::user::login_post,
pages::user::history_page
2024-10-07 09:24:54 +02:00
],
)
2024-10-05 01:21:43 +02:00
.attach(cors)
.manage(lib)
2023-10-06 18:29:55 +02:00
}