diff --git a/Cargo.toml b/Cargo.toml index ebe060d..464b811 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ repository = "https://github.com/YGGverse/yggtrackerd" clap = { version = "4.5", features = ["derive"] } rocket = "0.5" librqbit-core = "5.0" -chrono = "0.4.41" -url = "2.5" -urlencoding = "2.1" \ No newline at end of file +chrono = { version = "0.4.41", features = ["serde"] } +url = { version = "2.5", features = ["serde"] } +urlencoding = "2.1" +rocket_dyn_templates = { version = "0.2", features = ["tera"] } diff --git a/src/config.rs b/src/config.rs index dfb4520..dfd3215 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,6 +32,10 @@ pub struct Config { #[arg(long)] pub link: Option, + /// Optional reference to the [Aquatic](https://github.com/greatest-ape/aquatic) stats page + #[arg(long)] + pub stats: Option, + /// Appends following tracker(s) to the magnet links #[arg(long)] pub tracker: Option>, diff --git a/src/main.rs b/src/main.rs index 0bea458..31d5ba6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,16 +12,40 @@ use rocket::{ State, http::Status, response::{content::RawXml, status::Custom}, + serde::Serialize, }; +use rocket_dyn_templates::{Template, context}; use storage::{Order, Sort, Storage}; +use url::Url; + +#[derive(Clone, Debug, Serialize)] +#[serde(crate = "rocket::serde")] +pub struct Meta { + pub canonical: Option, + pub description: Option, + pub stats: Option, + pub title: String, + pub trackers: Option>, +} #[get("/")] -pub fn index() -> &'static str { - "Catalog in development, use /rss" +fn index(storage: &State, meta: &State) -> Result> { + Ok(Template::render( + "index", + context! { + meta: meta.inner(), + torrents: storage + .torrents( + Some((Sort::Modified, Order::Asc)), + Some(storage.default_limit), + ) + .map_err(|e| Custom(Status::InternalServerError, e.to_string()))? + }, + )) } #[get("/rss")] -pub fn rss(feed: &State, storage: &State) -> Result, Custom> { +fn rss(feed: &State, storage: &State) -> Result, Custom> { let mut b = feed.transaction(1024); // @TODO for torrent in storage .torrents( @@ -40,13 +64,14 @@ fn rocket() -> _ { use clap::Parser; let config = Config::parse(); let feed = Feed::init( - config.title, - config.description, - config.link, - config.tracker.map(|u| u.into_iter().collect()), // make sure it's unique + config.title.clone(), + config.description.clone(), + config.link.clone(), + config.tracker.clone().map(|u| u.into_iter().collect()), // make sure it's unique ); let storage = Storage::init(config.storage, config.limit, config.capacity).unwrap(); // @TODO handle rocket::build() + .attach(Template::fairing()) .configure(rocket::Config { port: config.port, address: config.address, @@ -54,5 +79,12 @@ fn rocket() -> _ { }) .manage(feed) .manage(storage) + .manage(Meta { + canonical: config.link, + description: config.description, + stats: config.stats, + title: config.title, + trackers: config.tracker, + }) .mount("/", routes![index, rss]) } diff --git a/src/storage.rs b/src/storage.rs index 61e9317..4826374 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -1,5 +1,6 @@ use chrono::{DateTime, Utc}; use librqbit_core::{torrent_metainfo, torrent_metainfo::TorrentMetaV1Owned}; +use rocket::serde::Serialize; use std::{ fs::{self, DirEntry}, path::PathBuf, @@ -15,14 +16,18 @@ pub enum Sort { pub enum Order { #[default] Asc, - Desc, + //Desc, } +#[derive(Clone, Debug, Serialize)] +#[serde(crate = "rocket::serde")] pub struct File { pub name: Option, pub length: u64, } +#[derive(Clone, Debug, Serialize)] +#[serde(crate = "rocket::serde")] pub struct Torrent { pub announce: Option, pub comment: Option, @@ -171,7 +176,7 @@ impl Storage { match sort { Sort::Modified => match order { Order::Asc => b.sort_by(|a, b| a.0.cmp(&b.0)), - Order::Desc => b.sort_by(|a, b| b.0.cmp(&a.0)), + //Order::Desc => b.sort_by(|a, b| b.0.cmp(&a.0)), }, } } diff --git a/templates/index.html.tera b/templates/index.html.tera new file mode 100644 index 0000000..b679e02 --- /dev/null +++ b/templates/index.html.tera @@ -0,0 +1,9 @@ +{% extends "layout/default" %} +{% block content %} +{% for torrent in torrents %} +
+ +

{{ torrent.name }}

+
+{% endfor %} +{% endblock content %} \ No newline at end of file diff --git a/templates/layout/default.html.tera b/templates/layout/default.html.tera new file mode 100644 index 0000000..4ab570c --- /dev/null +++ b/templates/layout/default.html.tera @@ -0,0 +1,103 @@ + + + + + {{ meta.title }} + {% if meta.description %} + + {% endif %} + + + +
+ +
+
+ {% block content %}{% endblock content %} +
+ + + \ No newline at end of file