From d4db9155a69c49a9f61f6cf66530361526f3d632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sat, 18 Oct 2025 20:54:23 +0200 Subject: [PATCH] read only --- Cargo.lock | 96 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + flake.nix | 2 + src/create_tables.sql | 6 +++ src/main.rs | 46 ++++++++++++++++++-- tasks.db | Bin 0 -> 8192 bytes templates/index.html | 21 +++++++++ 7 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 src/create_tables.sql create mode 100644 tasks.db create mode 100644 templates/index.html diff --git a/Cargo.lock b/Cargo.lock index 9d7cc6c..002be49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -123,18 +123,52 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d71b6127be86fdcfddb610f7182ac57211d4b18a3e9c82eb2d17662f2227ad6a" +[[package]] +name = "cc" +version = "1.2.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac9fe6cdbb24b6ade63616c0a0688e45bb56732262c158df3c0c4bea4ca47cb7" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "fallible-iterator" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2acce4a10f12dc2fb14a218589d4f1f62ef011b2d0cc4b3cb1bba8e94da14649" + +[[package]] +name = "fallible-streaming-iterator" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a" + +[[package]] +name = "find-msvc-tools" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52051878f80a721bb68ebfbc930e07b65ba72f2da88968ea5c06fd6ca3d3a127" + [[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + [[package]] name = "form_urlencoded" version = "1.2.2" @@ -177,6 +211,24 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash", +] + +[[package]] +name = "hashlink" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7382cf6263419f2d8df38c55d7da83da5c18aef87fc7a7fc1fb1e344edfe14c1" +dependencies = [ + "hashbrown", +] + [[package]] name = "http" version = "1.3.1" @@ -272,6 +324,17 @@ version = "0.2.177" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" +[[package]] +name = "libsqlite3-sys" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "133c182a6a2c87864fe97778797e46c7e999672690dc9fa3ee8e241aa4a9c13f" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "lock_api" version = "0.4.14" @@ -363,6 +426,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "pkg-config" +version = "0.3.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c" + [[package]] name = "proc-macro2" version = "1.0.101" @@ -390,6 +459,20 @@ dependencies = [ "bitflags", ] +[[package]] +name = "rusqlite" +version = "0.37.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "165ca6e57b20e1351573e3729b958bc62f0e48025386970b6e4d29e7a7e71f3f" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "rustc-hash" version = "2.1.1" @@ -473,6 +556,12 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + [[package]] name = "signal-hook-registry" version = "1.4.6" @@ -521,6 +610,7 @@ version = "0.1.0" dependencies = [ "askama", "axum", + "rusqlite", "tokio", ] @@ -606,6 +696,12 @@ version = "1.0.19" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f63a545481291138910575129486daeaf8ac54aee4387fe7906919f7830c7d9d" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "wasi" version = "0.11.1+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index efda9d6..34f8576 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,5 @@ edition = "2024" [dependencies] askama = "0.14.0" axum = "0.8.6" +rusqlite = { version = "0.37.0", features = ["bundled"] } tokio = { version = "1.48.0", features = ["full"] } diff --git a/flake.nix b/flake.nix index 2df12ad..f70093f 100644 --- a/flake.nix +++ b/flake.nix @@ -18,6 +18,8 @@ pkgs.rustc pkgs.bacon pkgs.rust-analyzer + pkgs.clippy + pkgs.sqlite ]; }; } diff --git a/src/create_tables.sql b/src/create_tables.sql new file mode 100644 index 0000000..1c23668 --- /dev/null +++ b/src/create_tables.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS task ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + worth FLOAT NOT NULL DEFAULT 1.0, + times_completed INTEGER NOT NULL DEFAULT 0 +); diff --git a/src/main.rs b/src/main.rs index cc77045..11c5683 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,14 +1,52 @@ -use axum::{response::Html, routing::get, Router}; +use std::sync::{Arc, Mutex}; + +use axum::{extract::State, response::Html, routing::get, Router}; +use rusqlite::Connection; +use askama::Template; + +#[derive(Template)] +#[template(path = "index.html")] +struct IndexTemplate { + tasks: Vec, +} + +#[derive(Debug, Clone, PartialEq)] +struct Task { + id: i32, + name: String, + worth: f32, + times_completed: i32, +} #[tokio::main] async fn main() { - let app = Router::new().route("/", get(handler)); + let db_connecion_str = "./tasks.db".to_string(); + let conn = Connection::open(db_connecion_str).unwrap(); + conn.execute(include_str!("create_tables.sql"), ()).unwrap(); + let conn = Arc::new(Mutex::new(conn)); + + let app = Router::new().route("/", get(root)).with_state(conn); let listener = tokio::net::TcpListener::bind("0.0.0.0:3001").await.unwrap(); println!("listening on {}", listener.local_addr().unwrap()); axum::serve(listener, app).await.unwrap(); } -async fn handler() -> Html<&'static str> { - Html("

Hello, World!

") +async fn root( + State(conn): State>> +) -> Html { + let conn = conn.lock().unwrap(); + let mut stmt = conn.prepare("SELECT id, name, worth, times_completed FROM task").unwrap(); + let tasks = stmt.query_map((), |row| { + Ok(Task { + id: row.get(0).unwrap(), + name: row.get(1).unwrap(), + worth: row.get(2).unwrap(), + times_completed: row.get(3).unwrap(), + }) + }).unwrap().collect::>().unwrap(); + let index = IndexTemplate {tasks}; + Html( + index.render().unwrap() + ) } diff --git a/tasks.db b/tasks.db new file mode 100644 index 0000000000000000000000000000000000000000..768836bc1bac7425ecc863dd3c9c65f437a854e2 GIT binary patch literal 8192 zcmWFz^vNtqRY=P(%1ta$FlG>7U}R))P*7lCU|@n`1}I=;U|?W@vOyGx52Ep5CI-E# zLS6<221b5Q27XU`N=M~JLtr!nMnhmU1V%$(Gz3ONU^E0qLtr!nMnhmU1V%$(2!ueR z2_w6>s3>ETcS&MWPHIVFaW)7uIS08qhPWz(I6C>bDu6^3G`JKL6cjR36g>SxT-{xR z6as=geI0`$6}(*|b-;3YiMgo?A+8Z23V!|}3VxwJJ`nNp{GyT!1vejmM}&-mi>sSs zs85K3p`HOmRY_)UYH@sWer`cdYDp@>8kkuM2rXQikiPjR2L4Y&AdE+yI2r<@Aut*O vqaiRF0;3@?8UmvsFd71*Aut*OqaiRF0;3^7WC-vuvNA|>a=KO|f(8fx_*On+ literal 0 HcmV?d00001 diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..ca97d97 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,21 @@ + + + + + + + Task Counter + + +
+

Task Counter!

+
+
+
    + {% for task in tasks %} +
  • {{ task.name }}
  • + {% endfor %} +
+
+ +