2024-04-28 11:02:16 -04:00
|
|
|
use axum::{
|
|
|
|
|
response::{IntoResponse, Redirect},
|
|
|
|
|
routing::{get, Router},
|
2024-04-28 20:23:19 -04:00
|
|
|
Extension,
|
2024-04-28 11:02:16 -04:00
|
|
|
};
|
2024-09-04 07:47:36 -04:00
|
|
|
use rand::{seq::SliceRandom, thread_rng};
|
2024-04-28 20:23:19 -04:00
|
|
|
use sqlx::PgPool;
|
2024-09-04 07:47:36 -04:00
|
|
|
use std::error::Error;
|
2024-03-24 10:45:16 -04:00
|
|
|
use tower_http::services::ServeDir;
|
|
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
use crate::database::{
|
|
|
|
|
link::{Link, LinkType},
|
|
|
|
|
PsqlData,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{
|
|
|
|
|
blog, garden,
|
|
|
|
|
templates::{
|
|
|
|
|
AboutTemplate, AiTemplate, BlogrollTemplate, ContactTemplate, HomeTemplate, HtmlTemplate,
|
|
|
|
|
LinksPageTemplate, NowTemplate, UsesTemplate,
|
|
|
|
|
},
|
|
|
|
|
AppState,
|
|
|
|
|
};
|
2024-04-28 20:23:19 -04:00
|
|
|
|
|
|
|
|
pub fn get_router(pool: PgPool) -> Router {
|
2024-03-24 10:45:16 -04:00
|
|
|
let assets_path = std::env::current_dir().unwrap();
|
2024-04-28 20:23:19 -04:00
|
|
|
let state = AppState { db: pool };
|
2024-03-24 10:45:16 -04:00
|
|
|
Router::new()
|
2024-04-28 20:23:19 -04:00
|
|
|
.nest("/blog", blog::get_router())
|
2024-07-27 13:20:31 -04:00
|
|
|
.nest("/garden", garden::get_router())
|
2024-04-28 20:23:19 -04:00
|
|
|
.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))
|
2024-08-14 07:54:13 -04:00
|
|
|
.route("/links", get(links))
|
2024-04-28 11:02:16 -04:00
|
|
|
.route(
|
|
|
|
|
"/robots.txt",
|
|
|
|
|
get(|| async { Redirect::permanent("/assets/robots.txt") }),
|
|
|
|
|
)
|
2024-04-28 20:23:19 -04:00
|
|
|
.layer(Extension(state))
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
async fn home() -> impl IntoResponse {
|
|
|
|
|
HtmlTemplate(HomeTemplate {})
|
2024-03-24 15:12:36 -04:00
|
|
|
}
|
2024-03-24 10:45:16 -04:00
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
async fn now() -> impl IntoResponse {
|
|
|
|
|
HtmlTemplate(NowTemplate {})
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
async fn about() -> impl IntoResponse {
|
|
|
|
|
HtmlTemplate(AboutTemplate {})
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
async fn contact() -> impl IntoResponse {
|
|
|
|
|
HtmlTemplate(ContactTemplate {})
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-04 07:47:36 -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
|
|
|
|
2024-09-04 07:47:36 -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
|
|
|
|
2024-09-04 07:47:36 -04:00
|
|
|
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
|
|
|
}
|
2024-08-14 07:54:13 -04:00
|
|
|
|
2024-09-04 07:47:36 -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)
|
2024-08-14 07:54:13 -04:00
|
|
|
}
|