achubb_website/src/html/root.rs

116 lines
3 KiB
Rust
Raw Normal View History

2024-04-28 11:02:16 -04:00
use axum::{
response::{IntoResponse, Redirect},
routing::{get, Router},
Extension,
2024-04-28 11:02:16 -04:00
};
use rand::{seq::SliceRandom, thread_rng};
use sqlx::PgPool;
use std::error::Error;
2024-03-24 10:45:16 -04:00
use tower_http::services::ServeDir;
use crate::database::{
link::{Link, LinkType},
PsqlData,
};
use super::{
blog, garden,
templates::{
AboutTemplate, AiTemplate, BlogrollTemplate, ContactTemplate, HomeTemplate, HtmlTemplate, InterestsTemplate, LinksPageTemplate, NowTemplate, UsesTemplate
},
AppState,
};
pub fn get_router(pool: PgPool) -> Router {
2024-03-24 10:45:16 -04:00
let assets_path = std::env::current_dir().unwrap();
let state = AppState { db: pool };
2024-03-24 10:45:16 -04:00
Router::new()
.nest("/blog", blog::get_router())
.nest("/garden", garden::get_router())
.nest_service(
"/assets",
ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),
)
2024-03-24 10:45:16 -04:00
.route("/", get(home))
.route("/now", get(now))
.route("/about", get(about))
.route("/contact", get(contact))
2024-07-05 20:00:43 -04:00
.route("/uses", get(uses))
2024-08-09 16:22:14 -04:00
.route("/ai", get(ai))
2024-08-09 21:42:46 -04:00
.route("/blogroll", get(blogroll))
.route("/links", get(links))
.route("/interests", get(interests))
2024-04-28 11:02:16 -04:00
.route(
"/robots.txt",
get(|| async { Redirect::permanent("/assets/robots.txt") }),
)
.layer(Extension(state))
2024-03-24 10:45:16 -04:00
}
async fn home() -> impl IntoResponse {
HtmlTemplate(HomeTemplate {})
}
2024-03-24 10:45:16 -04:00
async fn now() -> impl IntoResponse {
HtmlTemplate(NowTemplate {})
2024-03-24 10:45:16 -04:00
}
async fn about() -> impl IntoResponse {
HtmlTemplate(AboutTemplate {})
2024-03-24 10:45:16 -04:00
}
async fn contact() -> impl IntoResponse {
HtmlTemplate(ContactTemplate {})
2024-03-24 10:45:16 -04:00
}
async fn uses() -> impl IntoResponse {
HtmlTemplate(UsesTemplate {})
2024-07-05 20:00:43 -04:00
}
2024-08-09 16:22:14 -04:00
async fn ai() -> impl IntoResponse {
HtmlTemplate(AiTemplate {})
2024-08-09 16:22:14 -04:00
}
2024-08-09 21:42:46 -04:00
async fn interests() -> impl IntoResponse {
HtmlTemplate(InterestsTemplate {})
}
async fn blogroll(state: Extension<AppState>) -> impl IntoResponse {
let blogroll_page = BlogrollTemplate {
blogs: match get_links_as_list(&state.db, LinkType::BLOG).await {
Ok(list) => list,
Err(_) => Vec::new(),
},
};
HtmlTemplate(blogroll_page)
2024-08-09 21:42:46 -04:00
}
async fn links(state: Extension<AppState>) -> impl IntoResponse {
let links_page = LinksPageTemplate {
articles: match get_links_as_list(&state.db, LinkType::ARTICLE).await {
Ok(list) => list,
Err(_) => Vec::new(),
},
};
HtmlTemplate(links_page)
}
pub async fn get_links_as_list(
pool: &PgPool,
link_type: LinkType,
) -> Result<Vec<Link>, Box<dyn Error>> {
let mut links: Vec<Link> = match Link::read_all(pool).await {
Ok(a) => a.iter().map(|x| *x.clone()).collect(),
Err(_) => Vec::new(),
};
let mut rng = thread_rng();
links.shuffle(&mut rng);
let list: Vec<Link> = links
.into_iter()
.filter(|link| link.link_type == link_type)
.collect();
Ok(list)
}