- Main page list some todo items
- Form to add todo item
- title: on line
- details: long text
- status: Planned / In Progress / Done
- created: timestamp
- last_updated: timestamp
- deadline: timestamp
- Allow the user to
- edit todo item
- move items between states
[package]
name = "todo"
version = "0.1.0"
edition = "2024"
[dependencies]
askama = "0.15.6"
axum = "0.8.8"
serde = { version = "1.0.228", features = ["derive"] }
tokio = { version = "1.50.0", features = ["full"] }
[dev-dependencies]
headers = "0.4.1"
http-body-util = "0.1.3"
tower = { version = "0.5.3", features = ["util"] }
use askama::Template;
use axum::{
Router,
extract::Query,
http::StatusCode,
response::{Html, IntoResponse, Response},
routing::get,
};
use serde::Deserialize;
struct Item {
id: String,
title: String,
}
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(),
}
}
}
#[derive(Template)]
#[template(path = "main.html")]
struct MainTemplate {
title: String,
}
#[derive(Template)]
#[template(path = "edit.html")]
struct EditTemplate {
title: String,
}
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
struct Params {
text: String,
}
async fn main_page() -> impl IntoResponse {
let template = MainTemplate {
title: String::from("TODO"),
};
HtmlTemplate(template)
}
// Query(params): Query<Params>
async fn edit() -> impl IntoResponse {
let template = EditTemplate {
title: String::from("Add new item"),
};
HtmlTemplate(template)
}
fn create_router() -> Router {
Router::new()
.route("/", get(main_page))
.route("/edit", get(edit))
}
#[tokio::main]
async fn main() {
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on {}", listener.local_addr().unwrap());
axum::serve(listener, create_router()).await.unwrap();
}
#[cfg(test)]
mod tests;
#![allow(unused)]
fn main() {
use super::*;
use axum::{
body::Body,
http::{Request, StatusCode},
};
use http_body_util::BodyExt;
use tower::ServiceExt;
#[tokio::test]
async fn test_main() {
let response = create_router()
.oneshot(Request::builder().uri("/").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let content_type = response.headers().get("content-type").unwrap();
assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert!(html.contains(r#"<title>TODO</title>"#));
assert!(html.contains(r#"<h1>TODO</h1>"#));
}
#[tokio::test]
async fn test_edit_new() {
let response = create_router()
.oneshot(Request::builder().uri("/edit").body(Body::empty()).unwrap())
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let content_type = response.headers().get("content-type").unwrap();
assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");
let body = response.into_body();
let bytes = body.collect().await.unwrap().to_bytes();
let html = String::from_utf8(bytes.to_vec()).unwrap();
assert!(html.contains("<title>Add new item</title>"));
assert!(html.contains("<h1>Add new item</h1>"));
assert!(html.contains(r#"<form method="post" action="/edit">"#));
}
// #[tokio::test]
// async fn test_echo_with_empty_text() {
// let response = create_router()
// .oneshot(
// Request::builder()
// .uri("/echo?text=")
// .body(Body::empty())
// .unwrap(),
// )
// .await
// .unwrap();
// assert_eq!(response.status(), StatusCode::OK);
// let content_type = response.headers().get("content-type").unwrap();
// assert_eq!(content_type.to_str().unwrap(), "text/html; charset=utf-8");
// let body = response.into_body();
// let bytes = body.collect().await.unwrap().to_bytes();
// let html = String::from_utf8(bytes.to_vec()).unwrap();
// assert!(html.contains("You did not write anything."));
// assert!(!html.contains("You wrote: <b>Hello World</b>"));
// }
// #[tokio::test]
// async fn test_echo_without_text_param() {
// let response = create_router()
// .oneshot(Request::builder().uri("/echo").body(Body::empty()).unwrap())
// .await
// .unwrap();
// assert_eq!(response.status(), StatusCode::BAD_REQUEST);
// let content_type = response.headers().get("content-type").unwrap();
// assert_eq!(content_type.to_str().unwrap(), "text/plain; charset=utf-8");
// let body = response.into_body();
// let bytes = body.collect().await.unwrap().to_bytes();
// let html = String::from_utf8(bytes.to_vec()).unwrap();
// assert_eq!(
// html,
// "Failed to deserialize query string: missing field `text`"
// );
// }
}
{% include "incl/header.html" %}
{% include "incl/footer.html" %}