115 lines
2.6 KiB
Rust
115 lines
2.6 KiB
Rust
use crate::html::{api, blog, projects, HtmlTemplate, NavBar};
|
|
use askama::Template;
|
|
use axum::{
|
|
response::{IntoResponse, Redirect},
|
|
routing::{get, Router},
|
|
Extension,
|
|
};
|
|
use sqlx::PgPool;
|
|
use tower_http::services::ServeDir;
|
|
|
|
#[derive(Clone)]
|
|
pub struct AppState {
|
|
pub db: PgPool,
|
|
}
|
|
|
|
pub fn get_router(pool: PgPool) -> Router {
|
|
let assets_path = std::env::current_dir().unwrap();
|
|
let state = AppState { db: pool };
|
|
Router::new()
|
|
.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())),
|
|
)
|
|
.route("/", get(home))
|
|
.route("/now", get(now))
|
|
.route("/about", get(about))
|
|
.route("/contact", get(contact))
|
|
.route(
|
|
"/robots.txt",
|
|
get(|| async { Redirect::permanent("/assets/robots.txt") }),
|
|
)
|
|
.layer(Extension(state))
|
|
}
|
|
|
|
async fn home() -> impl IntoResponse {
|
|
let template = HomeTemplate {
|
|
active_navbar: NavBar::HOME,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "home.html")]
|
|
struct HomeTemplate {
|
|
active_navbar: &'static str,
|
|
}
|
|
|
|
pub async fn blog() -> impl IntoResponse {
|
|
let template = BlogTemplate {
|
|
active_navbar: NavBar::BLOG,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "blog.html")]
|
|
struct BlogTemplate {
|
|
active_navbar: &'static str,
|
|
}
|
|
|
|
pub async fn projects() -> impl IntoResponse {
|
|
let template = ProjectsTemplate {
|
|
active_navbar: NavBar::PROJECTS,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "projects.html")]
|
|
struct ProjectsTemplate {
|
|
active_navbar: &'static str,
|
|
}
|
|
|
|
async fn now() -> impl IntoResponse {
|
|
let template = NowTemplate {
|
|
active_navbar: NavBar::NOW,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "now.html")]
|
|
struct NowTemplate {
|
|
active_navbar: &'static str,
|
|
}
|
|
|
|
async fn about() -> impl IntoResponse {
|
|
let template = AboutTemplate {
|
|
active_navbar: NavBar::ABOUT,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "about.html")]
|
|
struct AboutTemplate {
|
|
active_navbar: &'static str,
|
|
}
|
|
|
|
async fn contact() -> impl IntoResponse {
|
|
let template = ContactTemplate {
|
|
active_navbar: NavBar::CONTACT,
|
|
};
|
|
HtmlTemplate(template)
|
|
}
|
|
|
|
#[derive(Template)]
|
|
#[template(path = "contact.html")]
|
|
struct ContactTemplate {
|
|
active_navbar: &'static str,
|
|
}
|