2024-01-21 17:15:59 -05:00
|
|
|
use anyhow::Context;
|
|
|
|
|
use askama::Template;
|
|
|
|
|
use axum::{
|
2024-02-17 15:36:43 -05:00
|
|
|
response::IntoResponse,
|
2024-01-21 17:15:59 -05:00
|
|
|
routing::get,
|
|
|
|
|
Router,
|
|
|
|
|
};
|
|
|
|
|
use tower_http::services::ServeDir;
|
|
|
|
|
use tracing::info;
|
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2024-02-17 15:36:43 -05:00
|
|
|
mod html;
|
2024-01-21 17:15:59 -05:00
|
|
|
|
|
|
|
|
pub async fn run() -> anyhow::Result<()> {
|
|
|
|
|
tracing_subscriber::registry()
|
|
|
|
|
.with(
|
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
|
|
|
.unwrap_or_else(|_| "achubb_backend".into()),
|
|
|
|
|
)
|
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
|
.init();
|
|
|
|
|
info!("initializing router...");
|
|
|
|
|
let assets_path = std::env::current_dir().unwrap();
|
|
|
|
|
let port = 8000_u16;
|
|
|
|
|
let addr = std::net::SocketAddr::from(([0, 0, 0, 0], port));
|
|
|
|
|
let api_router = Router::new().route("/hello", get(hello_from_the_server));
|
|
|
|
|
let router = Router::new()
|
|
|
|
|
.nest("/api", api_router)
|
|
|
|
|
.route("/", get(hello))
|
2024-02-17 15:36:43 -05:00
|
|
|
.route("/navbar", get(navbar))
|
2024-01-21 17:15:59 -05:00
|
|
|
.route("/another-page", get(another_page))
|
|
|
|
|
.nest_service(
|
|
|
|
|
"/assets",
|
|
|
|
|
ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),
|
|
|
|
|
);
|
|
|
|
|
info!("router initialized, now listening on port {}", port);
|
|
|
|
|
axum::Server::bind(&addr)
|
|
|
|
|
.serve(router.into_make_service())
|
|
|
|
|
.await
|
|
|
|
|
.context("error while starting server")?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn hello() -> impl IntoResponse {
|
|
|
|
|
let template = HelloTemplate {};
|
2024-02-17 15:36:43 -05:00
|
|
|
html::HtmlTemplate(template)
|
2024-01-21 17:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "hello.html")]
|
|
|
|
|
struct HelloTemplate;
|
|
|
|
|
|
2024-02-17 15:36:43 -05:00
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "test-navbar.html")]
|
|
|
|
|
struct NavbarTemplate;
|
2024-01-21 17:15:59 -05:00
|
|
|
|
2024-02-17 15:36:43 -05:00
|
|
|
async fn navbar() -> impl IntoResponse {
|
|
|
|
|
let template = NavbarTemplate {};
|
|
|
|
|
html::HtmlTemplate(template)
|
2024-01-21 17:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn another_page() -> impl IntoResponse {
|
|
|
|
|
let template = AnotherPageTemplate {};
|
2024-02-17 15:36:43 -05:00
|
|
|
html::HtmlTemplate(template)
|
2024-01-21 17:15:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "another-page.html")]
|
|
|
|
|
struct AnotherPageTemplate;
|
|
|
|
|
|
|
|
|
|
async fn hello_from_the_server() -> &'static str {
|
|
|
|
|
"Hello!"
|
|
|
|
|
}
|