aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs54
1 files changed, 45 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 2f1a00b..de7bc73 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -15,6 +15,7 @@ mod utils;
use std::{collections::HashSet, time::Duration};
use poem::http::StatusCode;
+use poem::i18n::{I18NResources, Locale};
use poem::{
endpoint::EmbeddedFilesEndpoint,
get, handler,
@@ -44,7 +45,7 @@ type Result<T> = std::result::Result<T, BlossomError>;
struct Static;
#[handler]
-async fn home(Data(reqwest): Data<&reqwest::Client>) -> templates::Home {
+async fn home(Data(reqwest): Data<&reqwest::Client>, locale: Locale) -> templates::Home {
let listenbrainz_client = listenbrainz::raw::Client::new();
let (live, listenbrainz, blogposts, poems) = tokio::join!(
live::get_live_status(reqwest),
@@ -59,20 +60,27 @@ async fn home(Data(reqwest): Data<&reqwest::Client>) -> templates::Home {
let poems = poems.unwrap_or_default();
let poem = poems.choose(&mut rand::thread_rng()).cloned();
templates::Home {
+ title: locale.text("title").unwrap(),
is_live,
listenbrainz,
blogposts,
poem,
+ locale,
}
}
// #[get("/blog/<blogpost>")]
#[handler]
-async fn blogpost(Path(blogpost): Path<String>) -> Result<templates::Blogpost> {
+async fn blogpost(Path(blogpost): Path<String>, locale: Locale) -> Result<templates::Blogpost> {
let blogpost = Blogpost::get_article(&blogpost).await?;
Ok(templates::Blogpost {
+ title: blogpost
+ .subject()
+ .unwrap_or(locale.text("untitled").unwrap().as_str())
+ .to_owned(),
blogpost,
filter_tags: HashSet::new(),
+ locale,
})
}
@@ -83,7 +91,10 @@ struct FilterTags {
// #[get("/blog?<filter>")]
#[handler]
-async fn get_blog(filter_tags: Option<Query<FilterTags>>) -> Result<templates::Blog> {
+async fn get_blog(
+ filter_tags: Option<Query<FilterTags>>,
+ locale: Locale,
+) -> Result<templates::Blog> {
let mut blogposts = Blogpost::get_articles().await?;
let tags: Vec<String> = posts::Post::get_tags(&blogposts)
.into_iter()
@@ -95,12 +106,15 @@ async fn get_blog(filter_tags: Option<Query<FilterTags>>) -> Result<templates::B
blogposts = posts::Post::filter_by_tags(blogposts, &filter_hashset);
}
Ok(templates::Blog {
+ title: locale.text("title-blog").unwrap(),
blogposts,
tags,
filter_tags: filter_hashset,
+ locale,
})
}
+// TODO: localize feed
#[handler]
async fn feed() -> Result<Response> {
let blogposts: Vec<Box<dyn Post + Send + Sync>> = Blogpost::get_articles()
@@ -132,8 +146,11 @@ async fn feed() -> Result<Response> {
}
#[handler]
-async fn contact() -> templates::Contact {
- templates::Contact
+async fn contact(locale: Locale) -> templates::Contact {
+ templates::Contact {
+ title: locale.text("title-contact").unwrap(),
+ locale,
+ }
}
#[handler]
@@ -142,21 +159,34 @@ async fn plants() -> Result<()> {
}
#[handler]
-async fn get_poem(Path(poem): Path<String>) -> Result<templates::Poem> {
+async fn get_poem(Path(poem): Path<String>, locale: Locale) -> Result<templates::Poem> {
let poem = Poem::get_article(&poem).await?;
- Ok(templates::Poem { poem, jiggle: 4 })
+ Ok(templates::Poem {
+ title: (&poem.title)
+ .clone()
+ .unwrap_or(locale.text("untitled").unwrap()),
+ poem,
+ jiggle: 4,
+ locale,
+ })
}
#[handler]
-async fn get_poetry() -> Result<templates::Poetry> {
+async fn get_poetry(locale: Locale) -> Result<templates::Poetry> {
let mut poems = Poem::get_articles().await?;
poems.sort_by_key(|poem| poem.created_at);
poems.reverse();
- Ok(templates::Poetry { poems, jiggle: 16 })
+ Ok(templates::Poetry {
+ title: locale.text("title-poetry").unwrap(),
+ poems,
+ jiggle: 16,
+ locale,
+ })
}
async fn custom_error(err: poem::Error) -> impl IntoResponse {
templates::Error {
+ title: err.to_string(),
status: err.status(),
message: err.to_string(),
}
@@ -171,6 +201,11 @@ async fn main() -> std::result::Result<(), std::io::Error> {
// let mut skinny_data = mastodon_async::Data::default();
// skinny_data.base = Cow::from("https://skinnyver.se");
+ let resources = I18NResources::builder()
+ .add_path("./resources")
+ .build()
+ .unwrap();
+
let blossom = Route::new()
.at("/", get(home))
.at("/blog", get(get_blog))
@@ -182,6 +217,7 @@ async fn main() -> std::result::Result<(), std::io::Error> {
.at("/plants", get(plants))
.nest("/static/", EmbeddedFilesEndpoint::<Static>::new())
.catch_all_error(custom_error)
+ .data(resources)
.with(Tracing)
.with(AddData::new(
reqwest::Client::builder()