use axum::{ routing::{get, post}, Form, Router, response::Html, }; use std::fs::OpenOptions; use std::io::Write; use chrono::Local; use serde::Deserialize; #[derive(Deserialize)] struct Note { content: String, } #[tokio::main] async fn main() { let app = Router::new() .route("/", get(show_form)) .route("/append", post(append_note)); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); axum::serve(listener, app).await.unwrap(); } async fn show_form() -> Html<&'static str> { Html(r#"
"#) } async fn append_note(Form(note): Form) -> Html<&'static str> { let now = Local::now(); let date_str = now.format("%Y-%m-%d").to_string(); let time_str = now.format("%H:%M").to_string(); // Create filename based on date let filename = format!("{}.txt", date_str); let mut file = OpenOptions::new() .create(true) .append(true) .open(&filename) .unwrap(); if let Err(e) = writeln!(file, "- [{}] {}", time_str, note.content.trim()) { eprintln!("Couldn't write to file: {}", e); } Html("") }