-
Notifications
You must be signed in to change notification settings - Fork 0
[refactor(config)] Harden config loading, enforce anti-corruption layer, and optimize service internals #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
25cb697
feat: update environment configuration and add engineering standards …
nxdun 8bca0b0
feat: add data models for contributions, health, validation, and ytdlp
nxdun 4b3f427
feat: enhance configuration loading with error handling and add upstr…
nxdun 0e54f1e
fix: update model imports in controllers for consistency
nxdun 1a40c84
refactor: standardize API key and CAPTCHA token handling across middl…
nxdun 2924513
feat: enhance rate limiting logic with improved error handling and do…
nxdun 74b7a11
refactor: update module structure and rename contributions router fun…
nxdun 799617d
refactor: update contributions router function for consistency and im…
nxdun 4771801
refactor: reorganize contributions module structure and improve cachi…
nxdun 1b3234f
refactor: update model imports and improve AppConfig initialization i…
nxdun 7a75f1d
refactor: update cache pruning interval from 600 seconds to 10 minutes
nxdun 67ba737
refactor: update environment variable formatting and enhance error ha…
nxdun 5c23a9f
refactor: optimize job response construction and enhance CORS matcher…
nxdun 2014414
refactor: improve contributions service initialization and enhance va…
nxdun 952b0c1
refactor: standardize environment variable formatting and enhance err…
nxdun 45e38e5
refactor: enhance documentation and suppress clippy warnings in tests
nxdun bd01f7e
refactor: replace hardcoded API key string with constant in unit tests
nxdun a820173
refactor: reorder middleware imports for improved readability
nxdun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,41 +1,48 @@ | ||
| ### ------------------------- | ||
| ### Application Runtime / Local | ||
| ### ------------------------- | ||
| APP_NAME = "REPLACE_WITH_APP_NAME" | ||
| APP_HOST = "REPLACE_WITH_APP_HOST" | ||
| APP_PORT = "REPLACE_WITH_APP_PORT" | ||
| APP_ENV = "REPLACE_WITH_APP_ENV" | ||
| ALLOWED_ORIGINS = "REPLACE_WITH_ALLOWED_ORIGINS" | ||
| RUST_LOG = "REPLACE_WITH_RUST_LOG" | ||
| YTDLP_PATH = "REPLACE_WITH_YTDLP_PATH" | ||
| YTDLP_OUTPUT_DIR = "REPLACE_WITH_YTDLP_OUTPUT_DIR" | ||
| YTDLP_TIMEOUT_SECS = "REPLACE_WITH_YTDLP_TIMEOUT_SECS" | ||
| YTDLP_EXTERNAL_DOWNLOADER = "REPLACE_WITH_YTDLP_EXTERNAL_DOWNLOADER" | ||
| YTDLP_EXTERNAL_DOWNLOADER_ARGS = "REPLACE_WITH_YTDLP_EXTERNAL_DOWNLOADER_ARGS" | ||
| CAPTCHA_SECRET_KEY = "REPLACE_WITH_CAPTCHA_SECRET_KEY" | ||
| MAX_CONCURRENT_DOWNLOADS = "REPLACE_WITH_MAX_CONCURRENT_DOWNLOADS" | ||
| APP_NAME=REPLACE_WITH_APP_NAME | ||
| APP_HOST=REPLACE_WITH_APP_HOST | ||
| APP_PORT=REPLACE_WITH_APP_PORT | ||
| APP_ENV=REPLACE_WITH_APP_ENV | ||
| ALLOWED_ORIGINS=REPLACE_WITH_ALLOWED_ORIGINS | ||
| RUST_LOG=REPLACE_WITH_RUST_LOG | ||
| DOWNLOAD_DIR=REPLACE_WITH_DOWNLOAD_DIR | ||
| YTDLP_PATH=REPLACE_WITH_YTDLP_PATH | ||
| YTDLP_EXTERNAL_DOWNLOADER=REPLACE_WITH_YTDLP_EXTERNAL_DOWNLOADER | ||
| YTDLP_EXTERNAL_DOWNLOADER_ARGS=REPLACE_WITH_YTDLP_EXTERNAL_DOWNLOADER_ARGS | ||
| CAPTCHA_SECRET_KEY=REPLACE_WITH_CAPTCHA_SECRET_KEY | ||
| MAX_CONCURRENT_DOWNLOADS=REPLACE_WITH_MAX_CONCURRENT_DOWNLOADS | ||
| MASTER_API_KEY=REPLACE_WITH_MASTER_API_KEY | ||
| GITHUB_PAT=REPLACE_WITH_GITHUB_PAT | ||
| GITHUB_USERNAME=REPLACE_WITH_GITHUB_USERNAME | ||
| GITHUB_GRAPHQL_URL=https://api.github.com/graphql | ||
| WARP_LICENSE_KEY=REPLACE_WITH_WARP_LICENSE_KEY | ||
|
|
||
| ### ------------------------- | ||
| ### Terraform / Infra - If you need to deploy | ||
| ### ------------------------- | ||
| AWS_ACCESS_KEY_ID = "REPLACE_WITH_AWS_ACCESS_KEY_ID" | ||
| AWS_SECRET_ACCESS_KEY = "REPLACE_WITH_AWS_SECRET_ACCESS_KEY" | ||
| AWS_ENDPOINT_URL_S3 = "https://REPLACE_WITH_R2_ACCOUNT_ID.r2.cloudflarestorage.com" | ||
| AWS_ACCESS_KEY_ID=REPLACE_WITH_AWS_ACCESS_KEY_ID | ||
| AWS_SECRET_ACCESS_KEY=REPLACE_WITH_AWS_SECRET_ACCESS_KEY | ||
| AWS_ENDPOINT_URL_S3=https://REPLACE_WITH_R2_ACCOUNT_ID.r2.cloudflarestorage.com | ||
| AWS_S3_BUCKET_NAME=REPLACE_WITH_AWS_S3_BUCKET_NAME | ||
| # Terraform provider secrets | ||
| TF_VAR_DO_TOKEN = "REPLACE_WITH_DIGITALOCEAN_TOKEN" | ||
| TF_VAR_CLOUDFLARE_API_TOKEN = "REPLACE_WITH_CLOUDFLARE_API_TOKEN" | ||
| TF_VAR_DO_TOKEN=REPLACE_WITH_DIGITALOCEAN_TOKEN | ||
| TF_VAR_CLOUDFLARE_API_TOKEN=REPLACE_WITH_CLOUDFLARE_API_TOKEN | ||
| # Production DOCKER registry credentials (GHCR) | ||
| TF_VAR_GHCR_PAT = "REPLACE_WITH_GHCR_PAT" | ||
| TF_VAR_GITHUB_USERNAME = "REPLACE_WITH_GITHUB_USERNAME" | ||
| TF_VAR_GHCR_PAT=REPLACE_WITH_GHCR_PAT | ||
| TF_VAR_GITHUB_USERNAME=REPLACE_WITH_GITHUB_USERNAME | ||
| # ENV values to pass to Production | ||
| TF_VAR_APP_ENV = "REPLACE_WITH_TF_VAR_APP_ENV" | ||
| TF_VAR_CAPTCHA_SECRET_KEY = "REPLACE_WITH_TF_VAR_CAPTCHA_SECRET_KEY" | ||
| TF_VAR_APP_PORT = "REPLACE_WITH_TF_VAR_APP_PORT" | ||
| TF_VAR_HOST_PORT = "REPLACE_WITH_TF_VAR_HOST_PORT" | ||
| TF_VAR_APP_NAME = "REPLACE_WITH_TF_VAR_APP_NAME" | ||
| TF_VAR_ALLOWED_ORIGINS = "REPLACE_WITH_TF_VAR_ALLOWED_ORIGINS" | ||
| TF_VAR_RUST_LOG = "REPLACE_WITH_TF_VAR_RUST_LOG" | ||
| TF_VAR_MAX_CONCURRENT_DOWNLOADS = "REPLACE_WITH_TF_VAR_MAX_CONCURRENT_DOWNLOADS" | ||
| TF_VAR_APP_HOST = "REPLACE_WITH_TF_VAR_APP_HOST" | ||
| TF_VAR_YTDLP_PATH = "REPLACE_WITH_TF_VAR_YTDLP_PATH" | ||
| TF_VAR_YTDLP_OUTPUT_DIR = "REPLACE_WITH_TF_VAR_YTDLP_OUTPUT_DIR" | ||
| TF_VAR_APP_ENV=REPLACE_WITH_TF_VAR_APP_ENV | ||
| TF_VAR_CAPTCHA_SECRET_KEY=REPLACE_WITH_TF_VAR_CAPTCHA_SECRET_KEY | ||
| TF_VAR_APP_PORT=REPLACE_WITH_TF_VAR_APP_PORT | ||
| TF_VAR_HOST_PORT=REPLACE_WITH_TF_VAR_HOST_PORT | ||
| TF_VAR_APP_NAME=REPLACE_WITH_TF_VAR_APP_NAME | ||
| TF_VAR_ALLOWED_ORIGINS=REPLACE_WITH_TF_VAR_ALLOWED_ORIGINS | ||
| TF_VAR_RUST_LOG=REPLACE_WITH_TF_VAR_RUST_LOG | ||
| TF_VAR_MAX_CONCURRENT_DOWNLOADS=REPLACE_WITH_TF_VAR_MAX_CONCURRENT_DOWNLOADS | ||
| TF_VAR_APP_HOST=REPLACE_WITH_TF_VAR_APP_HOST | ||
| TF_VAR_YTDLP_PATH=REPLACE_WITH_TF_VAR_YTDLP_PATH | ||
| TF_VAR_DOWNLOAD_DIR=REPLACE_WITH_TF_VAR_DOWNLOAD_DIR | ||
| TF_VAR_MASTER_API_KEY=REPLACE_WITH_TF_VAR_MASTER_API_KEY | ||
| TF_VAR_WARP_LICENSE_KEY=REPLACE_WITH_TF_VAR_WARP_LICENSE_KEY | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| # Nadzu Backend - Engineering Standards & Policies | ||
|
|
||
| This document serves as the foundational mandate for all engineering work on this codebase. It applies to both human developers and AI agents. Strict adherence is required to maintain the "Anti-Corruption Layer" and high-performance nature of the system. | ||
|
|
||
| ## 1. Architectural Integrity | ||
|
|
||
| ### DTO vs. Domain Model Separation | ||
| * **External DTOs (`*_dto.rs`)**: Strictly for mapping external API responses (e.g., GitHub, YouTube). They must mirror the external schema (e.g., `camelCase`). | ||
| * **Domain Models (`src/models/`)**: Clean, optimized structures used by our business logic and returned to our frontend. | ||
| * **Anti-Corruption Layer**: Every service must implement a transformation pass (e.g., `transform_calendar`) to convert "dirty" DTOs into "pure" Domain Models. **Never leak external API structures into the rest of the application.** | ||
|
|
||
| ### Service Layer Responsibility | ||
| * Services must handle business logic, caching, and external communication. | ||
| * Controllers must only handle request extraction, calling services, and mapping results to HTTP responses. | ||
|
|
||
| ## 2. Performance & Memory Safety | ||
|
|
||
| ### Zero-Allocation Strategy | ||
| * Use `std::borrow::Cow<'static, str>` for static metadata (colors, labels, constant status messages). | ||
| * Avoid `String::clone()` or `.to_string()` inside loops. | ||
| * Pre-allocate vector capacities when the size is known or estimable (e.g., `Vec::with_capacity(365)`). | ||
|
|
||
| ### Iteration Optimization | ||
| * Perform data transformations in a **single pass**. | ||
| * Calculate metadata (min/max values, counts) during the primary loop to leverage CPU cache and minimize cycles. | ||
|
|
||
| ## 3. Security Standards | ||
|
|
||
| ### Constant-Time Validation | ||
| * Sensitive comparisons (API keys, tokens) must use `constant_time_eq` to prevent timing attacks. | ||
| * Validation logic should be centralized in `AppConfig`. | ||
|
|
||
| ### Information Hiding | ||
| * Internal state (file paths, format flags, system IDs) must **never** be exposed in API responses. | ||
| * Use specific "Response" versions of models (e.g., `YtdlpJobResponse`) to filter sensitive fields. | ||
|
|
||
| ## 4. Configuration Management | ||
|
|
||
| ### Result-Based Loading | ||
| * `AppConfig::from_env()` must return a `Result<Self, ConfigError>`. | ||
| * Avoid `std::process::exit` or `panic!` deep in the logic; handle startup failures gracefully in `app.rs`. | ||
|
|
||
| ### Immutable State | ||
| * Config fields should be private where appropriate, using constructors and public methods (`check_api_key`) to enforce security policies. | ||
|
|
||
| ## 5. Error Handling & API Contract | ||
|
|
||
| ### Typed Errors | ||
| * Use the `AppError` enum for all internal failures. | ||
| * Map domain errors to correct HTTP status codes: | ||
| * Validation Error $\rightarrow$ `422 Unprocessable Entity` | ||
| * Upstream/External Failure $\rightarrow$ `502 Bad Gateway` | ||
| * Auth Failure $\rightarrow$ `401 Unauthorized` | ||
|
|
||
| ### Consistency | ||
| * Responses should be flat and idiomatic where possible (avoiding unnecessary "job" or "data" wrappers unless required by the specific API design). | ||
|
|
||
| ## 6. Development Workflow | ||
|
|
||
| ### Tooling | ||
| * **Clippy**: Must be zero-warning. | ||
| * **Rustfmt**: Must be applied to every file. | ||
| * **Makefile**: Use `make c` for a full validation suite before concluding any task. | ||
|
|
||
| ### Documentation | ||
| * All public-facing methods and services must have `///` (Rustdoc) comments explaining intent and behavior. | ||
| * Complex logic (like the Midnight Snap caching strategy) must be documented inline. | ||
|
|
||
| --- | ||
| *Follow these rules to ensure the codebase remains scalable, secure, and blazingly fast.* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.