pushing for review

This commit is contained in:
specCon18 2024-02-22 15:37:55 -05:00
parent 620b4b3de4
commit b1d7569150
19 changed files with 3742 additions and 228 deletions

27
src/handlers.rs Normal file
View file

@ -0,0 +1,27 @@
use super::templates;
use axum::response::IntoResponse;
pub async fn blog_post() -> impl IntoResponse {
let template = templates::PostTemplate {
title:"Test Tile".to_string(),
description:"this is a test description of a test blog post for rendering in its post template".to_string(),
post_date:"02/21/2024".to_string()
};
templates::HtmlTemplate(template)
}
pub async fn root() -> impl IntoResponse {
let template = templates::HomeTemplate {};
templates::HtmlTemplate(template)
}
pub async fn blog() -> impl IntoResponse {
let template = templates::BlogTemplate {};
templates::HtmlTemplate(template)
}
pub async fn resume() -> impl IntoResponse {
let template = templates::ResumeTemplate {};
templates::HtmlTemplate(template)
}
pub async fn projects() -> impl IntoResponse {
let template = templates::ProjectsTemplate {};
templates::HtmlTemplate(template)
}

View file

@ -1,18 +1,23 @@
use askama::Template;
use axum::{
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
Router,
};
use tower_http::services::ServeDir;
use tracing::info;
use std::env;
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod router;
mod handlers;
mod templates;
#[tokio::main]
async fn main() {
let assets_path = std::env::current_dir().unwrap();
setup_tracing();
let assets_path = env::current_dir().unwrap().join("assets");
let app = router::router(&assets_path);
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
println!("Listening on port 3000");
}
fn setup_tracing(){
tracing_subscriber::registry()
.with(
tracing_subscriber::EnvFilter::try_from_default_env()
@ -20,71 +25,6 @@ async fn main() {
)
.with(tracing_subscriber::fmt::layer())
.init();
info!("hello, web server!");
// build our application with a single route
let app = Router::new()
.route("/", get(root))
.route("/blog", get(blog))
.route("/resume", get(resume))
.route("/projects", get(projects))
.nest_service("/assets", ServeDir::new(format!("{}/assets", assets_path.to_str().unwrap())),);
// run our app with hyper, listening globally on port 3000
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
async fn root() -> impl IntoResponse {
let template = HelloTemplate {};
HtmlTemplate(template)
}
async fn blog() -> impl IntoResponse {
let template = BlogTemplate {};
HtmlTemplate(template)
}
async fn resume() -> impl IntoResponse {
let template = ResumeTemplate {};
HtmlTemplate(template)
}
async fn projects() -> impl IntoResponse {
let template = ProjectsTemplate {};
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "index.html")]
struct HelloTemplate;
#[derive(Template)]
#[template(path = "blog.html")]
struct BlogTemplate;
#[derive(Template)]
#[template(path = "resume.html")]
struct ResumeTemplate;
#[derive(Template)]
#[template(path = "projects.html")]
struct ProjectsTemplate;
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
struct HtmlTemplate<T>(T);
/// 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(),
}
}
}

14
src/router.rs Normal file
View file

@ -0,0 +1,14 @@
use axum::{Router, routing::get};
use tower_http::services::ServeDir;
use std::path::Path;
use super::handlers;
pub fn router(assets_path: &Path) -> Router {
Router::new()
.route("/", get(handlers::root))
.route("/blog", get(handlers::blog))
.route("/blog/post", get(handlers::blog_post))
.route("/resume", get(handlers::resume))
.route("/projects", get(handlers::projects))
.nest_service("/assets", ServeDir::new(assets_path))
}

52
src/templates.rs Normal file
View file

@ -0,0 +1,52 @@
use askama::Template;
use axum::{
http::StatusCode,
response::{IntoResponse, Html, Response}
};
#[derive(Template)]
#[template(path = "post.html")]
pub struct PostTemplate {
pub title: String,
pub description: String,
pub post_date: String,
}
#[derive(Template)]
#[template(path = "index.html")]
pub struct HomeTemplate;
#[derive(Template)]
#[template(path = "blog.html")]
pub struct BlogTemplate;
#[derive(Template)]
#[template(path = "resume.html")]
pub struct ResumeTemplate;
#[derive(Template)]
#[template(path = "projects.html")]
pub struct ProjectsTemplate;
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
pub struct HtmlTemplate<T>(pub T);
/// 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(),
}
}
}