From 49315ceb6823f99e77b333a5d2dd0341c8bb25bb Mon Sep 17 00:00:00 2001 From: Andrew Li Date: Tue, 10 Sep 2024 21:03:09 +0800 Subject: [PATCH 1/3] add API routes to get single user and all users algos --- webserver/Cargo.lock | 238 ++++++++++++++++++++++++++++++++++++++++-- webserver/Cargo.toml | 4 + webserver/src/main.rs | 89 +++++++++++++++- 3 files changed, 321 insertions(+), 10 deletions(-) diff --git a/webserver/Cargo.lock b/webserver/Cargo.lock index ea3ad9af..c3d6fe88 100644 --- a/webserver/Cargo.lock +++ b/webserver/Cargo.lock @@ -117,7 +117,7 @@ dependencies = [ "actix-utils", "futures-core", "futures-util", - "mio", + "mio 0.8.11", "socket2", "tokio", "tracing", @@ -248,6 +248,17 @@ dependencies = [ "alloc-no-stdlib", ] +[[package]] +name = "async-trait" +version = "0.1.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + [[package]] name = "autocfg" version = "1.2.0" @@ -329,6 +340,12 @@ version = "3.16.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + [[package]] name = "bytes" version = "1.6.0" @@ -452,6 +469,7 @@ checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" dependencies = [ "block-buffer", "crypto-common", + "subtle", ] [[package]] @@ -479,6 +497,12 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "fallible-iterator" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7" + [[package]] name = "fastrand" version = "2.1.0" @@ -532,6 +556,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", + "futures-sink", ] [[package]] @@ -540,6 +565,17 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" +[[package]] +name = "futures-macro" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.60", +] + [[package]] name = "futures-sink" version = "0.3.30" @@ -559,6 +595,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-core", + "futures-macro", + "futures-sink", "futures-task", "pin-project-lite", "pin-utils", @@ -636,6 +674,21 @@ version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + +[[package]] +name = "hmac" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c49c37c09c17a53d937dfbb742eb3a961d65a994e6bcdcf37e7399d0cc8ab5e" +dependencies = [ + "digest", +] + [[package]] name = "http" version = "0.2.12" @@ -856,6 +909,16 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.7.2" @@ -889,6 +952,18 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "mio" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" +dependencies = [ + "hermit-abi", + "libc", + "wasi", + "windows-sys 0.52.0", +] + [[package]] name = "mutually_exclusive_features" version = "0.0.3" @@ -1029,6 +1104,24 @@ version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "phf" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ade2d8b8f33c7333b51bcf0428d37e217e9f32192ae4772156f65063b8ce03dc" +dependencies = [ + "phf_shared", +] + +[[package]] +name = "phf_shared" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90fcb95eef784c2ac79119d1dd819e162b5da872ce6f3c3abe1e8ca1c082f72b" +dependencies = [ + "siphasher", +] + [[package]] name = "pin-project" version = "1.1.5" @@ -1067,6 +1160,49 @@ version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" +[[package]] +name = "postgres" +version = "0.19.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c9ec84ab55b0f9e418675de50052d494ba893fd28c65769a6e68fcdacbee2b8" +dependencies = [ + "bytes", + "fallible-iterator", + "futures-util", + "log", + "tokio", + "tokio-postgres", +] + +[[package]] +name = "postgres-protocol" +version = "0.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acda0ebdebc28befa84bee35e651e4c5f09073d668c7aed4cf7e23c3cda84b23" +dependencies = [ + "base64 0.22.0", + "byteorder", + "bytes", + "fallible-iterator", + "hmac", + "md-5", + "memchr", + "rand", + "sha2", + "stringprep", +] + +[[package]] +name = "postgres-types" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02048d9e032fb3cc3413bbf7b83a15d84a5d419778e2628751896d856498eee9" +dependencies = [ + "bytes", + "fallible-iterator", + "postgres-protocol", +] + [[package]] name = "powerfmt" version = "0.2.0" @@ -1213,8 +1349,12 @@ version = "0.1.0" dependencies = [ "actix-cors", "actix-web", + "postgres", "reqwest", "serde", + "serde_json", + "tokio", + "tokio-postgres", "tracing", "tracing-actix-web", "tracing-subscriber", @@ -1336,11 +1476,12 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.116" +version = "1.0.128" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e17db7126d17feb94eb3fad46bf1a96b034e8aacbc2e775fe81505f8b0b2813" +checksum = "6ff5456707a1de34e7e37f2a6fd3d3f808c318259cbd01ab6377795054b483d8" dependencies = [ "itoa", + "memchr", "ryu", "serde", ] @@ -1368,6 +1509,17 @@ dependencies = [ "digest", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "sharded-slab" version = "0.1.7" @@ -1386,6 +1538,12 @@ dependencies = [ "libc", ] +[[package]] +name = "siphasher" +version = "0.3.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38b58827f4464d87d377d175e90bf58eb00fd8716ff0a62f80356b5e61555d0d" + [[package]] name = "slab" version = "0.4.9" @@ -1411,6 +1569,23 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "stringprep" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b4df3d392d81bd458a8a621b8bffbd2302a12ffe288a9d931670948749463b1" +dependencies = [ + "unicode-bidi", + "unicode-normalization", + "unicode-properties", +] + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "1.0.109" @@ -1530,19 +1705,19 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.37.0" +version = "1.40.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1adbebffeca75fcfd058afa480fb6c0b81e165a0323f9c9d39c9697e37c46787" +checksum = "e2b070231665d27ad9ec9b8df639893f46727666c6767db40317fbe920a5d998" dependencies = [ "backtrace", "bytes", "libc", - "mio", + "mio 1.0.2", "parking_lot", "pin-project-lite", "signal-hook-registry", "socket2", - "windows-sys 0.48.0", + "windows-sys 0.52.0", ] [[package]] @@ -1555,6 +1730,32 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-postgres" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03adcf0147e203b6032c0b2d30be1415ba03bc348901f3ff1cc0df6a733e60c3" +dependencies = [ + "async-trait", + "byteorder", + "bytes", + "fallible-iterator", + "futures-channel", + "futures-util", + "log", + "parking_lot", + "percent-encoding", + "phf", + "pin-project-lite", + "postgres-protocol", + "postgres-types", + "rand", + "socket2", + "tokio", + "tokio-util", + "whoami", +] + [[package]] name = "tokio-util" version = "0.7.10" @@ -1701,6 +1902,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-properties" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ea75f83c0137a9b98608359a5f1af8144876eb67bcb1ce837368e906a9f524" + [[package]] name = "url" version = "2.5.0" @@ -1754,6 +1961,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.92" @@ -1830,6 +2043,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "whoami" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d" +dependencies = [ + "redox_syscall", + "wasite", + "web-sys", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/webserver/Cargo.toml b/webserver/Cargo.toml index 5c824a3c..eeffa162 100644 --- a/webserver/Cargo.toml +++ b/webserver/Cargo.toml @@ -13,3 +13,7 @@ tracing = "0.1.37" actix-cors = "0.7.0" reqwest = { version = "0.12.4", features = ["json"] } serde = { version = "1.0.199", features = ["derive"] } +postgres = "0.19.8" +tokio = "1.40.0" +tokio-postgres = "0.7.11" +serde_json = "1.0.128" diff --git a/webserver/src/main.rs b/webserver/src/main.rs index e0a91147..c15bf8bd 100644 --- a/webserver/src/main.rs +++ b/webserver/src/main.rs @@ -1,7 +1,9 @@ use actix_cors::Cors; -use actix_web::{http::header, post, web, App, HttpResponse, HttpServer, Responder}; -use reqwest::Client; +use actix_web::{http::header, get, post, web, App, HttpResponse, HttpServer, Responder}; use serde::Deserialize; +use serde_json::json; +use reqwest::Client; +use tokio_postgres::{NoTls}; const LINTER_BASE_URL: &str = "http://linter:18081"; const SANDBOX_BASE_URL: &str = "http://sandbox:18080"; @@ -83,6 +85,83 @@ async fn linter(data: web::Path<(String, String)>) -> impl Responder { } } +async fn get_algorithms(case: String, uid: Option) -> impl Responder { + let (postgres_client, connection) = match tokio_postgres::connect("host=localhost user=postgres password=yourpassword dbname=nutc", NoTls).await { + Ok((client, connection)) => (client, connection), + Err(e) => { + eprintln!("Failed to connect to the database: {}", e); + return HttpResponse::InternalServerError().finish(); + } + }; + + actix_web::rt::spawn(async move { + if let Err(e) = connection.await { + eprintln!("connection error: {}", e); + } + }); + + let query = r#" + WITH "MostRecentAlgos" AS ( + WITH "FilteredAlgosByCase" AS ( + SELECT a."uid", f."key", f."createdAt" AS "timestamp" + FROM "algos" AS a + INNER JOIN "AlgoFile" AS f ON a."algoFileKey" = f."key" + WHERE a."case" = $1 + AND ($2::text IS NULL OR a."uid" = $2) + ) + SELECT "uid", MAX("timestamp") AS "timestamp" + FROM "FilteredAlgosByCase" + GROUP BY "uid" + ) + SELECT + p."firstName" || ' ' || p."lastName" AS "name", + a."uid", + a."key" + FROM "profiles" AS p + INNER JOIN ( + SELECT u."uid", f."key" + FROM "MostRecentAlgos" AS u + INNER JOIN "AlgoFile" AS f ON u."timestamp" = f."createdAt" + ) AS a ON p."uid" = a."uid"; + "#; + + let params: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> = vec![&case, &uid]; + + let rows = match postgres_client.query(query, ¶ms).await { + Ok(rows) => rows, + Err(e) => { + eprintln!("query failed: {}", e); + return HttpResponse::InternalServerError().finish(); + } + }; + + let response_data: Vec = rows + .into_iter() + .map(|row| { + let values: Vec = (0..row.len()) + .map(|i| row.get::>(i).unwrap_or_else(|| "NULL".to_string())) + .collect(); + json!(values) + }) + .collect(); + + HttpResponse::Ok().json(response_data) +} + +#[tracing::instrument] +#[get("/algorithms/{case}/{uid}")] +async fn get_single_user_algorithm(data: web::Path<(String, String)>) -> impl Responder { + let (case, uid) = data.into_inner(); + get_algorithms(case, Some(uid)).await +} + +#[tracing::instrument] +#[get("/algorithms/{case}")] +async fn get_all_user_algorithms(data: web::Path) -> impl Responder { + let case = data.into_inner(); + get_algorithms(case, None).await +} + #[actix_web::main] async fn main() -> std::io::Result<()> { tracing_subscriber::fmt() @@ -110,7 +189,11 @@ async fn main() -> std::io::Result<()> { header::CONNECTION, ]) .supports_credentials(); - App::new().service(linter).wrap(cors) + App::new() + .service(linter) + .service(get_single_user_algorithm) + .service(get_all_user_algorithms) + .wrap(cors) }) .bind(("0.0.0.0", 16124))? .run() From b7803df4b6b7d56fb37edca810468018d699475f Mon Sep 17 00:00:00 2001 From: Max Glass Date: Thu, 19 Sep 2024 21:39:55 -0600 Subject: [PATCH 2/3] Update database column names, use env vars --- webserver/.gitignore | 1 + webserver/Cargo.lock | 54 +++++++++++++++++++++++++ webserver/Cargo.toml | 2 + webserver/src/main.rs | 91 ++++++++++++++++++++++++++----------------- 4 files changed, 113 insertions(+), 35 deletions(-) diff --git a/webserver/.gitignore b/webserver/.gitignore index 2f7896d1..14ee5009 100644 --- a/webserver/.gitignore +++ b/webserver/.gitignore @@ -1 +1,2 @@ target/ +.env diff --git a/webserver/Cargo.lock b/webserver/Cargo.lock index c3d6fe88..c0048ddd 100644 --- a/webserver/Cargo.lock +++ b/webserver/Cargo.lock @@ -439,6 +439,40 @@ dependencies = [ "typenum", ] +[[package]] +name = "deadpool" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6541a3916932fe57768d4be0b1ffb5ec7cbf74ca8c903fdfd5c0fe8aa958f0ed" +dependencies = [ + "deadpool-runtime", + "num_cpus", + "tokio", +] + +[[package]] +name = "deadpool-postgres" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ab8a4ea925ce79678034870834602a2980f4b88c09e97feb266496dbb4493d2" +dependencies = [ + "async-trait", + "deadpool", + "getrandom", + "tokio", + "tokio-postgres", + "tracing", +] + +[[package]] +name = "deadpool-runtime" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "092966b41edc516079bdf31ec78a2e0588d1d0c08f78b91d8307215928642b2b" +dependencies = [ + "tokio", +] + [[package]] name = "deranged" version = "0.3.11" @@ -472,6 +506,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "encoding_rs" version = "0.8.34" @@ -620,8 +660,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "94b22e06ecb0110981051723910cbf0b5f5e09a2062dd7663334ee79a9d1286c" dependencies = [ "cfg-if", + "js-sys", "libc", "wasi", + "wasm-bindgen", ] [[package]] @@ -1004,6 +1046,16 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" +[[package]] +name = "num_cpus" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4161fcb6d602d4d2081af7c3a45852d875a03dd337a6bfdd6e06407b61342a43" +dependencies = [ + "hermit-abi", + "libc", +] + [[package]] name = "object" version = "0.32.2" @@ -1349,6 +1401,8 @@ version = "0.1.0" dependencies = [ "actix-cors", "actix-web", + "deadpool-postgres", + "dotenv", "postgres", "reqwest", "serde", diff --git a/webserver/Cargo.toml b/webserver/Cargo.toml index eeffa162..73152e5c 100644 --- a/webserver/Cargo.toml +++ b/webserver/Cargo.toml @@ -17,3 +17,5 @@ postgres = "0.19.8" tokio = "1.40.0" tokio-postgres = "0.7.11" serde_json = "1.0.128" +dotenv = "0.15.0" +deadpool-postgres = "0.14.0" diff --git a/webserver/src/main.rs b/webserver/src/main.rs index c15bf8bd..c57cf9f8 100644 --- a/webserver/src/main.rs +++ b/webserver/src/main.rs @@ -1,22 +1,14 @@ use actix_cors::Cors; -use actix_web::{http::header, get, post, web, App, HttpResponse, HttpServer, Responder}; +use actix_web::{get, http::header, post, web, App, HttpResponse, HttpServer, Responder}; +use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod}; +use reqwest::Client; use serde::Deserialize; use serde_json::json; -use reqwest::Client; -use tokio_postgres::{NoTls}; +use tokio_postgres::NoTls; const LINTER_BASE_URL: &str = "http://linter:18081"; const SANDBOX_BASE_URL: &str = "http://sandbox:18080"; -// #[repr(i32)] -// #[derive(Deserialize, Debug, PartialEq)] -// pub enum LintingResultOption { -// Unknown = -1, -// Failure = 0, -// Success = 1, -// Pending = 2, -// } - #[derive(Deserialize, Debug)] pub struct LinterResponse { pub linting_status: i32, @@ -85,27 +77,21 @@ async fn linter(data: web::Path<(String, String)>) -> impl Responder { } } -async fn get_algorithms(case: String, uid: Option) -> impl Responder { - let (postgres_client, connection) = match tokio_postgres::connect("host=localhost user=postgres password=yourpassword dbname=nutc", NoTls).await { - Ok((client, connection)) => (client, connection), +async fn get_algorithms(case: String, uid: Option, pool: &Pool) -> impl Responder { + let postgres_client = match pool.get().await { + Ok(client) => client, Err(e) => { eprintln!("Failed to connect to the database: {}", e); return HttpResponse::InternalServerError().finish(); } }; - actix_web::rt::spawn(async move { - if let Err(e) = connection.await { - eprintln!("connection error: {}", e); - } - }); - let query = r#" WITH "MostRecentAlgos" AS ( WITH "FilteredAlgosByCase" AS ( - SELECT a."uid", f."key", f."createdAt" AS "timestamp" + SELECT a."uid", f."s3Key", f."createdAt" AS "timestamp" FROM "algos" AS a - INNER JOIN "AlgoFile" AS f ON a."algoFileKey" = f."key" + INNER JOIN "algo_file" AS f ON a."algoFileS3Key" = f."s3Key" WHERE a."case" = $1 AND ($2::text IS NULL OR a."uid" = $2) ) @@ -115,18 +101,17 @@ async fn get_algorithms(case: String, uid: Option) -> impl Responder { ) SELECT p."firstName" || ' ' || p."lastName" AS "name", - a."uid", - a."key" + a."s3Key" FROM "profiles" AS p INNER JOIN ( - SELECT u."uid", f."key" + SELECT u."uid", f."s3Key" FROM "MostRecentAlgos" AS u - INNER JOIN "AlgoFile" AS f ON u."timestamp" = f."createdAt" + INNER JOIN "algo_file" AS f ON u."timestamp" = f."createdAt" ) AS a ON p."uid" = a."uid"; "#; - + let params: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> = vec![&case, &uid]; - + let rows = match postgres_client.query(query, ¶ms).await { Ok(rows) => rows, Err(e) => { @@ -139,7 +124,10 @@ async fn get_algorithms(case: String, uid: Option) -> impl Responder { .into_iter() .map(|row| { let values: Vec = (0..row.len()) - .map(|i| row.get::>(i).unwrap_or_else(|| "NULL".to_string())) + .map(|i| { + row.get::>(i) + .unwrap_or_else(|| "NULL".to_string()) + }) .collect(); json!(values) }) @@ -150,20 +138,51 @@ async fn get_algorithms(case: String, uid: Option) -> impl Responder { #[tracing::instrument] #[get("/algorithms/{case}/{uid}")] -async fn get_single_user_algorithm(data: web::Path<(String, String)>) -> impl Responder { +async fn get_single_user_algorithm( + data: web::Path<(String, String)>, + db_pool: web::Data, +) -> impl Responder { let (case, uid) = data.into_inner(); - get_algorithms(case, Some(uid)).await + get_algorithms(case, Some(uid), &db_pool).await } #[tracing::instrument] #[get("/algorithms/{case}")] -async fn get_all_user_algorithms(data: web::Path) -> impl Responder { +async fn get_all_user_algorithms( + data: web::Path, + db_pool: web::Data, +) -> impl Responder { let case = data.into_inner(); - get_algorithms(case, None).await + get_algorithms(case, None, &db_pool).await } #[actix_web::main] async fn main() -> std::io::Result<()> { + dotenv::dotenv().ok(); + + let host = + std::env::var("WEBSERVER_DB_HOST").expect("env variable `WEBSERVER_DB_HOST` should be set"); + let user = + std::env::var("WEBSERVER_DB_USER").expect("env variable `WEBSERVER_DB_USER` should be set"); + let password = std::env::var("WEBSERVER_DB_PASSWORD") + .expect("env variable `WEBSERVER_DB_PASSWORD` should be set"); + let dbname = + std::env::var("WEBSERVER_DB_NAME").expect("env variable `WEBSERVER_DB_NAME` should be set"); + + let mut pg_config = tokio_postgres::Config::new(); + pg_config.host(host); + pg_config.user(user); + pg_config.password(password); + pg_config.dbname(dbname); + let mgr_config = ManagerConfig { + recycling_method: RecyclingMethod::Fast, + }; + + let pool = Pool::builder(Manager::from_config(pg_config, NoTls, mgr_config)) + .max_size(16) + .build() + .expect("Failed to connect to DB"); + tracing_subscriber::fmt() .event_format( tracing_subscriber::fmt::format() @@ -175,8 +194,9 @@ async fn main() -> std::io::Result<()> { tracing::info!("Starting server."); - HttpServer::new(|| { + HttpServer::new(move || { let cors = Cors::default() + .allowed_origin("http://localhost:3000") .allowed_origin("http://localhost:3001") .allowed_origin("https://nutc.io") .allowed_origin("https://www.nutc.io") @@ -190,6 +210,7 @@ async fn main() -> std::io::Result<()> { ]) .supports_credentials(); App::new() + .app_data(web::Data::new(pool.clone())) .service(linter) .service(get_single_user_algorithm) .service(get_all_user_algorithms) From 7f4df70eb7fa7a1b8a32ad7c1e2d63b410d1170a Mon Sep 17 00:00:00 2001 From: Max Glass Date: Thu, 19 Sep 2024 22:44:39 -0600 Subject: [PATCH 3/3] add prisma studio to webserver --- web/dev/docker-compose.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/web/dev/docker-compose.yml b/web/dev/docker-compose.yml index f2e6fc61..fb7bdf09 100644 --- a/web/dev/docker-compose.yml +++ b/web/dev/docker-compose.yml @@ -40,6 +40,17 @@ services: volumes: - postgres_data:/var/lib/postgresql/data + prisma-studio: + image: node:latest + working_dir: /app + volumes: + - ..:/app + command: ["npx", "prisma", "studio"] + ports: + - "5555:5555" # You can customize this port if needed + depends_on: + - postgres + volumes: postgres_data: localstack: