From 4f9bd23495a3a2fa5ad3d3cad1b2b8f1af16ccb0 Mon Sep 17 00:00:00 2001 From: Patrick Meade Date: Thu, 8 Aug 2024 17:59:48 -0500 Subject: [PATCH] Now authenticating with the correct database --- Cargo.lock | 8 ++++---- Cargo.toml | 2 +- bin/build-docker | 8 +++++++- bin/run-app | 10 +++++++--- bin/run-app-docker | 29 ++++++++++++++++++++++------- bin/run-mongo-docker | 26 +++++++++++++++++++------- bin/run-mongo-shell-docker | 13 ++++++++++++- src/context.rs | 17 ++++++++++++++--- src/database.rs | 38 ++++++++++++++++++-------------------- src/error.rs | 4 ++-- src/main.rs | 11 ++++++----- 11 files changed, 112 insertions(+), 54 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e361460..fd7c0e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1572,9 +1572,9 @@ checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3" [[package]] name = "serde" -version = "1.0.204" +version = "1.0.205" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bc76f558e0cbb2a839d37354c575f1dc3fdc6546b5be373ba43d95f231bf7c12" +checksum = "e33aedb1a7135da52b7c21791455563facbbcc43d0f0f66165b42c21b3dfb150" dependencies = [ "serde_derive", ] @@ -1590,9 +1590,9 @@ dependencies = [ [[package]] name = "serde_derive" -version = "1.0.204" +version = "1.0.205" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0cd7e117be63d3c3678776753929474f3b04a43a080c744d6b0ae2a8c28e222" +checksum = "692d6f5ac90220161d6774db30c662202721e64aed9058d2c394f451261420c1" dependencies = [ "proc-macro2", "quote", diff --git a/Cargo.toml b/Cargo.toml index 37bdbf3..8ce2916 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,6 @@ gotham = "0.7.4" gotham_restful = "0.9.0" log = "0.4.22" mongodb = "3.0.1" -serde = "1.0.204" +serde = "1.0.205" serde_json = "1.0.122" tokio = { version = "1.39.2", features = [ "full" ] } diff --git a/bin/build-docker b/bin/build-docker index 00f227d..4e663ea 100755 --- a/bin/build-docker +++ b/bin/build-docker @@ -1,7 +1,13 @@ #!/usr/bin/env bash # build-docker +# use `docker buildx build` to create a Docker image of the application +# by default the image is `wipac-disk-tracking:latest-SNAPSHOT` unless +# you set the BUILD_TAG environment variable before calling the script + +: ${BUILD_TAG:="latest-SNAPSHOT"} + docker buildx build \ --file Dockerfile \ - --tag wipac-disk-tracking:latest-SNAPSHOT \ + --tag wipac-disk-tracking:${BUILD_TAG} \ . diff --git a/bin/run-app b/bin/run-app index dafafd8..f63a497 100755 --- a/bin/run-app +++ b/bin/run-app @@ -1,12 +1,16 @@ #!/usr/bin/env bash # run-app +# use `cargo run` to run an instance of the application locally +# when starting this script, you can provide one of the environment +# variables listed below to override the defaults + export MONGODB_DATABASE=${MONGODB_DATABASE:="disk_tracking"} export MONGODB_HOSTNAME=${MONGODB_HOSTNAME:="localhost"} export MONGODB_PASSWORD=${MONGODB_PASSWORD:="hunter2"} -export MONGODB_PORT=${MONGODB_PORT:="27017"} -# export MONGODB_USERNAME=${MONGODB_USERNAME:="disk_tracking"} -export MONGODB_USERNAME=${MONGODB_USERNAME:="root"} +export MONGODB_PORT_NUMBER=${MONGODB_PORT_NUMBER:="27017"} +export MONGODB_USERNAME=${MONGODB_USERNAME:="disk_tracking"} +export PORT=${PORT:="8080"} export RUST_LOG=${RUST_LOG:="debug"} cargo run --bin wipac-disk-tracking diff --git a/bin/run-app-docker b/bin/run-app-docker index bd1efff..f481e8a 100755 --- a/bin/run-app-docker +++ b/bin/run-app-docker @@ -1,15 +1,30 @@ #!/usr/bin/env bash # run-app-docker +# use `docker run` to run an instance of the application in a Docker +# container. when starting this script, you can provide one of the +# environment variables listed below to override the defaults + +: ${DOCKER_CONTAINER_NAME:="disk_tracking"} +: ${DOCKER_MONGO_CONTAINER_NAME:="disk_tracking_mongo"} +: ${MONGODB_DATABASE:="disk_tracking"} +: ${MONGODB_HOSTNAME:="localhost"} +: ${MONGODB_PASSWORD:="hunter2"} +: ${MONGODB_USERNAME:="disk_tracking"} +: ${MONGODB_PORT_NUMBER:="27017"} +: ${PORT:="8080"} + docker run \ --detach \ - --env=MONGODB_HOSTNAME=disk_tracking_mongo \ - --env=MONGODB_PASSWORD=hunter2 \ - --env=MONGODB_PORT=27017 \ - --env=MONGODB_USERNAME=disk_tracking \ + --env=MONGODB_DATABASE=${MONGODB_DATABASE} \ + --env=MONGODB_HOSTNAME=${DOCKER_MONGO_CONTAINER_NAME} \ + --env=MONGODB_PASSWORD=${MONGODB_PASSWORD} \ + --env=MONGODB_PORT_NUMBER=${MONGODB_PORT_NUMBER} \ + --env=MONGODB_USERNAME=${MONGODB_USERNAME} \ + --env=PORT=${PORT} \ --env=RUST_LOG=debug \ - --link disk_tracking_mongo:disk_tracking_mongo \ - --name=disk_tracking \ - --publish 8080:8080 \ + --link ${DOCKER_MONGO_CONTAINER_NAME}:${DOCKER_MONGO_CONTAINER_NAME} \ + --name=${DOCKER_CONTAINER_NAME} \ + --publish ${PORT}:${PORT} \ --rm \ wipac-disk-tracking:latest-SNAPSHOT diff --git a/bin/run-mongo-docker b/bin/run-mongo-docker index 6c0a011..13cb75f 100755 --- a/bin/run-mongo-docker +++ b/bin/run-mongo-docker @@ -1,13 +1,25 @@ #!/usr/bin/env bash # run-mongo-docker - # --detach \ - # --rm \ +# use `docker run` to create a TEMPORARY instance of MongoDB in a +# Docker container for development/testing purposes. when starting +# this script, you can provide one of the environment variables listed +# below to override the defaults + +: ${DOCKER_CONTAINER_NAME:="disk_tracking_mongo"} +: ${MONGODB_DATABASE:="disk_tracking"} +: ${MONGODB_PASSWORD:="hunter2"} +: ${MONGODB_USERNAME:="disk_tracking"} +: ${MONGODB_PORT_NUMBER:="27017"} + docker run \ - --env=MONGODB_DATABASE=disk_tracking \ - --env=MONGODB_PASSWORD=hunter2 \ + --detach \ + --env=MONGODB_DATABASE=${MONGODB_DATABASE} \ + --env=MONGODB_PASSWORD=${MONGODB_PASSWORD} \ --env=MONGODB_ROOT_PASSWORD=hunter2 \ - --env=MONGODB_USERNAME=disk_tracking \ - --name=disk_tracking_mongo \ - --publish 27017:27017 \ + --env=MONGODB_USERNAME=${MONGODB_USERNAME} \ + --env=MONGODB_PORT_NUMBER=${MONGODB_PORT_NUMBER} \ + --name=${DOCKER_CONTAINER_NAME} \ + --publish ${MONGODB_PORT_NUMBER}:${MONGODB_PORT_NUMBER} \ + --rm \ bitnami/mongodb:latest diff --git a/bin/run-mongo-shell-docker b/bin/run-mongo-shell-docker index 330e505..4609ae3 100755 --- a/bin/run-mongo-shell-docker +++ b/bin/run-mongo-shell-docker @@ -1,10 +1,21 @@ #!/usr/bin/env bash # run-mongo-shell-docker +# use `docker run` to create an interactive mongoshell that connects to +# the TEMPORARY instance of MongoDB for development/testing purposes. +# when starting this script, you can provide one of the environment +# variables listed below to override the defaults + +: ${MONGODB_DATABASE:="disk_tracking"} +: ${MONGODB_HOSTNAME:="localhost"} +: ${MONGODB_PASSWORD:="hunter2"} +: ${MONGODB_USERNAME:="disk_tracking"} +: ${MONGODB_PORT_NUMBER:="27017"} + docker run \ --interactive \ --network="host" \ --rm \ --tty \ mongo:latest \ - mongosh --host localhost:27017 -u "root" -p "hunter2" --authenticationDatabase "admin" + mongosh --host ${MONGODB_HOSTNAME}:${MONGODB_PORT_NUMBER} -u "${MONGODB_USERNAME}" -p "${MONGODB_PASSWORD}" --authenticationDatabase "${MONGODB_DATABASE}" diff --git a/src/context.rs b/src/context.rs index 2d44cfa..c718944 100644 --- a/src/context.rs +++ b/src/context.rs @@ -15,18 +15,29 @@ pub struct ApplicationContext { impl ApplicationContext { pub fn get_mongo_url(&self) -> String { - format!("mongodb://{}:{}@{}:{}/", self.mongo_user, self.mongo_password, self.mongo_host, self.mongo_port) + format!( + "mongodb://{}:{}@{}:{}/{}", + self.mongo_user, + self.mongo_password, + self.mongo_host, + self.mongo_port, + self.mongo_database + ) } } pub fn build_context() -> Result { // read database parameters from the environment let mongo_host = env::var("MONGODB_HOSTNAME").unwrap_or_else(|_| "localhost".into()); - let mongo_port = env::var("MONGODB_PORT").unwrap_or_else(|_| "27017".into()); + let mongo_port = env::var("MONGODB_PORT_NUMBER").unwrap_or_else(|_| "27017".into()); let mongo_user = env::var("MONGODB_USERNAME").unwrap_or_else(|_| "disk_tracking".into()); let mongo_database = env::var("MONGO_DB_NAME").unwrap_or_else(|_| "disk_tracking".into()); let mongo_password = match env::var("MONGODB_PASSWORD") { - Err(_) => return Err(ContextError("environment variable MONGODB_PASSWORD not defined".to_string())), + Err(_) => { + return Err(ContextError( + "environment variable MONGODB_PASSWORD not defined".to_string(), + )) + } Ok(x) => x, }; diff --git a/src/database.rs b/src/database.rs index a635b7c..087d0b5 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,6 @@ // database.rs -use mongodb::{bson::document::Document, Client, options::ClientOptions}; +use mongodb::{bson::document::Document, options::ClientOptions, Client}; use crate::context::ApplicationContext; use crate::error::Result; @@ -19,25 +19,23 @@ pub async fn setup_mongo(context: &ApplicationContext) -> Result { pub async fn count_disk_events(context: &ApplicationContext, client: &Client) -> Result { let database = client.database(&context.mongo_database); let collection = database.collection::(DISK_EVENT_COLLECTION); - let count = collection.count_documents(Document::new()).await?; + let count = collection.count_documents(Document::new()).await?; Ok(count) -// match collection.count_documents(None, None).await { -// Ok(count) => { -// let body = json!({ -// "status": "ok", -// "count": count, -// }); -// (StatusCode::OK, body.to_string()) -// }, -// Err(_) => { -// let body = json!({ -// "status": "error", -// "message": "Failed to connect to the database", -// }); -// (StatusCode::INTERNAL_SERVER_ERROR, body.to_string()) -// }, -// } - + // match collection.count_documents(None, None).await { + // Ok(count) => { + // let body = json!({ + // "status": "ok", + // "count": count, + // }); + // (StatusCode::OK, body.to_string()) + // }, + // Err(_) => { + // let body = json!({ + // "status": "error", + // "message": "Failed to connect to the database", + // }); + // (StatusCode::INTERNAL_SERVER_ERROR, body.to_string()) + // }, + // } } - diff --git a/src/error.rs b/src/error.rs index 3e15333..bb70aa9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -24,10 +24,10 @@ pub fn get_error_message(e: ApplicationError) -> String { match e { ApplicationError::ContextError(x) => { format!("disk-tracking: Unable to build application context: {x}") - }, + } ApplicationError::MongoError(x) => { format!("disk-tracking: Database error: {x}") - }, + } } } diff --git a/src/main.rs b/src/main.rs index 14fd4e3..edaa7b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,11 +10,10 @@ pub mod error; // response::IntoResponse, // http::StatusCode, // }; -use mongodb::{Client, options::ClientOptions}; // use serde_json::json; // use std::net::SocketAddr; -use context::{build_context, ApplicationContext}; +use context::build_context; use database::{count_disk_events, setup_mongo}; use error::{get_error_message, Result}; @@ -22,10 +21,10 @@ use error::{get_error_message, Result}; async fn main() { // initialize logging, configured by environment env_logger::init(); - + // run the application match do_main().await { Err(e) => eprintln!("Error: {}", get_error_message(e)), - Ok(_) => {}, + Ok(_) => {} } } @@ -35,7 +34,9 @@ async fn do_main() -> Result<()> { println!("{:?}", context); println!("{}", context.get_mongo_url()); - let client = setup_mongo(&context).await.expect("Unable to initialize MongoDB client"); + let client = setup_mongo(&context) + .await + .expect("Unable to initialize MongoDB client"); let count = count_disk_events(&context, &client).await?; println!("{}", count);