From 6094beb199a4ff8c2110f653cb3aa976f01bc757 Mon Sep 17 00:00:00 2001 From: Mohamad Date: Fri, 27 Dec 2024 15:40:32 +0100 Subject: [PATCH] backend changes --- backend/Cargo.lock | 74 +++++++++++++++++++++++++++++++++ backend/Cargo.toml | 7 +++- backend/{src => }/form_data.db | Bin 20480 -> 20480 bytes backend/src/handlers.rs | 3 +- backend/src/main.rs | 13 +++++- backend/src/models.rs | 4 +- 6 files changed, 94 insertions(+), 7 deletions(-) rename backend/{src => }/form_data.db (90%) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index d12ae40..194ebb2 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -19,6 +19,21 @@ dependencies = [ "tracing", ] +[[package]] +name = "actix-cors" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0346d8c1f762b41b458ed3145eea914966bb9ad20b9be0d6d463b20d45586370" +dependencies = [ + "actix-utils", + "actix-web", + "derive_more", + "futures-util", + "log", + "once_cell", + "smallvec", +] + [[package]] name = "actix-files" version = "0.6.6" @@ -464,6 +479,19 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "env_logger" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" +dependencies = [ + "humantime", + "is-terminal", + "log", + "regex", + "termcolor", +] + [[package]] name = "equivalent" version = "1.0.1" @@ -511,8 +539,11 @@ dependencies = [ name = "formies_be" version = "0.1.0" dependencies = [ + "actix-cors", "actix-files", "actix-web", + "env_logger", + "log", "rusqlite", "serde", "serde_json", @@ -547,6 +578,7 @@ dependencies = [ "futures-task", "pin-project-lite", "pin-utils", + "slab", ] [[package]] @@ -620,6 +652,12 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "hermit-abi" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" + [[package]] name = "http" version = "0.2.12" @@ -649,6 +687,12 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" +[[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + [[package]] name = "icu_collections" version = "1.5.0" @@ -804,6 +848,17 @@ dependencies = [ "hashbrown 0.15.2", ] +[[package]] +name = "is-terminal" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys", +] + [[package]] name = "itoa" version = "1.0.14" @@ -837,6 +892,7 @@ version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "afc22eff61b133b115c6e8c74e818c628d6d5e7a502afea6f64dee076dd94326" dependencies = [ + "cc", "pkg-config", "vcpkg", ] @@ -1274,6 +1330,15 @@ dependencies = [ "syn", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + [[package]] name = "time" version = "0.3.37" @@ -1439,6 +1504,15 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 46bbf1b..900be72 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -5,8 +5,11 @@ edition = "2021" [dependencies] actix-web = "4.0" -rusqlite = "0.29" +rusqlite = { version = "0.29", features = ["bundled"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" uuid = { version = "1.0", features = ["v4"] } -actix-files = "0.6" \ No newline at end of file +actix-files = "0.6" +actix-cors = "0.6" +env_logger = "0.10" # Check for the latest version +log = "0.4" \ No newline at end of file diff --git a/backend/src/form_data.db b/backend/form_data.db similarity index 90% rename from backend/src/form_data.db rename to backend/form_data.db index 8e70a8c6600d26650df72de455ea0230afccd186..663f6585599331c9f5ca034a0765c8b3a084bff7 100644 GIT binary patch literal 20480 zcmeI&-)q}O00(f%N}R6Xczg0gDI5x;2}DJ|Y)c-4x{f@;94B*RSQ*3BkJBQM?L<;o zO38z>(#QTId)VKwKVa|u4+i@S`nZ!4FLGEiw=l-&_pp3-(w*+^gZ*$n4!`MzDJ8)) z9y=+~xKBBr=bjM4aa@^QtL&PG5=*SiGc4u{>D!WJ?s4!(rTUIrsl4IV-&KFByjlPC zw!*j%0SG_<0uX=z1Rwwb2teTD5x9_rwc5r8|LRfdxDicG-Eo*C;dGJ|k}I8o)wV5S zx1aVbQg}c%?~_6ZePUbR+T>ucyVo8(BVSw3wigOylfKcsRDa(l{b8@SSj2Zy=khIo z|J;kIlTv@g=8ik8PxkJfMRu%id)Tu{XE+#GeS4JMJha<;2aB)n?hmZpuliZH$mUg7 zw#dNhTC7&bI?U&!3E2$&EwbNdZ|zxZ(4F>Sr@dqSHIA+NO1ZYX!E@onr)SAY#AdT2 z=QN$>`B6b zrE=}d+HDs3SVl@73G6?0FZ*9r*dCTaZTzQYD52#M&5Kxzk4a>Gu2W0;+Tpp zF{NjzxGhGGOCy%eX1OezN0&J=4ahN#lj%e}-@dyt9XnxkT|3VftADS`sxB$2q&A77 zx72z|s~?wEho7BjbzOI4pFK5HKag~4D3a?3p5&TtVCp8N)My@s|Im5A&~=}(-1&_b zWw|9Av*&zy`1!>bnrhN!!_;)CX$DM93+j^L%ZAkOXuYX=iq}+i>&zJ+N0fBgQUYsz z&eRR#BqjYzteBphhB5WURvMquZLuhukwh-MI zqSmULEnT_Ke%C_vHCKIG{e?vc5P$##AOHafKmY;|fB*y_009X6YXY0XkY8qHM#u^- zvj9?hBn&@UX4N9w|L3dEIrc$-00bZa0SG_<0uX=z1Rwwb2teQi1!fP~5rW}tRa0E* zI<7}0MGhL0?lnBgbaYSBjKGmi*)%lSSY`>}rFiqO1Us1#AGuGaZs0#==QgfRRTNHB zWJfpRqaVcc#X}XE+|uN1|DUhEW&8ghI6zc`00bZa0SG_<0uX=z1Rwwb2teRJ6Ug@e ImsxW76Utt+Pyhe` delta 171 zcmZozz}T>Wae_1>(?l6(Q6>hxx+q@$9}FzK&lva>_|Nh_<9oSTP@ssHtBHY`U0hk2 zvDtm{3|{NW#{80#|L{srmgke0Y|m#rxr)zj@&P_ePAx`uadC0RCgaIW{F-1@A^Z}P nOZcULs&pB-CZFNAU}6y1Ea-5Yf8qpVHZBGrKyZ0cxu~)L>>, form: web::Json
, ) -> impl Responder { + println!("Received form: {:?}", form); let conn = db.lock().unwrap(); // Lock the Mutex to access the database let form_id = Uuid::new_v4().to_string(); let form_json = serde_json::to_string(&form.fields).unwrap(); @@ -34,7 +35,7 @@ pub async fn get_forms(db: web::Data>>) -> impl Responder let forms_iter = stmt .query_map([], |row| { - let id: String = row.get(0)?; + let id: Option = row.get(0)?; let name: String = row.get(1)?; let fields: String = row.get(2)?; let fields = serde_json::from_str(&fields).unwrap(); diff --git a/backend/src/main.rs b/backend/src/main.rs index 720366e..341c4cc 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -1,4 +1,5 @@ -use actix_files as fs; +use actix_cors::Cors; +// use actix_files as fs; use actix_web::{web, App, HttpServer}; use std::sync::{Arc, Mutex}; @@ -9,6 +10,8 @@ mod models; #[actix_web::main] async fn main() -> std::io::Result<()> { // Initialize the database connection + env_logger::init(); + let db = Arc::new(Mutex::new( db::init_db().expect("Failed to initialize the database"), )); @@ -16,8 +19,14 @@ async fn main() -> std::io::Result<()> { // Start the Actix-Web server HttpServer::new(move || { App::new() + .wrap( + Cors::default() + .allow_any_origin() + .allow_any_header() + .allow_any_method(), + ) .app_data(web::Data::new(db.clone())) - .service(fs::Files::new("/", "./frontend/public").index_file("index.html")) + // .service(fs::Files::new("/", "./frontend/public").index_file("index.html")) .route("/forms", web::post().to(handlers::create_form)) .route("/forms", web::get().to(handlers::get_forms)) .route( diff --git a/backend/src/models.rs b/backend/src/models.rs index 9a63d43..6678636 100644 --- a/backend/src/models.rs +++ b/backend/src/models.rs @@ -1,8 +1,8 @@ use serde::{Deserialize, Serialize}; -#[derive(Serialize, Deserialize)] +#[derive(Serialize, Deserialize, Debug)] pub struct Form { - pub id: String, + pub id: Option, pub name: String, pub fields: serde_json::Value, // JSON array of form fields }