# jdevries3133/calcount architecture

> Architecture diagram of the GitHub repository jdevries3133/calcount, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-10). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jdevries3133/calcount
- Repository: https://github.com/jdevries3133/calcount

## Overview

##### Project Type and Purpose
- The README makes clear that Calcount is “A GPT Calorie counter app.” This is a full-stack web application built using Rust.
- It integrates user management (auth), calorie tracking (balancing), content (blog) and interactive elements (count chat) for a personalized experience. External services such as Stripe for subscription management and OpenAI for language/model responses are also key.

##### File Structure for Architectural Components
- The Rust source files in the src/ directory are organized into modules such as auth, balancing, blog, count_chat, legal, metrics, preferences, and stripe. This modularizes the backend application’s functionality.
- The presence of Dockerfile, Makefile, and Terraform files (infra.tf) suggests containerized deployments and infrastructure-as-code practices.
- SQL migration files under the migrations/ directory and numerous sqlx query files indicate a strong database component with compiled queries.
- The src/static directory, Tailwind configuration (tailwind.config.js, tailwind.css) and HTMX files hint at a frontend layer delivered as static assets and enhanced via JavaScript.
- Several scripts and githooks provide details about deployment and development processes.

##### Main Components
a. Client/Frontend:
– Represent the browser/user interface, which loads static assets (HTML, CSS from Tailwind, JS, HTMX-based pages).
– Label how HTMX works with the backend for dynamic updates.
b. Web Server / Application Backend (Rust):
– Showcase the central Rust application which is composed of multiple modules:
- Authentication Module: (src/auth) for login, registration, session handling, and password reset.
- Calorie Balancing Module: (src/balancing) for tracking/checkpoint functions.
- Blog Module: (src/blog) for managing posts.
- Count Chat Module: (src/count_chat) that interacts with OpenAI and includes helper JS files.
- Preferences Module: (src/preferences) for handling user settings.
- Stripe Module: (src/stripe) for subscription creation, customer portal operations, webhooks, etc.
- Other modules such as legal, metrics, and shared middleware.
c. Database:
– A central SQL database that is managed by the migrations and sqlx query integrations.
– Show data flow between application modules and the database.
d. External Services:
– OpenAI API: Integrated in the count_chat module for LLM-powered functionality.
– Stripe API: Integrated in the stripe module for handling subscriptions, billing, and customer portal link creation.
e. Infrastructure / Deployment:
– Include containerization (Docker) and CI/CD pipelines (via GitHub workflows in .github/workflows/ci_cd.yml).
– Highlight infrastructure-as-code elements with Terraform (infra.tf).

##### Relationships and Interactions
- Backend modules communicating with the database using query executions.

##### Architectural Patterns and Technologies
- The project uses a modular monolithic architecture, where all business logic is split into clearly defined modules.
- MVC (or similar) aspects: Controllers (src/controllers.rs, src/routes.rs) manage requests, and models (src/models.rs, module-specific models in stripe, preferences, etc.) represent data.

## Diagram (Mermaid)

```mermaid
flowchart TD 
    %% Presentation Layer
    subgraph "Presentation Layer"
        A("Browser/Client"):::frontend
        B("Static Assets (HTMX,Tailwind)"):::frontend
    end

    %% Application Layer
    subgraph "Application Layer"
        C("Rust Web Server"):::backend
        D("Authentication Module"):::backend
        E("Calorie Balancing Module"):::backend
        F("Blog Module"):::backend
        G("Count Chat Module"):::backend
        H("Preferences Module"):::backend
        I("Stripe Module"):::backend
        J("Legal Module"):::backend
        K("Metrics Module"):::backend
    end

    %% Data Layer
    subgraph "Data Layer"
        L(("SQL Database")):::database
    end

    %% External Services
    subgraph "External Services"
        M(("OpenAI API")):::external
        N(("Stripe API")):::external
    end

    %% Infrastructure / Deployment
    subgraph "Infrastructure/Deployment"
        O("Docker"):::infra
        P("CI/CD Pipeline"):::infra
        Q("Terraform"):::infra
    end

    %% Connections: Presentation to Application
    A -->|"HTTP"| B
    B -->|"HTMX"| C

    %% Connections: Application internal routing to modules
    C -->|"auth"| D
    C -->|"balance"| E
    C -->|"blog"| F
    C -->|"chat"| G
    C -->|"preferences"| H
    C -->|"billing"| I
    C -->|"legal"| J
    C -->|"metrics"| K

    %% Database interactions
    D -->|"query"| L
    E -->|"query"| L
    F -->|"query"| L
    G -->|"query"| L
    H -->|"query"| L
    I -->|"query"| L
    J -->|"query"| L
    K -->|"query"| L

    %% External API calls
    G -->|"OpenAI"| M
    I -->|"Stripe"| N

    %% Deployment interactions
    C --> O
    C --> P
    C --> Q

    %% Additional module interactions (authentication gating)
    D -->|"gateway"| E
    D -->|"gateway"| F

    %% Styles
    classDef frontend fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef backend fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef database fill:#FFA500,stroke:#000,stroke-width:2px;
    classDef external fill:#DDA0DD,stroke:#000,stroke-width:2px;
    classDef infra fill:#F4A460,stroke:#000,stroke-width:2px;

    %% Click Events
    click D "https://github.com/jdevries3133/calcount/tree/main/src/auth"
    click E "https://github.com/jdevries3133/calcount/tree/main/src/balancing"
    click F "https://github.com/jdevries3133/calcount/tree/main/src/blog"
    click G "https://github.com/jdevries3133/calcount/tree/main/src/count_chat"
    click H "https://github.com/jdevries3133/calcount/tree/main/src/preferences"
    click I "https://github.com/jdevries3133/calcount/tree/main/src/stripe"
    click J "https://github.com/jdevries3133/calcount/tree/main/src/legal"
    click K "https://github.com/jdevries3133/calcount/tree/main/src/metrics"
    click L "https://github.com/jdevries3133/calcount/tree/main/migrations"
    click B "https://github.com/jdevries3133/calcount/tree/main/src/static"
    click O "https://github.com/jdevries3133/calcount/tree/main/Dockerfile"
    click P "https://github.com/jdevries3133/calcount/blob/main/.github/workflows/ci_cd.yml"
    click Q "https://github.com/jdevries3133/calcount/blob/main/infra.tf"
```
