little-hesinde/rusty-library/src/handlers/opds.rs

72 lines
1.9 KiB
Rust
Raw Normal View History

2024-05-08 16:11:39 +00:00
use std::sync::Arc;
use poem::{
handler,
2024-05-09 06:39:46 +00:00
web::{Data, WithContentType},
2024-05-08 16:11:39 +00:00
IntoResponse,
};
2024-05-09 06:39:46 +00:00
use time::OffsetDateTime;
2024-05-08 16:11:39 +00:00
use crate::{
app_state::AppState,
2024-05-09 06:39:46 +00:00
handlers::error::HandlerError,
2024-05-08 16:11:39 +00:00
opds::{
author::Author, content::Content, entry::Entry, feed::Feed, link::Link,
media_type::MediaType, relation::Relation,
},
};
#[handler]
pub async fn handler(state: Data<&Arc<AppState>>) -> Result<WithContentType<String>, poem::Error> {
2024-05-09 06:39:46 +00:00
let now = OffsetDateTime::now_utc();
2024-05-08 16:11:39 +00:00
let author = Author {
name: "Thallian".to_string(),
uri: "https://code.vanwa.ch/shu/rusty-library".to_string(),
email: None,
};
let home_link = Link {
href: "/opds".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Start,
title: Some("Home".to_string()),
count: None,
};
let self_link = Link {
href: "/opds".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Myself,
title: None,
count: None,
};
let books_entry = Entry {
title: "Books".to_string(),
id: "rusty:books".to_string(),
2024-05-09 06:39:46 +00:00
updated: now,
2024-05-08 16:11:39 +00:00
content: Content {
media_type: MediaType::Text,
content: "Index of all books".to_string(),
},
links: vec![Link {
href: "/opds/books".to_string(),
media_type: MediaType::Navigation,
rel: Relation::Subsection,
title: None,
count: None,
}],
};
let feed = Feed {
title: "rusty-library".to_string(),
id: "rusty:catalog".to_string(),
2024-05-09 06:39:46 +00:00
updated: now,
2024-05-08 16:11:39 +00:00
icon: "favicon.ico".to_string(),
author,
links: vec![home_link, self_link],
entries: vec![books_entry],
};
2024-05-09 06:39:46 +00:00
let xml = feed.as_xml().map_err(HandlerError::OpdsError)?;
2024-05-08 16:11:39 +00:00
Ok(xml.with_content_type("application/atom+xml"))
}