From 05982880d368b476521d09a16b4152ad4661f38a Mon Sep 17 00:00:00 2001 From: Daniel Svitan Date: Sat, 10 May 2025 19:08:42 +0200 Subject: [PATCH] :zap: Adds rusqlite and dotenv --- backend/Cargo.lock | 75 +++++++++++++++++++++++++++++++++++++++++++++ backend/Cargo.toml | 4 ++- backend/src/db.rs | 39 +++++++++++++++++++++++ backend/src/main.rs | 11 ++++++- 4 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 backend/src/db.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 77096b0..5490b96 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -84,7 +84,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" name = "backend" version = "0.1.0" dependencies = [ + "dotenv", "rocket", + "rusqlite", ] [[package]] @@ -194,6 +196,12 @@ dependencies = [ "syn", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "either" version = "1.15.0" @@ -225,6 +233,18 @@ dependencies = [ "windows-sys 0.59.0", ] +[[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 = "fastrand" version = "2.3.0" @@ -251,6 +271,12 @@ 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 = "futures" version = "0.3.31" @@ -388,6 +414,18 @@ name = "hashbrown" version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "84b26c544d002229e640969970a2e74021aadf6e2f96372b9c58eff97de08eb3" +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 = "hermit-abi" @@ -516,6 +554,17 @@ version = "0.2.172" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa" +[[package]] +name = "libsqlite3-sys" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "947e6816f7825b2b45027c2c32e7085da9934defa535de4a6a46b10a4d5257fa" +dependencies = [ + "cc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.9.4" @@ -724,6 +773,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 = "powerfmt" version = "0.2.0" @@ -960,6 +1015,20 @@ dependencies = [ "uncased", ] +[[package]] +name = "rusqlite" +version = "0.35.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a22715a5d6deef63c637207afbe68d0c72c3f8d0022d7cf9714c442d6157606b" +dependencies = [ + "bitflags", + "fallible-iterator", + "fallible-streaming-iterator", + "hashlink", + "libsqlite3-sys", + "smallvec", +] + [[package]] name = "rustc-demangle" version = "0.1.24" @@ -1385,6 +1454,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" + [[package]] name = "version_check" version = "0.9.5" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 7e598c5..b0fa34a 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -4,4 +4,6 @@ version = "0.1.0" edition = "2024" [dependencies] -rocket = { version = "0.5.1" } +rocket = "0.5.1" +dotenv = "0.15.0" +rusqlite = { version = "0.35.0", features = ["bundled"] } diff --git a/backend/src/db.rs b/backend/src/db.rs new file mode 100644 index 0000000..506ad4a --- /dev/null +++ b/backend/src/db.rs @@ -0,0 +1,39 @@ +use rusqlite::{Connection, Error}; + +#[derive(Debug)] +pub enum ActionKind { + Text, + Script, +} + +#[derive(Debug)] +pub struct Action { + id: i32, + name: String, + kind: ActionKind, + source: String, + created_at: String, + updated_at: String, +} + +pub struct Conn { + conn: Connection, +} + +pub fn new_conn(db_path: &str) -> Result { + let conn = Connection::open(db_path).expect("failed to open database"); + + conn.execute( + "CREATE TABLE action ( + id INTEGER PRIMARY KEY, + name TEXT NOT NULL, + kind TEXT NOT NULL, + source TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL + )", + (), // empty list of parameters. + )?; + + Ok(Conn { conn }) +} diff --git a/backend/src/main.rs b/backend/src/main.rs index d3ba5e6..220e87c 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,4 +1,9 @@ -#[macro_use] extern crate rocket; +mod db; + +use dotenv; + +#[macro_use] +extern crate rocket; #[get("/")] async fn index() -> &'static str { @@ -7,5 +12,9 @@ async fn index() -> &'static str { #[launch] fn rocket() -> _ { + dotenv::dotenv().ok(); + let db_path = dotenv::var("DB_PATH").expect("DB_PATH is not set"); + let db = db::new_conn(&db_path); + rocket::build().mount("/", routes![index]) }