# jackmisner/brewtracker architecture

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

- Interactive diagram: https://gitdiagram.com/jackmisner/brewtracker
- Repository: https://github.com/jackmisner/brewtracker

## Overview

1. **Top-level shape**
   BrewTracker is a two-tier monorepo: a **React/TypeScript frontend** in `frontend/` and a **Flask/Python API** in `backend/`. The frontend owns user interaction and local UI state; the backend owns authentication, calculations, persistence, AI analysis, and domain rules.

2. **Frontend boundary**
   The frontend is organized as a classic SPA with `pages/` for routes, `components/` for reusable UI, `services/` for API access, `reducers/` and `hooks/` for state orchestration, and `contexts/` for cross-cutting preferences like units. This creates a clear separation between presentation, client state, and server communication.

3. **Frontend data flow**
   User actions in pages such as recipe builder, brew sessions, beer XML, and settings call TypeScript service modules, which go through a shared HTTP client in `src/services/api.ts`. Those services map backend API responses into typed models under `src/types/`, while reducers store workflow state for multi-step features like recipe editing, BeerXML import, and session tracking.

4. **Backend entry and API boundary**
   The backend starts from `backend/app.py`, which wires Flask configuration, CORS, auto-seeding, error handling, security headers, and blueprint registration. Each domain area is exposed as a Flask blueprint in `backend/routes/`, keeping the API surface split by feature rather than by transport mechanics.

5. **Backend domain layers**
   The backend uses a strong “routes → services → utils/models” structure. Routes stay thin and delegate to services; services implement business rules and orchestration; utils provide reusable pure logic such as brewing math, unit conversion, sanitization, validation, crypto, and security monitoring. MongoEngine models in `backend/models/` define persisted domain objects and validation.

6. **Persistence and seed data**
   MongoDB is the main datastore, accessed through `mongodb_service.py` and MongoEngine models. Initial domain data comes from JSON files in `backend/data/` and is loaded by seed scripts for ingredients, beer styles, and system users. That means the app bootstraps itself with reference data and special accounts before normal use.

7. **Core brewing workflow**
   Recipe creation and editing is centered on brewing calculations: OG, FG, ABV, IBU, SRM, unit conversion, and recipe normalization. These calculations live primarily in `backend/utils/brewing_calculation_core.py`, `recipe_api_calculator.py`, and `recipe_orm_calculator.py`, then surface through recipe- and analytics-related routes and frontend recipe builder components.

8. **AI analysis subsystem**
   The AI feature is not a generic LLM integration; it is a **flowchart-based rules engine** under `backend/services/ai/`. `flowchart_engine.py`, `flowchart_nodes.py`, `optimization_strategies.py`, and `recipe_context.py` execute YAML-defined workflows such as recipe optimization and unit conversion. The frontend’s AI suggestions UI consumes this output through a dedicated AI service and reducer.

9. **BeerXML and ingredient matching**
   BeerXML import/export is a separate pipeline spanning frontend and backend. The frontend provides import/export and review screens; the backend exposes BeerXML routes and ingredient lookup/matching logic. This subsystem bridges external recipe files into BrewTracker’s internal recipe model and handles unit/ingredient normalization during import.

10. **Brew session and attenuation analytics**
   Brew sessions are tracked as a lifecycle tied to recipes: creation, editing, fermentation notes, dry hop tracking, and gravity stabilization analysis. A separate attenuation analytics path aggregates real-world yeast performance data and feeds both the analytics page and backend analytics/attenuation services.

11. **Authentication, user settings, and system users**
   Authentication is JWT-based, with Google OAuth support handled by backend verification services and a frontend sign-in component. User settings, account deletion, and username validation are separate flows. The “system users” concept is important architecturally because deleted-user public content can be re-attributed instead of removed, preserving community recipes while protecting privacy.

12. **Security and operational boundaries**
   Security is a first-class backend layer: rate limiting, request validation, input sanitization, headers, monitoring, crypto utilities, and geolocation support are all in dedicated modules rather than embedded in routes. This makes the backend’s trust boundary explicit: untrusted client input enters through routes, is validated/sanitized, then reaches business logic and MongoDB.

13. **Deployment and delivery**
   The repo includes CI for both apps via GitHub Actions, plus separate deployment workflows for backend and frontend. The backend includes `Dockerfile` and `fly.toml`, suggesting containerized deployment on Fly.io, while the frontend includes `vercel.json`, suggesting Vercel hosting. This reinforces that the two halves are independently built and deployed but share the same product surface.

14. **Tests as architectural documentation**
   The test suites closely mirror the system structure: backend tests cover routes, services, utils, models, and security behavior; frontend tests cover components, reducers, services, hooks, and pages. That symmetry confirms the main boundaries are feature-oriented modules with strong separation between UI, API, domain logic, and infrastructure.

Main entry points: [`frontend/src/pages`](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/pages) (Pages), [`backend/routes`](https://github.com/jackmisner/brewtracker/tree/main/backend/routes) (API routes), [`backend/services`](https://github.com/jackmisner/brewtracker/tree/main/backend/services) (Domain services), [`frontend/src/services`](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/services) (Feature services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["App shell<br/>react entry<br/>[App.tsx]"]
  node_frontend_pages["Pages<br/>route views"]
  node_frontend_components["UI components<br/>presentational"]
  node_frontend_services_api["API client<br/>http client<br/>[api.ts]"]
  node_frontend_services["Feature services<br/>service layer"]
  node_frontend_state["State logic<br/>reducers/hooks"]
  node_frontend_contexts(("Unit context<br/>cross-cutting state"))
  node_frontend_types["Typed models<br/>api models"]
  node_frontend_tests["Frontend tests<br/>test suite"]
end

subgraph group_backend["Backend"]
  node_backend_app["Flask app<br/>api bootstrap<br/>[app.py]"]
  node_backend_routes["API routes<br/>blueprints"]
  node_backend_services_domain["Domain services<br/>business logic"]
  node_backend_ai{{"AI engine<br/>workflow rules"}}
  node_backend_utils["Core utils<br/>calculation/security"]
  node_backend_models[("Mongo models<br/>persistence models<br/>[mongo_models.py]")]
  node_backend_mongo[("MongoDB<br/>datastore<br/>[mongodb_service.py]")]
  node_backend_seed_data["Seed data<br/>reference data"]
  node_backend_seeds["Seed scripts<br/>bootstrap scripts"]
  node_backend_tests["Backend tests<br/>test suite"]
end

subgraph group_infra["Delivery"]
  node_ci_cd["CI/CD<br/>workflows<br/>[workflows]"]
end

node_frontend_app -->|"renders"| node_frontend_pages
node_frontend_app -->|"uses"| node_frontend_components
node_frontend_pages -->|"calls"| node_frontend_services
node_frontend_services -->|"through"| node_frontend_services_api
node_frontend_services -->|"maps"| node_frontend_types
node_frontend_state -->|"orchestrates"| node_frontend_services
node_frontend_contexts -->|"provides"| node_frontend_components
node_frontend_pages -->|"uses"| node_frontend_state
node_frontend_pages -->|"reads"| node_frontend_contexts
node_frontend_tests -.->|"covers"| node_frontend_pages
node_frontend_tests -.->|"covers"| node_frontend_services
node_backend_app -->|"registers"| node_backend_routes
node_backend_routes -->|"delegates"| node_backend_services_domain
node_backend_services_domain -->|"uses"| node_backend_utils
node_backend_services_domain -->|"persists via"| node_backend_models
node_backend_models -->|"stored in"| node_backend_mongo
node_backend_seeds -->|"loads"| node_backend_seed_data
node_backend_services_domain -->|"includes"| node_backend_ai
node_backend_routes -->|"exposes"| node_backend_ai
node_backend_routes -->|"reads/writes"| node_backend_models
node_backend_tests -.->|"covers"| node_backend_routes
node_backend_tests -.->|"covers"| node_backend_services_domain
node_backend_tests -.->|"covers"| node_backend_utils
node_ci_cd -.->|"builds/deploys"| node_backend_app
node_ci_cd -.->|"builds/deploys"| node_frontend_app

click node_frontend_app "https://github.com/jackmisner/brewtracker/blob/main/frontend/src/App.tsx"
click node_frontend_pages "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/components"
click node_frontend_services_api "https://github.com/jackmisner/brewtracker/blob/main/frontend/src/services/api.ts"
click node_frontend_services "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/services"
click node_frontend_state "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/reducers"
click node_frontend_contexts "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/contexts"
click node_frontend_types "https://github.com/jackmisner/brewtracker/tree/main/frontend/src/types"
click node_backend_app "https://github.com/jackmisner/brewtracker/blob/main/backend/app.py"
click node_backend_routes "https://github.com/jackmisner/brewtracker/tree/main/backend/routes"
click node_backend_services_domain "https://github.com/jackmisner/brewtracker/tree/main/backend/services"
click node_backend_ai "https://github.com/jackmisner/brewtracker/tree/main/backend/services/ai"
click node_backend_utils "https://github.com/jackmisner/brewtracker/tree/main/backend/utils"
click node_backend_models "https://github.com/jackmisner/brewtracker/blob/main/backend/models/mongo_models.py"
click node_backend_mongo "https://github.com/jackmisner/brewtracker/blob/main/backend/services/mongodb_service.py"
click node_backend_seed_data "https://github.com/jackmisner/brewtracker/tree/main/backend/data"
click node_backend_seeds "https://github.com/jackmisner/brewtracker/tree/main/backend/seeds"
click node_frontend_tests "https://github.com/jackmisner/brewtracker/tree/main/frontend/tests"
click node_backend_tests "https://github.com/jackmisner/brewtracker/tree/main/backend/tests"
click node_ci_cd "https://github.com/jackmisner/brewtracker/blob/main/.github/workflows"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_pages,node_frontend_components,node_frontend_services_api,node_frontend_services,node_frontend_state,node_frontend_contexts,node_frontend_types,node_frontend_tests toneBlue
class node_backend_app,node_backend_routes,node_backend_services_domain,node_backend_ai,node_backend_utils,node_backend_models,node_backend_mongo,node_backend_seed_data,node_backend_seeds,node_backend_tests toneAmber
class node_ci_cd toneMint
```

## Components

### Frontend

React/TypeScript SPA for user workflows and client state.

- [App shell](https://github.com/jackmisner/brewtracker/blob/main/frontend/src/App.tsx): react entry · Bootstraps the SPA and shared layout/routing. · `frontend/src/App.tsx`
- [Pages](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/pages): route views · Top-level screens for recipes, sessions, auth, settings, and content. · `frontend/src/pages`
- [UI components](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/components): presentational · Reusable panels and workflow-specific controls for recipes, BeerXML, brew sessions, and analytics. · `frontend/src/components`
- [API client](https://github.com/jackmisner/brewtracker/blob/main/frontend/src/services/api.ts): http client · Shared request layer used by feature services. · `frontend/src/services/api.ts`
- [Feature services](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/services): service layer · Domain-specific client services for recipes, BeerXML, brewing, analytics, AI, and user settings. · `frontend/src/services`
- [State logic](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/reducers): reducers/hooks · Workflow state orchestration for builders, imports, sessions, analytics, and settings. · `frontend/src/reducers`
- [Unit context](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/contexts): cross-cutting state · Shared preferences and global UI context such as measurement units. · `frontend/src/contexts`
- [Typed models](https://github.com/jackmisner/brewtracker/tree/main/frontend/src/types): api models · Shared TypeScript types for API payloads, recipes, sessions, users, and units. · `frontend/src/types`
- [Frontend tests](https://github.com/jackmisner/brewtracker/tree/main/frontend/tests): test suite · Mirrors UI, hooks, reducers, services, contexts, and pages. · `frontend/tests`

### Backend

Flask/Python API for domain logic, persistence, and security.

- [Flask app](https://github.com/jackmisner/brewtracker/blob/main/backend/app.py): api bootstrap · Application entrypoint wiring config, CORS, headers, seeding, and blueprints. · `backend/app.py`
- [API routes](https://github.com/jackmisner/brewtracker/tree/main/backend/routes): blueprints · Feature-oriented HTTP endpoints for auth, recipes, BeerXML, sessions, analytics, and settings. · `backend/routes`
- [Domain services](https://github.com/jackmisner/brewtracker/tree/main/backend/services): business logic · Orchestration and rules for auth, AI, attenuation, lookup, user lifecycle, and persistence. · `backend/services`
- [AI engine](https://github.com/jackmisner/brewtracker/tree/main/backend/services/ai): workflow rules · Flowchart-based recipe optimization and unit-conversion workflows driven by YAML configs. · `backend/services/ai`
- [Core utils](https://github.com/jackmisner/brewtracker/tree/main/backend/utils): calculation/security · Brewing math, unit conversion, validation, sanitization, crypto, rate limiting, and security headers. · `backend/utils`
- [Mongo models](https://github.com/jackmisner/brewtracker/blob/main/backend/models/mongo_models.py): persistence models · MongoEngine domain objects and validation rules. · `backend/models/mongo_models.py`
- [MongoDB](https://github.com/jackmisner/brewtracker/blob/main/backend/services/mongodb_service.py): datastore · Primary persistent store accessed through the Mongo service and models. · `backend/services/mongodb_service.py`
- [Seed data](https://github.com/jackmisner/brewtracker/tree/main/backend/data): reference data · JSON reference sets for beer styles, ingredients, and system users. · `backend/data`
- [Seed scripts](https://github.com/jackmisner/brewtracker/tree/main/backend/seeds): bootstrap scripts · Initial loaders that populate reference data and system accounts. · `backend/seeds`
- [Backend tests](https://github.com/jackmisner/brewtracker/tree/main/backend/tests): test suite · Covers routes, services, utils, models, workflows, and security behavior. · `backend/tests`

### Delivery

CI, deployment, and runtime packaging.

- [CI/CD](https://github.com/jackmisner/brewtracker/blob/main/.github/workflows): workflows · GitHub Actions for backend/frontend CI and separate deployment pipelines. · `.github/workflows`

## Connections

- App shell → Pages: renders · Top-level app composes routed screens.
- App shell → UI components: uses · Pages and shared layout are built from reusable components.
- Pages → Feature services: calls · Screens invoke feature services for data and actions.
- Feature services → API client: through · All network access passes through the shared API client.
- Feature services → Typed models: maps · Service responses are typed using shared models.
- State logic → Feature services: orchestrates · Reducers/hooks coordinate multi-step workflows around service calls.
- Unit context → UI components: provides · Shared preferences flow into UI components.
- Pages → State logic: uses · Route screens rely on local workflow state.
- Pages → Unit context: reads · Pages consume global preference context.
- Frontend tests → Pages: covers · Tests mirror the page and component boundaries.
- Frontend tests → Feature services: covers · Tests validate client-side service behavior.
- Flask app → API routes: registers · Flask entrypoint wires feature blueprints.
- API routes → Domain services: delegates · Routes remain thin and hand off logic to services.
- Domain services → Core utils: uses · Business logic relies on shared math, security, and validation helpers.
- Domain services → Mongo models: persists via · Services read and write MongoEngine models.
- Mongo models → MongoDB: stored in · Domain models are backed by MongoDB.
- Seed scripts → Seed data: loads · Bootstrap scripts populate the reference datasets.
- Domain services → AI engine: includes · AI workflows are a specialized service subsystem.
- API routes → AI engine: exposes · AI endpoints publish workflow results to clients.
- API routes → Mongo models: reads/writes · Endpoints interact with persisted domain objects through services.
- Backend tests → API routes: covers · Tests validate API behavior and boundaries.
- Backend tests → Domain services: covers · Tests validate business rules and orchestration.
- Backend tests → Core utils: covers · Tests validate calculations and security helpers.
- CI/CD → Flask app: builds/deploys · Backend workflow handles packaging and deployment.
- CI/CD → App shell: builds/deploys · Frontend workflow handles build and hosting deployment.
