2024-07-10 06:55:38 -04:00
|
|
|
use crate::html::{api, blog, projects, HtmlTemplate};
|
2024-03-24 10:45:16 -04:00
|
|
|
use askama::Template;
|
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-04-28 20:23:19 -04:00
|
|
|
use sqlx::PgPool;
|
2024-03-24 10:45:16 -04:00
|
|
|
use tower_http::services::ServeDir;
|
|
|
|
|
|
2024-05-17 14:55:30 -04:00
|
|
|
use super::{blog::get_articles_as_links_list, projects::get_projects_as_links_list};
|
2024-05-11 09:32:29 -04:00
|
|
|
|
2024-04-28 20:23:19 -04:00
|
|
|
#[derive(Clone)]
|
|
|
|
|
pub struct AppState {
|
|
|
|
|
pub db: PgPool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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("/api", api::get_router())
|
|
|
|
|
.nest("/blog", blog::get_router())
|
|
|
|
|
.nest("/projects", projects::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-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-05-11 09:32:29 -04:00
|
|
|
async fn home(state: Extension<AppState>) -> impl IntoResponse {
|
|
|
|
|
let db_pool = &state.db;
|
2024-05-17 14:55:30 -04:00
|
|
|
let article_list: Vec<String> = get_articles_as_links_list(db_pool)
|
2024-05-11 09:32:29 -04:00
|
|
|
.await
|
|
|
|
|
.expect("couldn't get articles");
|
|
|
|
|
|
2024-05-17 14:55:30 -04:00
|
|
|
let (article_head, _) = article_list.split_at(5);
|
|
|
|
|
|
|
|
|
|
let project_list: Vec<String> = get_projects_as_links_list(db_pool)
|
|
|
|
|
.await
|
|
|
|
|
.expect("Couldn't get projects");
|
|
|
|
|
|
|
|
|
|
let (project_head, _) = project_list.split_at(5);
|
2024-05-11 09:32:29 -04:00
|
|
|
|
2024-03-24 15:12:36 -04:00
|
|
|
let template = HomeTemplate {
|
2024-05-17 14:55:30 -04:00
|
|
|
recent_blogs: article_head.join("\n"),
|
|
|
|
|
recent_projects: project_head.join("\n"),
|
2024-04-28 11:02:16 -04:00
|
|
|
};
|
2024-04-28 20:23:19 -04:00
|
|
|
HtmlTemplate(template)
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "home.html")]
|
2024-03-24 15:12:36 -04:00
|
|
|
struct HomeTemplate {
|
2024-05-11 09:32:29 -04:00
|
|
|
recent_blogs: String,
|
2024-05-17 14:55:30 -04:00
|
|
|
recent_projects: String,
|
2024-03-24 15:12:36 -04:00
|
|
|
}
|
2024-03-24 10:45:16 -04:00
|
|
|
|
|
|
|
|
async fn now() -> impl IntoResponse {
|
2024-07-10 06:55:38 -04:00
|
|
|
let template = NowTemplate {};
|
2024-04-28 20:23:19 -04:00
|
|
|
HtmlTemplate(template)
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "now.html")]
|
2024-07-10 06:55:38 -04:00
|
|
|
struct NowTemplate {}
|
2024-03-24 10:45:16 -04:00
|
|
|
|
|
|
|
|
async fn about() -> impl IntoResponse {
|
2024-07-10 06:55:38 -04:00
|
|
|
let template = AboutTemplate {};
|
2024-04-28 20:23:19 -04:00
|
|
|
HtmlTemplate(template)
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "about.html")]
|
2024-07-10 06:55:38 -04:00
|
|
|
struct AboutTemplate {}
|
2024-03-24 10:45:16 -04:00
|
|
|
|
|
|
|
|
async fn contact() -> impl IntoResponse {
|
2024-07-10 06:55:38 -04:00
|
|
|
let template = ContactTemplate {};
|
2024-04-28 20:23:19 -04:00
|
|
|
HtmlTemplate(template)
|
2024-03-24 10:45:16 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "contact.html")]
|
2024-07-10 06:55:38 -04:00
|
|
|
struct ContactTemplate {}
|
2024-07-05 20:00:43 -04:00
|
|
|
|
|
|
|
|
async fn uses() -> impl IntoResponse {
|
2024-07-10 06:55:38 -04:00
|
|
|
let template = UsesTemplate {};
|
2024-07-05 20:00:43 -04:00
|
|
|
HtmlTemplate(template)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "uses.html")]
|
2024-07-10 06:55:38 -04:00
|
|
|
struct UsesTemplate {}
|