2023-12-17 14:45:33 -05:00
|
|
|
use anyhow::Context;
|
|
|
|
|
use askama::Template;
|
|
|
|
|
use axum::{
|
|
|
|
|
http::StatusCode,
|
|
|
|
|
response::{Html, IntoResponse, Response},
|
|
|
|
|
routing::get,
|
|
|
|
|
Router,
|
|
|
|
|
};
|
|
|
|
|
use tower_http::services::ServeDir;
|
2023-12-16 17:58:58 -05:00
|
|
|
use tracing::info;
|
|
|
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
2023-12-15 17:57:46 -05:00
|
|
|
|
2023-12-16 17:58:58 -05:00
|
|
|
#[tokio::main]
|
2023-12-17 14:45:33 -05:00
|
|
|
async fn main() -> anyhow::Result<()> {
|
2023-12-16 17:58:58 -05:00
|
|
|
tracing_subscriber::registry()
|
|
|
|
|
.with(
|
|
|
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
2023-12-16 18:01:57 -05:00
|
|
|
.unwrap_or_else(|_| "achubb_backend".into()),
|
2023-12-16 17:58:58 -05:00
|
|
|
)
|
|
|
|
|
.with(tracing_subscriber::fmt::layer())
|
|
|
|
|
.init();
|
2023-12-17 14:45:33 -05:00
|
|
|
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));
|
2023-12-18 18:31:15 -05:00
|
|
|
let api_router = Router::new().route("/hello", get(hello_from_the_server));
|
2023-12-17 15:43:25 -05:00
|
|
|
let router = Router::new()
|
2023-12-18 18:31:15 -05:00
|
|
|
.nest("/api", api_router)
|
2023-12-17 15:43:25 -05:00
|
|
|
.route("/", get(hello))
|
|
|
|
|
.route("/another-page", get(another_page))
|
|
|
|
|
.nest_service(
|
|
|
|
|
"/assets",
|
|
|
|
|
ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),
|
|
|
|
|
);
|
2023-12-17 14:45:33 -05:00
|
|
|
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 {};
|
|
|
|
|
HtmlTemplate(template)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "hello.html")]
|
|
|
|
|
struct HelloTemplate;
|
|
|
|
|
|
|
|
|
|
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
|
|
|
|
|
struct HtmlTemplate<T>(T);
|
2023-12-15 17:57:46 -05:00
|
|
|
|
2023-12-17 14:45:33 -05:00
|
|
|
/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
|
|
|
|
|
impl<T> IntoResponse for HtmlTemplate<T>
|
|
|
|
|
where
|
|
|
|
|
T: Template,
|
|
|
|
|
{
|
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
|
// Attempt to render the template with askama
|
|
|
|
|
match self.0.render() {
|
|
|
|
|
// If we're able to successfully parse and aggregate the template, serve it
|
|
|
|
|
Ok(html) => Html(html).into_response(),
|
|
|
|
|
// If we're not, return an error or some bit of fallback HTML
|
|
|
|
|
Err(err) => (
|
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
|
format!("Failed to render template. Error: {}", err),
|
|
|
|
|
)
|
|
|
|
|
.into_response(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-14 20:50:31 -05:00
|
|
|
}
|
2023-12-17 15:43:25 -05:00
|
|
|
|
|
|
|
|
async fn another_page() -> impl IntoResponse {
|
|
|
|
|
let template = AnotherPageTemplate {};
|
|
|
|
|
HtmlTemplate(template)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Template)]
|
|
|
|
|
#[template(path = "another-page.html")]
|
|
|
|
|
struct AnotherPageTemplate;
|
2023-12-18 18:31:15 -05:00
|
|
|
|
|
|
|
|
async fn hello_from_the_server() -> &'static str {
|
|
|
|
|
"Hello!"
|
|
|
|
|
}
|