From cd15708002c84a55df61e7d4f088649d059777cd Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 12:35:25 -0400 Subject: [PATCH 1/6] Added .env docker sample file --- .env.docker.sample | 6 ++++++ .gitignore | 1 + 2 files changed, 7 insertions(+) create mode 100644 .env.docker.sample diff --git a/.env.docker.sample b/.env.docker.sample new file mode 100644 index 0000000..30ccbb4 --- /dev/null +++ b/.env.docker.sample @@ -0,0 +1,6 @@ +SECRET_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h +POSTGRES_USER=icarus_op_test +POSTGRES_PASSWORD=password +POSTGRES_DB=icarus_auth_test_db +POSTGRES_HOST=localhost +DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB} \ No newline at end of file diff --git a/.gitignore b/.gitignore index e551aa3..7394036 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /target Cargo.lock .env +.env.docker -- 2.43.0 From 706080f9a793b3e8289e5be8b446e8cd440dd4b7 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 12:36:33 -0400 Subject: [PATCH 2/6] Updated env docker sample file --- .env.docker.sample | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env.docker.sample b/.env.docker.sample index 30ccbb4..7e892f0 100644 --- a/.env.docker.sample +++ b/.env.docker.sample @@ -2,5 +2,5 @@ SECRET_KEY=refero34o8rfhfjn983thf39fhc943rf923n3h POSTGRES_USER=icarus_op_test POSTGRES_PASSWORD=password POSTGRES_DB=icarus_auth_test_db -POSTGRES_HOST=localhost -DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB} \ No newline at end of file +POSTGRES_HOST=auth_db +DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:5432/${POSTGRES_DB} -- 2.43.0 From 6fed4ae51fcd3af8de651a42e55f49f79ba3ac6d Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 12:40:49 -0400 Subject: [PATCH 3/6] Changes to docker file --- .dockerignore.yaml | 7 +++++-- Dockerfile | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.dockerignore.yaml b/.dockerignore.yaml index dfcd3fa..b21695d 100644 --- a/.dockerignore.yaml +++ b/.dockerignore.yaml @@ -8,7 +8,10 @@ pkg/ .gitea/ # Ignore environment files (configure via docker-compose instead) -.env* +.env +.env.sample +.env.docker +.env.docker.sample # Ignore IDE/editor specific files .idea/ @@ -18,4 +21,4 @@ pkg/ *.DS_Store # Add any other files/directories you don't need in the image -# e.g., logs/, tmp/ \ No newline at end of file +# e.g., logs/, tmp/ diff --git a/Dockerfile b/Dockerfile index c1b1081..5c144a5 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,7 +34,7 @@ RUN --mount=type=ssh mkdir src && \ # Copy the actual source code COPY src ./src # If you have other directories like `templates` or `static`, copy them too -COPY .env ./.env +COPY .env.docker ./.env COPY migrations ./migrations # << --- SSH MOUNT ADDED HERE --- >> @@ -68,4 +68,4 @@ EXPOSE 3000 # Set the command to run your application # Ensure this matches the binary name copied above -CMD ["./icarus_auth"] \ No newline at end of file +CMD ["./icarus_auth"] -- 2.43.0 From 62c57c72629073e4afc6fa5c8857234fc21f42fe Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 12:45:35 -0400 Subject: [PATCH 4/6] Version bump --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index c4d8cf2..dcc377e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "icarus_auth" -version = "0.3.4" +version = "0.3.6" edition = "2024" rust-version = "1.86" -- 2.43.0 From fcbd077e51f3fe13ba840128522c15b2f84ab0c6 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 13:04:04 -0400 Subject: [PATCH 5/6] Fixed issue --- src/lib.rs | 2 +- src/main.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ca334c5..c81cfea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -34,8 +34,8 @@ pub mod db { } async fn get_db_url() -> String { - #[cfg(debug_assertions)] // Example: Only load .env in debug builds dotenvy::dotenv().ok(); + // dotenvy::from_filename(".env").ok(); env::var(keys::DBURL).expect(keys::error::ERROR) } diff --git a/src/main.rs b/src/main.rs index 48239d6..27b8bb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -74,6 +74,7 @@ mod tests { pub const LIMIT: usize = 6; pub async fn get_pool() -> Result { + dotenvy::dotenv().ok(); // Load .env file if it exists let tm_db_url = std::env::var(keys::DBURL).expect("DATABASE_URL must be present"); let tm_options = sqlx::postgres::PgConnectOptions::from_str(&tm_db_url).unwrap(); sqlx::PgPool::connect_with(tm_options).await -- 2.43.0 From d0a57193e2a50536040fcef71e5b7a532aad6345 Mon Sep 17 00:00:00 2001 From: phoenix Date: Sun, 27 Apr 2025 13:49:52 -0400 Subject: [PATCH 6/6] Saving changes --- .dockerignore | 2 ++ Dockerfile | 9 +++++++-- docker-compose.yaml | 4 +++- src/lib.rs | 21 ++++++++++++++++++--- 4 files changed, 30 insertions(+), 6 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f70fa48 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +.env +!/.env.docker diff --git a/Dockerfile b/Dockerfile index 5c144a5..199a162 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,9 +34,14 @@ RUN --mount=type=ssh mkdir src && \ # Copy the actual source code COPY src ./src # If you have other directories like `templates` or `static`, copy them too -COPY .env.docker ./.env +COPY .env.docker ./.env.docker COPY migrations ./migrations +# Explicitly expose the env var +# ENV DATABASE_URL=${DATABASE_URL} +ARG ENV_FILE=.env.docker +COPY ${ENV_FILE} ./.env.docker + # << --- SSH MOUNT ADDED HERE --- >> # Build *only* dependencies to leverage Docker cache # This dummy build caches dependencies as a separate layer @@ -60,7 +65,7 @@ COPY --from=builder /usr/src/app/target/release/icarus_auth . # Copy other necessary files like .env (if used for runtime config) or static assets # It's generally better to configure via environment variables in Docker though -COPY --from=builder /usr/src/app/.env . +COPY --from=builder /usr/src/app/.env.docker . COPY --from=builder /usr/src/app/migrations ./migrations # Expose the port your Axum app listens on (e.g., 3000 or 8000) diff --git a/docker-compose.yaml b/docker-compose.yaml index ed72c2b..e350c3a 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -11,7 +11,9 @@ services: # Map host port 8000 to container port 3000 (adjust as needed) - "8000:3000" env_file: - - .env + - .env.docker + environment: + - DATABASE_URL=${DATABASE_URL} depends_on: auth_db: condition: service_healthy # Wait for the DB to be healthy before starting the app diff --git a/src/lib.rs b/src/lib.rs index c81cfea..8821f9d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,6 @@ mod connection_settings { pub mod db { use sqlx::postgres::PgPoolOptions; - use std::env; use crate::{connection_settings, keys}; @@ -34,9 +33,25 @@ pub mod db { } async fn get_db_url() -> String { - dotenvy::dotenv().ok(); + // dotenvy::dotenv().ok(); // dotenvy::from_filename(".env").ok(); - env::var(keys::DBURL).expect(keys::error::ERROR) + // env::var(keys::DBURL).expect(keys::error::ERROR) + + /* + std::env::var(keys::DBURL) + .or_else(|_| { + dotenvy::dotenv().ok(); + dotenvy::var(keys::DBURL) + }) + .expect("DATABASE_URL must be set in environment or .env file") + */ + if let Err(_) = dotenvy::from_filename(".env.docker") { + eprintln!("Note: .env.docker not found, using system env vars"); + dotenvy::dotenv().ok(); + std::env::var(keys::DBURL).expect(keys::error::ERROR) + } else { + std::env::var(keys::DBURL).expect(keys::error::ERROR) + } } pub async fn migrations(pool: &sqlx::PgPool) { -- 2.43.0