From f00159f53b3774601500ec65345791311ff6efa1 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 30 Jan 2024 16:16:05 +0000 Subject: migrate to poem and askama --- src/main.rs | 212 ++++++++++++++++++++++++++++-------------------------------- 1 file changed, 98 insertions(+), 114 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ed768b4..c38b2eb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,155 +1,139 @@ +mod atom; +mod blog; mod error; mod live; mod posts; mod scrobbles; mod skweets; - -use std::borrow::Cow; -use std::collections::HashSet; -use std::time::Duration; - -use atom_syndication::Feed; -use rocket::fs::{relative, FileServer}; -use rocket::http::{ContentType, Status}; -use rocket::{Request, State}; -use rocket_dyn_templates::{context, Template}; +mod templates; +mod utils; + +use std::rc::Rc; +use std::{collections::HashSet, time::Duration}; + +use poem::http::StatusCode; +use poem::Response; +use poem::{ + endpoint::EmbeddedFilesEndpoint, + get, handler, + listener::TcpListener, + middleware::AddData, + web::{Data, Path, Query}, + EndpointExt, Route, Server, +}; +use rust_embed::RustEmbed; use error::BlossomError; +use serde::Deserialize; type Result = std::result::Result; -struct Clients { - listenbrainz: listenbrainz::raw::Client, - skinnyverse: mastodon_async::Mastodon, - reqwest: reqwest::Client, -} - -#[macro_use] -extern crate rocket; +#[derive(RustEmbed)] +#[folder = "static/"] +struct Static; -#[get("/")] -async fn home(clients: &State) -> Template { +#[handler] +async fn home(Data(reqwest): Data<&reqwest::Client>) -> templates::Home { + let listenbrainz_client = listenbrainz::raw::Client::new(); let (live, listenbrainz, blogposts) = tokio::join!( - live::get_live_status(&clients.reqwest), - scrobbles::get_now_playing(&clients.listenbrainz), + live::get_live_status(reqwest), + scrobbles::get_now_playing(&listenbrainz_client), // skweets::get_recents(&clients.skinnyverse), - posts::get_blogposts() + blog::get_blogposts() ); let is_live = live.unwrap_or_default().online; let listenbrainz = listenbrainz.unwrap_or_default(); let blogposts = blogposts.unwrap_or_default(); - Template::render( - "home", - context! { - is_live, - listenbrainz, - // skweets, - blogposts, - }, - ) + templates::Home { + is_live, + listenbrainz, + blogposts, + } +} + +// #[get("/blog/")] +#[handler] +async fn blogpost(Path(blogpost): Path) -> Result { + let blogpost = blog::get_blogpost(&blogpost).await?; + Ok(templates::Blogpost { + blogpost, + filter_tags: HashSet::new(), + }) } -#[get("/blog/")] -async fn blogpost(blogpost: &str) -> Result