66 lines
2.0 KiB
Rust
66 lines
2.0 KiB
Rust
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#"
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
<style>
|
|
body { font-family: sans-serif; background: #1a1a1a; color: #eee; padding: 20px; }
|
|
textarea { width: 100%; height: 150px; background: #333; color: #fff; border: 1px solid #555; padding: 10px; border-radius: 4px; }
|
|
button { width: 100%; padding: 15px; margin-top: 10px; background: #2563eb; color: white; border: none; border-radius: 4px; cursor: pointer; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<form action="/append" method="post">
|
|
<textarea name="content" autofocus></textarea>
|
|
<button type="submit">Append Note</button>
|
|
</form>
|
|
</body>
|
|
</html>
|
|
"#)
|
|
}
|
|
|
|
async fn append_note(Form(note): Form<Note>) -> 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("<script>window.location.href='/';</script>")
|
|
} |