From f6281b882000f7fa2cde1bcc97c8f834d9e7d23c Mon Sep 17 00:00:00 2001 From: ElnuDev Date: Sat, 9 Jul 2022 12:21:08 -0700 Subject: [PATCH] Add testing flag and CORS --- Cargo.lock | 16 ++++++++++++++++ Cargo.toml | 1 + src/database.rs | 5 ++++- src/main.rs | 16 ++++++++++++++-- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7fb3845..be6ede7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -19,6 +19,21 @@ dependencies = [ "tokio-util", ] +[[package]] +name = "actix-cors" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "414360eed71ba2d5435b185ba43ecbe281dfab5df3898286d6b7be8074372c92" +dependencies = [ + "actix-utils", + "actix-web", + "derive_more", + "futures-util", + "log", + "once_cell", + "smallvec", +] + [[package]] name = "actix-http" version = "3.2.1" @@ -1045,6 +1060,7 @@ dependencies = [ name = "soudan" version = "0.1.0" dependencies = [ + "actix-cors", "actix-web", "chrono", "md5", diff --git a/Cargo.toml b/Cargo.toml index 4613f99..c16dc99 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] actix-web = "4" +actix-cors = "0.6.1" rusqlite = { version = "0.27.0", features = ["chrono"] } serde = { version = "1", features = ["derive"] } serde_json = "1" diff --git a/src/database.rs b/src/database.rs index 760d267..4209996 100644 --- a/src/database.rs +++ b/src/database.rs @@ -7,7 +7,10 @@ pub struct Database { } impl Database { - pub fn new() -> Result { + pub fn new(testing: bool) -> Result { + if !testing { + unimplemented!("Persistent databases unimplemented!"); + } let conn = Connection::open_in_memory()?; conn.execute( "CREATE TABLE comment ( diff --git a/src/main.rs b/src/main.rs index 4d3eb66..5f92d64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,12 @@ mod comment; +use actix_cors::Cors; pub use comment::*; mod database; pub use database::Database; use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder}; -use std::sync::Mutex; +use std::{env, sync::Mutex}; use validator::Validate; struct AppState { @@ -45,13 +46,24 @@ async fn post_comment(data: web::Data, bytes: web::Bytes) -> impl Resp #[actix_web::main] async fn main() -> Result<(), std::io::Error> { - let db = Database::new().unwrap(); + let testing = { + let mut testing = false; + for argument in env::args() { + if argument == "--testing" || argument == "-t" { + testing = true; + break; + } + } + testing + }; + let db = Database::new(testing).unwrap(); let state = web::Data::new(AppState { db: Mutex::new(db) }); HttpServer::new(move || { App::new() .service(get_comments) .service(post_comment) .app_data(state.clone()) + .wrap(if testing { Cors::permissive() } else { Cors::default() }) }) .bind(("127.0.0.1", 8080))? .run()