# saulvvelazquez/metas_app architecture

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

- Interactive diagram: https://gitdiagram.com/saulvvelazquez/metas_app
- Repository: https://github.com/saulvvelazquez/metas_app

## Overview

1. **Project shape**
   - This repository is a small **API-only backend** for managing personal goals (“metas”).
   - The runtime is **Python + FastAPI**, with the service started via `uvicorn main:app --reload` according to the README, though the visible entrypoint file in the tree is `app.py`.

2. **Core application boundary**
   - The main boundary is the **HTTP API layer** in FastAPI.
   - All business interactions happen through endpoints; there is no frontend, worker, or async job subsystem in this repo.

3. **Request handling pipeline**
   - A **global middleware** intercepts requests.
   - For non-public routes it reads `x-user-id` and `x-rol` headers, converts them into request-scoped identity, and stores them on `request.state`.
   - That state is the main mechanism used downstream for authorization and ownership checks.

4. **Authentication and identity**
   - Authentication is **header-based**, not session-cookie or token-based.
   - `POST /register` creates accounts with **bcrypt-hashed passwords**.
   - `POST /login` validates credentials and returns the identity headers expected by the rest of the API.
   - `GET /me` exposes the authenticated user profile using the request identity injected by middleware.

5. **Authorization model**
   - Access control is **RBAC** with two roles: `admin` and `user`.
   - Role drives behavior across endpoints:
     - admins can view all goals and manage users/categories
     - regular users can only access their own goals
   - Ownership checks are enforced on goal update/delete operations using the authenticated user id.

6. **Primary business subsystem: goals management**
   - `/metas` is the core domain resource.
   - It supports full CRUD-style operations with business rules around:
     - linking a goal to a user
     - linking a goal to a category
     - validating progress between 0 and 100
     - updating goal state based on progress
   - The domain model centers on `titulo`, `descripcion`, `progreso`, `estado`, `fecha_inicio`, and `fecha_limite`.

7. **Administration subsystem**
   - Admin-only routes provide system-level control:
     - `/stats` for global metrics
     - `/usuarios` for listing users
     - `/usuarios/{id}` for account deletion
     - `/categorias` for creating categories
   - This is the clearest separation between ordinary user flow and privileged management flow.

8. **Persistence layer**
   - The repository uses **MySQL** as the backing store, with **PyMySQL** as the connector.
   - `base.sql` defines the schema and seedable structure.
   - The schema is relational and normalized around three tables: `usuarios`, `categorias`, and `metas`.

9. **Data model and relationships**
   - `usuarios` is the identity/role table.
   - `categorias` is a lookup/dictionary table for organizing goals.
   - `metas` is the main transactional table, with foreign keys to both `usuarios` and `categorias`.
   - The relationships are:
     - one user to many goals
     - one category to many goals

10. **Validation and domain integrity**
   - **Pydantic** enforces input shape and business constraints.
   - Key validation rules include:
     - valid email format
     - minimum password length
     - progress constrained to 0–100
     - start date not after deadline
   - This keeps bad data out before it reaches the database layer.

11. **Security implementation**
   - Passwords are never stored in plaintext; hashing is done with **bcrypt** and salt is automatic.
   - The system relies on request headers for identity, so the trust boundary is the API middleware and route guards, not a separate auth server.

12. **Operational and diagnostic pieces**
   - `test.py` is used to validate the **MySQL connection**, so it functions as a lightweight environment check rather than part of the runtime path.
   - The API also exposes FastAPI’s built-in documentation at `/docs` and `/openapi.json`, which serves as the contract surface for clients.

Main entry points: [`app.py`](https://github.com/saulvvelazquez/metas_app/blob/main/app.py) (Goal routes), [`base.sql`](https://github.com/saulvvelazquez/metas_app/blob/main/base.sql) (Usuarios), [`requirements.txt`](https://github.com/saulvvelazquez/metas_app/blob/main/requirements.txt) (Dependencies), [`encriptar.py`](https://github.com/saulvvelazquez/metas_app/blob/main/encriptar.py) (Password hashing)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["HTTP API"]
  node_app["FastAPI app<br/>entrypoint<br/>[app.py]"]
  node_middleware["Identity middleware<br/>request middleware<br/>[app.py]"]
  node_auth_routes["Auth routes<br/>endpoint group<br/>[app.py]"]
  node_meta_routes["Goal routes<br/>endpoint group<br/>[app.py]"]
  node_admin_routes["Admin routes<br/>endpoint group<br/>[app.py]"]
  node_docs["API docs<br/>openapi surface"]
end

subgraph group_domain["Domain & Data"]
  node_validators{{"Pydantic rules<br/>validation"}}
  node_hashing["Password hashing<br/>security util<br/>[encriptar.py]"]
  node_rbac{{"RBAC policy<br/>authorization<br/>[app.py]"}}
  node_identity(("User identity<br/>request state"))
  node_users_table[("Usuarios<br/>table<br/>[base.sql]")]
  node_categories_table[("Categorias<br/>table<br/>[base.sql]")]
  node_metas_table[("Metas<br/>table<br/>[base.sql]")]
  node_mysql[("MySQL<br/>database")]
  node_schema["Schema SQL<br/>ddl<br/>[base.sql]"]
end

subgraph group_ops["Support"]
  node_conn_test["DB test<br/>environment check<br/>[test.py]"]
  node_deps["Dependencies<br/>package list<br/>[requirements.txt]"]
end

node_app -->|"installs"| node_middleware
node_app -->|"exposes"| node_auth_routes
node_app -->|"exposes"| node_meta_routes
node_app -->|"exposes"| node_admin_routes
node_app -->|"serves"| node_docs
node_middleware -->|"injects"| node_identity
node_auth_routes -->|"uses"| node_hashing
node_auth_routes -->|"validates"| node_validators
node_auth_routes -->|"reads/writes"| node_users_table
node_auth_routes -->|"returns"| node_identity
node_meta_routes -->|"validates"| node_validators
node_meta_routes -->|"enforces"| node_rbac
node_meta_routes -->|"reads/writes"| node_metas_table
node_meta_routes -->|"joins"| node_categories_table
node_meta_routes -->|"joins"| node_users_table
node_admin_routes -->|"gates"| node_rbac
node_admin_routes -->|"reads/writes"| node_users_table
node_admin_routes -->|"writes"| node_categories_table
node_admin_routes -->|"reads"| node_metas_table
node_schema -->|"defines"| node_users_table
node_schema -->|"defines"| node_categories_table
node_schema -->|"defines"| node_metas_table
node_metas_table -->|"stored in"| node_mysql
node_users_table -->|"stored in"| node_mysql
node_categories_table -->|"stored in"| node_mysql
node_conn_test -->|"checks"| node_mysql
node_deps -.->|"supports"| node_app
node_deps -.->|"supports"| node_hashing

click node_app "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_middleware "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_auth_routes "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_meta_routes "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_admin_routes "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_hashing "https://github.com/saulvvelazquez/metas_app/blob/main/encriptar.py"
click node_rbac "https://github.com/saulvvelazquez/metas_app/blob/main/app.py"
click node_users_table "https://github.com/saulvvelazquez/metas_app/blob/main/base.sql"
click node_categories_table "https://github.com/saulvvelazquez/metas_app/blob/main/base.sql"
click node_metas_table "https://github.com/saulvvelazquez/metas_app/blob/main/base.sql"
click node_schema "https://github.com/saulvvelazquez/metas_app/blob/main/base.sql"
click node_conn_test "https://github.com/saulvvelazquez/metas_app/blob/main/test.py"
click node_deps "https://github.com/saulvvelazquez/metas_app/blob/main/requirements.txt"

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_app,node_middleware,node_auth_routes,node_meta_routes,node_admin_routes,node_docs toneBlue
class node_validators,node_hashing,node_rbac,node_identity,node_users_table,node_categories_table,node_metas_table,node_mysql,node_schema toneAmber
class node_conn_test,node_deps toneMint
```

## Components

### HTTP API

FastAPI request handling and route surface

- [FastAPI app](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): entrypoint · Application bootstrap and route assembly · `app.py`
- [Identity middleware](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): request middleware · Reads user headers and stores request-scoped identity · `app.py`
- [Auth routes](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): endpoint group · Register, login, and profile access · `app.py`
- [Goal routes](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): endpoint group · CRUD for metas with ownership and progress rules · `app.py`
- [Admin routes](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): endpoint group · Privileged user, category, and stats management · `app.py`
- **API docs**: openapi surface · Interactive docs and schema contract

### Domain & Data

Core business rules, identity, and relational persistence

- **Pydantic rules**: validation · Email, password, progress, and date constraints
- [Password hashing](https://github.com/saulvvelazquez/metas_app/blob/main/encriptar.py): security util · bcrypt-based password hashing and verification · `encriptar.py`
- [RBAC policy](https://github.com/saulvvelazquez/metas_app/blob/main/app.py): authorization · Admin and user role checks plus ownership enforcement · `app.py`
- **User identity**: request state · Authenticated user and role carried on request state
- [Usuarios](https://github.com/saulvvelazquez/metas_app/blob/main/base.sql): table · Accounts, roles, and credentials · `base.sql`
- [Categorias](https://github.com/saulvvelazquez/metas_app/blob/main/base.sql): table · Category lookup data · `base.sql`
- [Metas](https://github.com/saulvvelazquez/metas_app/blob/main/base.sql): table · Goals with user and category links · `base.sql`
- **MySQL**: database · Relational backing store accessed through PyMySQL
- [Schema SQL](https://github.com/saulvvelazquez/metas_app/blob/main/base.sql): ddl · Database schema and seedable structure · `base.sql`

### Support

Documentation and environment checks

- [DB test](https://github.com/saulvvelazquez/metas_app/blob/main/test.py): environment check · Lightweight MySQL connectivity check · `test.py`
- [Dependencies](https://github.com/saulvvelazquez/metas_app/blob/main/requirements.txt): package list · Python runtime and library requirements · `requirements.txt`

## Connections

- FastAPI app → Identity middleware: installs · Middleware runs before protected requests
- FastAPI app → Auth routes: exposes · Publishes register, login, and me endpoints
- FastAPI app → Goal routes: exposes · Publishes goal management endpoints
- FastAPI app → Admin routes: exposes · Publishes privileged management endpoints
- FastAPI app → API docs: serves · FastAPI documentation and schema output
- Identity middleware → User identity: injects · Maps x-user-id and x-rol into request state
- Auth routes → Password hashing: uses · Hashes passwords at registration and verifies at login
- Auth routes → Pydantic rules: validates · Checks email and password shape before persistence
- Auth routes → Usuarios: reads/writes · Creates and authenticates accounts
- Auth routes → User identity: returns · Login issues the identity headers used by the API
- Goal routes → Pydantic rules: validates · Enforces progress and date constraints
- Goal routes → RBAC policy: enforces · Applies ownership and role checks on goal actions
- Goal routes → Metas: reads/writes · Stores and updates goal records
- Goal routes → Categorias: joins · Associates goals with categories
- Goal routes → Usuarios: joins · Associates goals with owners
- Admin routes → RBAC policy: gates · Admin-only access for stats, users, and categories
- Admin routes → Usuarios: reads/writes · Lists and deletes user accounts
- Admin routes → Categorias: writes · Creates categories
- Admin routes → Metas: reads · Computes global metrics
- Schema SQL → Usuarios: defines · Relational schema for accounts
- Schema SQL → Categorias: defines · Relational schema for categories
- Schema SQL → Metas: defines · Relational schema for goals
- Metas → MySQL: stored in · Primary transactional data lives in MySQL
- Usuarios → MySQL: stored in · Identity data lives in MySQL
- Categorias → MySQL: stored in · Lookup data lives in MySQL
- DB test → MySQL: checks · Verifies connectivity to the database
- Dependencies → FastAPI app: supports · Declares runtime packages for the API
- Dependencies → Password hashing: supports · Provides bcrypt and related libraries
