Skip to content

Commit

Permalink
feat: add index page with askama
Browse files Browse the repository at this point in the history
  • Loading branch information
litsynp committed Apr 20, 2024
1 parent 4684e80 commit 621c319
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 1 deletion.
63 changes: 63 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ utoipa-swagger-ui = { version = "6.0.0", features = ["axum"] }
utoipa-redoc = { version = "3.0.0", features = ["axum"] }
utoipa-rapidoc = { version = "3.0.0", features = ["axum"] }
clap = { version = "4.5.4", features = ["derive"] }
askama = "0.12.1"
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ pub mod user {
pub mod service;
pub mod views;
}

pub mod web {
pub mod handlers;
}
3 changes: 2 additions & 1 deletion src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use rust_todo_api::{
auth::{self, models::JWT_SECRET},
common,
common::middlewares::{auth_middleware, AuthState},
todo, user,
todo, user, web,
};

pub fn build_routes(pool: Pool<Postgres>) -> Router {
Expand Down Expand Up @@ -121,6 +121,7 @@ pub fn build_routes(pool: Pool<Postgres>) -> Router {
);

Router::new()
.route("/", get(web::handlers::index))
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", ApiDoc::openapi()))
// .merge(Redoc::with_url("/redoc", ApiDoc::openapi())) // Uncomment to enable Redoc
// There is no need to create `RapiDoc::with_openapi` because the OpenApi is served
Expand Down
30 changes: 30 additions & 0 deletions src/web/handlers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use askama::Template;
use axum::http::StatusCode;
use axum::response::{Html, IntoResponse, Response};

pub async fn index() -> impl IntoResponse {
let template = IndexTemplate {};
HtmlTemplate(template)
}

#[derive(Template)]
#[template(path = "index.html")]
struct IndexTemplate;

struct HtmlTemplate<T>(T);

impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response {
match self.0.render() {
Ok(html) => Html(html).into_response(),
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to render template. Error: {}", err),
)
.into_response(),
}
}
}
7 changes: 7 additions & 0 deletions templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Todo App</h1>
<p>Index page</p>
</body>
</html>

0 comments on commit 621c319

Please sign in to comment.