achubb_website/src/html/root.rs

55 lines
1.6 KiB
Rust
Raw Normal View History

use crate::html::{api, blog, AppState};
2024-04-28 11:02:16 -04:00
use axum::{
http::StatusCode,
2024-04-28 11:02:16 -04:00
response::{IntoResponse, Redirect},
routing::{get, Router},
Extension,
2024-04-28 11:02:16 -04:00
};
use sqlx::PgPool;
2024-03-24 10:45:16 -04:00
use tower_http::services::ServeDir;
use super::{garden, get_page};
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("/api", api::get_router())
.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-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(state: Extension<AppState>) -> Result<impl IntoResponse, StatusCode> {
get_page(&state.db, "home").await
}
2024-03-24 10:45:16 -04:00
async fn now(state: Extension<AppState>) -> Result<impl IntoResponse, StatusCode> {
get_page(&state.db, "now").await
2024-03-24 10:45:16 -04:00
}
async fn about(state: Extension<AppState>) -> Result<impl IntoResponse, StatusCode> {
get_page(&state.db, "about").await
2024-03-24 10:45:16 -04:00
}
async fn contact(state: Extension<AppState>) -> Result<impl IntoResponse, StatusCode> {
get_page(&state.db, "contact").await
2024-03-24 10:45:16 -04:00
}
async fn uses(state: Extension<AppState>) -> Result<impl IntoResponse, StatusCode> {
get_page(&state.db, "uses").await
2024-07-05 20:00:43 -04:00
}