added askama, base template and index
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
use axum;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
||||
|
||||
mod pages;
|
||||
mod post_parser;
|
||||
mod router;
|
||||
// mod template;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
|
9
axum_server/src/pages/index.rs
Normal file
9
axum_server/src/pages/index.rs
Normal file
@ -0,0 +1,9 @@
|
||||
use askama::Template;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "index.html")]
|
||||
pub struct IndexTemplate {}
|
||||
|
||||
pub async fn render_index() -> IndexTemplate {
|
||||
IndexTemplate {}
|
||||
}
|
1
axum_server/src/pages/mod.rs
Normal file
1
axum_server/src/pages/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod index;
|
@ -1,11 +1,11 @@
|
||||
use crate::post_parser::parse_post;
|
||||
use crate::{pages::index::render_index, post_parser::parse_post};
|
||||
use axum::{extract::MatchedPath, http::Request, routing::get, Router};
|
||||
use tower_http::trace::TraceLayer;
|
||||
use tracing::info_span;
|
||||
|
||||
pub fn get_router() -> Router {
|
||||
Router::new()
|
||||
.route("/", get(|| async { "Hello, World!" }))
|
||||
.route("/", get(render_index))
|
||||
.route("/blog/:post_id", get(parse_post))
|
||||
.layer(
|
||||
TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
||||
|
9
axum_server/src/template.rs
Normal file
9
axum_server/src/template.rs
Normal file
@ -0,0 +1,9 @@
|
||||
pub use askama::*;
|
||||
use axum::http::Response;
|
||||
|
||||
pub fn into_response<T: Template>(t: &T) -> Response {
|
||||
match t.render() {
|
||||
Ok(body) => Html(body),
|
||||
Err(_) => StatusCode::INTERNAL_SERVER_ERROR.into_response(),
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user