diff options
-rw-r--r-- | dichroism/.gitignore | 1 | ||||
-rw-r--r-- | dichroism/Cargo.lock | 294 | ||||
-rw-r--r-- | dichroism/Cargo.toml | 15 | ||||
-rw-r--r-- | dichroism/diesel.toml | 5 | ||||
-rw-r--r-- | dichroism/migrations/.gitkeep | 0 | ||||
-rw-r--r-- | dichroism/migrations/2020-10-04-235458_create_images/down.sql | 1 | ||||
-rw-r--r-- | dichroism/migrations/2020-10-04-235458_create_images/up.sql | 5 | ||||
-rw-r--r-- | dichroism/src/bin/dichroismd.rs | 44 | ||||
-rw-r--r-- | dichroism/src/config.rs | 32 | ||||
-rw-r--r-- | dichroism/src/constants.rs | 3 | ||||
-rw-r--r-- | dichroism/src/error.rs | 1 | ||||
-rw-r--r-- | dichroism/src/handlers.rs | 31 | ||||
-rw-r--r-- | dichroism/src/image_api.rs | 11 | ||||
-rw-r--r-- | dichroism/src/image_repo.rs | 32 | ||||
-rw-r--r-- | dichroism/src/lib.rs | 16 | ||||
-rw-r--r-- | dichroism/src/main.rs | 35 | ||||
-rw-r--r-- | dichroism/src/models.rs | 5 | ||||
-rw-r--r-- | dichroism/src/product_api.rs | 0 | ||||
-rw-r--r-- | dichroism/src/product_repo.rs | 18 | ||||
-rw-r--r-- | dichroism/src/schema.rs | 6 | ||||
-rw-r--r-- | dichroism/src/types.rs | 5 | ||||
-rw-r--r-- | dichroism/tests/integration_tests.rs | 5 |
22 files changed, 513 insertions, 52 deletions
diff --git a/dichroism/.gitignore b/dichroism/.gitignore index e3fef61..b213475 100644 --- a/dichroism/.gitignore +++ b/dichroism/.gitignore @@ -1,3 +1,4 @@ +*.env *.jpeg *.jpg *.png diff --git a/dichroism/Cargo.lock b/dichroism/Cargo.lock index 14ed1f5..03afc5e 100644 --- a/dichroism/Cargo.lock +++ b/dichroism/Cargo.lock @@ -298,6 +298,106 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d25d88fd6b8041580a654f9d0c581a047baee2b3efee13275f2fc392fc75034" [[package]] +name = "async-channel" +version = "1.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21279cfaa4f47df10b1816007e738ca3747ef2ee53ffc51cdbf57a8bb266fee3" +dependencies = [ + "concurrent-queue", + "event-listener", + "futures-core", +] + +[[package]] +name = "async-executor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d373d78ded7d0b3fa8039375718cde0aace493f2e34fb60f51cbf567562ca801" +dependencies = [ + "async-task", + "concurrent-queue", + "fastrand", + "futures-lite", + "once_cell", + "vec-arena", +] + +[[package]] +name = "async-global-executor" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fefeb39da249f4c33af940b779a56723ce45809ef5c54dad84bb538d4ffb6d9e" +dependencies = [ + "async-executor", + "async-io", + "futures-lite", + "num_cpus", + "once_cell", +] + +[[package]] +name = "async-io" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8cf20dd2c6d20ef09876e189b44f213e66e846972cb303eef28d9dfbe790928" +dependencies = [ + "concurrent-queue", + "fastrand", + "futures-lite", + "libc", + "log", + "nb-connect", + "once_cell", + "parking", + "polling", + "vec-arena", + "waker-fn", + "winapi 0.3.9", +] + +[[package]] +name = "async-mutex" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479db852db25d9dbf6204e6cb6253698f175c15726470f78af0d918e99d6156e" +dependencies = [ + "event-listener", +] + +[[package]] +name = "async-std" +version = "1.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a9fa76751505e8df1c7a77762f60486f60c71bbd9b8557f4da6ad47d083732ed" +dependencies = [ + "async-global-executor", + "async-io", + "async-mutex", + "blocking", + "crossbeam-utils", + "futures-channel", + "futures-core", + "futures-io", + "futures-lite", + "gloo-timers", + "kv-log-macro", + "log", + "memchr", + "num_cpus", + "once_cell", + "pin-project-lite", + "pin-utils", + "slab", + "wasm-bindgen-futures", +] + +[[package]] +name = "async-task" +version = "4.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ab27c1aa62945039e44edaeee1dc23c74cc0c303dd5fe0fb462a184f1c3a518" + +[[package]] name = "async-trait" version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -309,6 +409,12 @@ dependencies = [ ] [[package]] +name = "atomic-waker" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "065374052e7df7ee4047b1160cca5e1467a12351a40b3da123c870ba0b8eda2a" + +[[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -396,6 +502,20 @@ dependencies = [ ] [[package]] +name = "blocking" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2640778f8053e72c11f621b0a5175a0560a269282aa98ed85107773ab8e2a556" +dependencies = [ + "async-channel", + "atomic-waker", + "fastrand", + "futures-lite", + "once_cell", + "waker-fn", +] + +[[package]] name = "brotli-sys" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -449,6 +569,12 @@ dependencies = [ ] [[package]] +name = "cache-padded" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "631ae5198c9be5e753e5cc215e1bd73c2b466a3565173db433f52bb9d3e66dba" + +[[package]] name = "cc" version = "1.0.60" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -476,6 +602,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" [[package]] +name = "concurrent-queue" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30ed07550be01594c6026cff2a1d7fe9c8f683caa798e12b68694ac9e88286a3" +dependencies = [ + "cache-padded", +] + +[[package]] name = "const_fn" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -586,14 +721,17 @@ name = "dichroism" version = "0.1.0" dependencies = [ "actix-web", + "async-std", "base64 0.13.0", "diesel", "env_logger", "image", - "lazy_static", "listenfd", "log", + "once_cell", "regex", + "serde", + "serde_json", ] [[package]] @@ -605,6 +743,7 @@ dependencies = [ "byteorder", "diesel_derives", "libsqlite3-sys", + "r2d2", ] [[package]] @@ -680,6 +819,21 @@ dependencies = [ ] [[package]] +name = "event-listener" +version = "2.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7531096570974c3a9dcf9e4b8e1cede1ec26cf5046219fb3b9d897503b9be59" + +[[package]] +name = "fastrand" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca5faf057445ce5c9d4329e382b2ce7ca38550ef3b73a5348362d5f24e0c7fe3" +dependencies = [ + "instant", +] + +[[package]] name = "flate2" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -750,6 +904,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de27142b013a8e869c14957e6d2edeef89e97c289e69d042ee3a49acd8b51789" [[package]] +name = "futures-lite" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b2ec5fce115ef4d4cc77f46025232fba6a2f84381ff42337794147050aae971" +dependencies = [ + "fastrand", + "futures-core", + "futures-io", + "memchr", + "parking", + "pin-project-lite", + "waker-fn", +] + +[[package]] name = "futures-macro" version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -843,6 +1012,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aaf91faf136cb47367fa430cd46e37a788775e7fa104f8b4bcb3861dc389b724" [[package]] +name = "gloo-timers" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47204a46aaff920a1ea58b11d03dec6f704287d27561724a4631e450654a891f" +dependencies = [ + "futures-channel", + "futures-core", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] name = "h2" version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1008,6 +1190,15 @@ dependencies = [ ] [[package]] +name = "js-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca059e81d9486668f12d455a4ea6daa600bd408134cd17e3d3fb5a32d1f016f8" +dependencies = [ + "wasm-bindgen", +] + +[[package]] name = "kernel32-sys" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1018,6 +1209,15 @@ dependencies = [ ] [[package]] +name = "kv-log-macro" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0de8b303297635ad57c9f5059fd9cee7a47f8e8daa09df0fcd07dd39fb22977f" +dependencies = [ + "log", +] + +[[package]] name = "language-tags" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1196,6 +1396,16 @@ dependencies = [ ] [[package]] +name = "nb-connect" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8123a81538e457d44b933a02faf885d3fe8408806b23fa700e8f01c6c3a98998" +dependencies = [ + "libc", + "winapi 0.3.9", +] + +[[package]] name = "net2" version = "0.2.35" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1276,6 +1486,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] +name = "parking" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "427c3892f9e783d91cc128285287e70a59e206ca452770ece88a76f7a3eddd72" + +[[package]] name = "parking_lot" version = "0.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1358,6 +1574,19 @@ dependencies = [ ] [[package]] +name = "polling" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7215a098a80ab8ebd6349db593dc5faf741781bad0c4b7c5701fea6af548d52c" +dependencies = [ + "cfg-if", + "libc", + "log", + "wepoll-sys", + "winapi 0.3.9", +] + +[[package]] name = "ppv-lite86" version = "0.2.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1400,6 +1629,17 @@ dependencies = [ ] [[package]] +name = "r2d2" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f" +dependencies = [ + "log", + "parking_lot", + "scheduled-thread-pool", +] + +[[package]] name = "rand" version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1521,6 +1761,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" [[package]] +name = "scheduled-thread-pool" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7" +dependencies = [ + "parking_lot", +] + +[[package]] name = "scoped_threadpool" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1987,12 +2236,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6454029bf181f092ad1b853286f23e2c507d8e8194d01d92da4a55c274a5508c" [[package]] +name = "vec-arena" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eafc1b9b2dfc6f5529177b62cf806484db55b32dc7c9658a118e11bbeb33061d" + +[[package]] name = "version_check" version = "0.9.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" [[package]] +name = "waker-fn" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" + +[[package]] name = "wasi" version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2024,6 +2285,18 @@ dependencies = [ ] [[package]] +name = "wasm-bindgen-futures" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7866cab0aa01de1edf8b5d7936938a7e397ee50ce24119aef3e1eaa3b6171da" +dependencies = [ + "cfg-if", + "js-sys", + "wasm-bindgen", + "web-sys", +] + +[[package]] name = "wasm-bindgen-macro" version = "0.2.68" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2053,12 +2326,31 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d649a3145108d7d3fbcde896a468d1bd636791823c9921135218ad89be08307" [[package]] +name = "web-sys" +version = "0.3.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4bf6ef87ad7ae8008e15a355ce696bed26012b7caa21605188cfd8214ab51e2d" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] name = "weezl" version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e0e26e7a4d998e3d7949c69444b8b4916bac810da0d3a82ae612c89e952782f4" [[package]] +name = "wepoll-sys" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "142bc2cba3fe88be1a8fcb55c727fa4cd5b0cf2d7438722792e22f26f04bc1e0" +dependencies = [ + "cc", +] + +[[package]] name = "widestring" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/dichroism/Cargo.toml b/dichroism/Cargo.toml index d921225..0e19889 100644 --- a/dichroism/Cargo.toml +++ b/dichroism/Cargo.toml @@ -4,15 +4,24 @@ version = "0.1.0" authors = ["Adam T. Carpenter <atc@53hor.net>"] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "dichroism" +path = "src/lib.rs" + +[[bin]] +name = "dichroismd" +path = "src/bin/dichroismd.rs" [dependencies] actix-web = "3" +async-std = "1" base64 = "0.13" -diesel = { version = "1.4", features = [ "sqlite" ] } +diesel = { version = "1.4", features = [ "sqlite", "r2d2" ] } env_logger = "0.7" image = "0.23" -lazy_static = "1" listenfd = "0.3" log = "0.4" +once_cell = "1" regex = "1" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" diff --git a/dichroism/diesel.toml b/dichroism/diesel.toml new file mode 100644 index 0000000..92267c8 --- /dev/null +++ b/dichroism/diesel.toml @@ -0,0 +1,5 @@ +# For documentation on how to configure this file, +# see diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" diff --git a/dichroism/migrations/.gitkeep b/dichroism/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/dichroism/migrations/.gitkeep diff --git a/dichroism/migrations/2020-10-04-235458_create_images/down.sql b/dichroism/migrations/2020-10-04-235458_create_images/down.sql new file mode 100644 index 0000000..bc3690f --- /dev/null +++ b/dichroism/migrations/2020-10-04-235458_create_images/down.sql @@ -0,0 +1 @@ +DROP TABLE "images" diff --git a/dichroism/migrations/2020-10-04-235458_create_images/up.sql b/dichroism/migrations/2020-10-04-235458_create_images/up.sql new file mode 100644 index 0000000..7e92e57 --- /dev/null +++ b/dichroism/migrations/2020-10-04-235458_create_images/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE "images" ( + "id" INTEGER NOT NULL, + "path" TEXT NOT NULL, + PRIMARY KEY("id") +) diff --git a/dichroism/src/bin/dichroismd.rs b/dichroism/src/bin/dichroismd.rs new file mode 100644 index 0000000..2dc059d --- /dev/null +++ b/dichroism/src/bin/dichroismd.rs @@ -0,0 +1,44 @@ +use actix_web::{App, HttpServer}; +use dichroism::config; +use dichroism::handlers; +use dichroism::result::Result; +use diesel::prelude::SqliteConnection; +use diesel::r2d2::ConnectionManager; +use diesel::r2d2::Pool; +use listenfd::ListenFd; + +#[actix_web::main] +async fn main() -> Result<()> { + // Gather config. + let config = config::Config::new_from_env().await?; + let bind_addr = config.bind_addr; + + // Initialize DB connection pool. + //let manager = ConnectionManager::<SqliteConnection>::new(config.db_url); + let manager = ConnectionManager::<SqliteConnection>::new(&config.db_url); + let pool = Pool::builder().build(manager)?; + + // Initialize application server. + let mut server = HttpServer::new(move || { + App::new() + .data(config.clone()) + .data(pool.clone()) + .service(handlers::hello) + .service(handlers::create_image) + .service(handlers::get_products) + }); + + let mut listenfd = ListenFd::from_env(); + server = if let Some(l) = listenfd + .take_tcp_listener(0) + .expect("Unable to grab TCP listener!") + { + // If using listenfd, use it to allow for cargo watch auto-reloading. + server.listen(l)? + } else { + // Bind to config for release. + server.bind(bind_addr)? + }; + + Ok(server.run().await?) +} diff --git a/dichroism/src/config.rs b/dichroism/src/config.rs index e5b4046..c3dec51 100644 --- a/dichroism/src/config.rs +++ b/dichroism/src/config.rs @@ -1 +1,31 @@ -struct Config {} +use crate::constants::*; +use crate::error::DichroismError; +use crate::result::Result; +use async_std::fs::metadata; +use async_std::path::PathBuf; +use std::env; +use std::net::SocketAddr; + +#[derive(Debug, Clone)] +pub struct Config { + pub db_url: String, + pub img_root: PathBuf, + pub bind_addr: SocketAddr, +} + +impl Config { + pub async fn new_from_env() -> Result<Self> { + let img_root = PathBuf::from(env::var(ENV_IMG_ROOT)?); + let meta = metadata(&img_root).await?; + + if !meta.is_dir() || meta.permissions().readonly() { + return Err(Box::new(DichroismError::InvalidImageRoot)); + } + + Ok(Config { + db_url: env::var(ENV_DB_URL)?, + img_root, + bind_addr: env::var(ENV_BIND_ADDR)?.parse()?, + }) + } +} diff --git a/dichroism/src/constants.rs b/dichroism/src/constants.rs new file mode 100644 index 0000000..fe45c1e --- /dev/null +++ b/dichroism/src/constants.rs @@ -0,0 +1,3 @@ +pub const ENV_IMG_ROOT: &str = "DICHROISM_IMG_ROOT"; +pub const ENV_BIND_ADDR: &str = "DICHROISM_BIND_ADDR"; +pub const ENV_DB_URL: &str = "DICHROISM_DB_URL"; diff --git a/dichroism/src/error.rs b/dichroism/src/error.rs index 2f01381..5e00abc 100644 --- a/dichroism/src/error.rs +++ b/dichroism/src/error.rs @@ -1,6 +1,7 @@ #[derive(Debug)] pub enum DichroismError { UriDataExtract, + InvalidImageRoot, } impl std::error::Error for DichroismError {} diff --git a/dichroism/src/handlers.rs b/dichroism/src/handlers.rs index 1e0ae28..e4ea6a0 100644 --- a/dichroism/src/handlers.rs +++ b/dichroism/src/handlers.rs @@ -1,13 +1,29 @@ +use super::image_repo; +use super::product_repo; +use super::types::DbPool; +use crate::config::Config; use crate::image_api; -use actix_web::{get, post, HttpResponse, Responder}; +use actix_web::{get, post, web, Error, HttpResponse, Responder}; #[get("/")] async fn hello() -> impl Responder { HttpResponse::Ok().body("Hey, this is an API!") } +#[get("/images")] +async fn get_images(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> { + let conn = pool.get().expect("Couldn't get DB connection from pool."); + let images = web::block(move || image_repo::read_images(&conn)) + .await + .map_err(|e| { + eprintln!("{}", e); + HttpResponse::InternalServerError().finish() + })?; + Ok(HttpResponse::Ok().json(images)) +} + #[post("/images")] -async fn create_image(req_body: String) -> impl Responder { +async fn create_image(_config: web::Data<Config>, req_body: String) -> impl Responder { let data = match image_api::extract_data(&req_body) { Err(e) => return HttpResponse::BadRequest().body(format!("fail: {}", e.to_string())), Ok(d) => d, @@ -24,6 +40,13 @@ async fn create_image(req_body: String) -> impl Responder { } #[get("/products")] -async fn get_products(_req_body: String) -> impl Responder { - HttpResponse::Ok().body("got products!") +async fn get_products(pool: web::Data<DbPool>) -> Result<HttpResponse, Error> { + let conn = pool.get().expect("Couldn't get DB connection from pool."); + let products = web::block(move || product_repo::read_products(&conn)) + .await + .map_err(|e| { + eprintln!("{}", e); + HttpResponse::InternalServerError().finish() + })?; + Ok(HttpResponse::Ok().json(products)) } diff --git a/dichroism/src/image_api.rs b/dichroism/src/image_api.rs index 44effe4..97faead 100644 --- a/dichroism/src/image_api.rs +++ b/dichroism/src/image_api.rs @@ -3,10 +3,11 @@ use crate::result::Result; use base64::decode; use regex::Regex; -lazy_static! { - static ref DATA_URI_RE: Regex = - Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)").expect("Couldn't parse Regex!"); -} +use once_cell::sync::Lazy; + +static DATA_URI_RE: Lazy<Regex> = Lazy::new(|| { + Regex::new("^data:image/(png|jpeg);base64,(?P<data>.+)").expect("Couldn't parse Regex.") +}); pub fn generate_images(data: &str) -> Result<()> { let bytes = decode(data)?; @@ -24,7 +25,7 @@ pub fn extract_data(uri: &str) -> Result<&str> { Ok(caps .name("data") - .expect("Should never fail if regex succeeded") + .expect("Should never fail if regex succeeded.") .as_str()) } diff --git a/dichroism/src/image_repo.rs b/dichroism/src/image_repo.rs new file mode 100644 index 0000000..c36f94e --- /dev/null +++ b/dichroism/src/image_repo.rs @@ -0,0 +1,32 @@ +use super::models::ProductImg; +use diesel::prelude::*; +use diesel::result::Error; + +type DBConn = SqliteConnection; + +pub fn read_images(conn: &DBConn) -> Result<Vec<ProductImg>, Error> { + use crate::schema::images::dsl::*; + let results = images.load::<ProductImg>(conn)?; + Ok(results) +} + +pub fn create_image() { + todo!() +} + +pub fn update_image() { + todo!() +} + +pub fn delete_image() { + todo!() +} + +#[cfg(test)] +mod tests { + + #[test] + fn test() -> std::result::Result<(), Box<dyn std::error::Error>> { + Ok(()) + } +} diff --git a/dichroism/src/lib.rs b/dichroism/src/lib.rs new file mode 100644 index 0000000..e3842d3 --- /dev/null +++ b/dichroism/src/lib.rs @@ -0,0 +1,16 @@ +#[macro_use] +extern crate serde; +#[macro_use] +extern crate diesel; + +pub mod config; +mod constants; +mod error; +pub mod handlers; +mod image_api; +mod image_repo; +mod models; +mod product_repo; +pub mod result; +mod schema; +pub mod types; diff --git a/dichroism/src/main.rs b/dichroism/src/main.rs deleted file mode 100644 index 442b92d..0000000 --- a/dichroism/src/main.rs +++ /dev/null @@ -1,35 +0,0 @@ -#[macro_use] -extern crate lazy_static; - -use actix_web::{App, HttpServer}; -use listenfd::ListenFd; - -mod config; -mod error; -mod handlers; -mod image_api; -mod result; - -#[actix_web::main] -async fn main() -> std::io::Result<()> { - let mut listenfd = ListenFd::from_env(); - let mut server = HttpServer::new(|| { - App::new() - .service(handlers::hello) - .service(handlers::create_image) - .service(handlers::get_products) - }); - - server = if let Some(l) = listenfd - .take_tcp_listener(0) - .expect("Unable to grab TCP listener!") - { - // "Debug mode" with cargo watch auto-reloading - server.listen(l)? - } else { - // "Release mode" - server.bind("127.0.0.1:8000")? - }; - - server.run().await -} diff --git a/dichroism/src/models.rs b/dichroism/src/models.rs new file mode 100644 index 0000000..5516688 --- /dev/null +++ b/dichroism/src/models.rs @@ -0,0 +1,5 @@ +#[derive(Debug, Queryable, Serialize)] +pub struct ProductImg { + pub id: i32, + pub path: String, +} diff --git a/dichroism/src/product_api.rs b/dichroism/src/product_api.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/dichroism/src/product_api.rs diff --git a/dichroism/src/product_repo.rs b/dichroism/src/product_repo.rs new file mode 100644 index 0000000..67c5d3e --- /dev/null +++ b/dichroism/src/product_repo.rs @@ -0,0 +1,18 @@ +use super::models::ProductImg; +use diesel::prelude::*; +use diesel::result::Error; + +type DBConn = SqliteConnection; + +pub fn read_products(_conn: &DBConn) -> Result<Vec<ProductImg>, Error> { + todo!() +} + +#[cfg(test)] +mod tests { + + #[test] + fn test() -> std::result::Result<(), Box<dyn std::error::Error>> { + Ok(()) + } +} diff --git a/dichroism/src/schema.rs b/dichroism/src/schema.rs new file mode 100644 index 0000000..e1c4408 --- /dev/null +++ b/dichroism/src/schema.rs @@ -0,0 +1,6 @@ +table! { + images (id) { + id -> Integer, + path -> Text, + } +} diff --git a/dichroism/src/types.rs b/dichroism/src/types.rs new file mode 100644 index 0000000..91aef95 --- /dev/null +++ b/dichroism/src/types.rs @@ -0,0 +1,5 @@ +use diesel::r2d2::ConnectionManager; +use diesel::r2d2::Pool; +use diesel::SqliteConnection; + +pub type DbPool = Pool<ConnectionManager<SqliteConnection>>; diff --git a/dichroism/tests/integration_tests.rs b/dichroism/tests/integration_tests.rs index 3989260..bd2f4fd 100644 --- a/dichroism/tests/integration_tests.rs +++ b/dichroism/tests/integration_tests.rs @@ -1,4 +1,3 @@ -#[test] -fn test_add_two() { - assert_eq!(4, 2 + 2); +fn test() { + assert_eq!(2, 2); } |