Adds rusqlite and dotenv

This commit is contained in:
Daniel Svitan
2025-05-10 19:08:42 +02:00
parent b8d3d427a3
commit 05982880d3
4 changed files with 127 additions and 2 deletions

39
backend/src/db.rs Normal file
View File

@@ -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<Conn, Error> {
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 })
}

View File

@@ -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])
}