How saulvvelazquez/metas_app works
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 Goal routes · base.sql Usuarios · requirements.txt Dependencies · encriptar.py Password hashing
Components 17
HTTP API
FastAPI request handling and route surface
- FastAPI app · Application bootstrap and route assembly
app.py - Identity middleware · Reads user headers and stores request-scoped identity
app.py - Auth routes · Register, login, and profile access
app.py - Goal routes · CRUD for metas with ownership and progress rules
app.py - Admin routes · Privileged user, category, and stats management
app.py - API docs · Interactive docs and schema contract
Domain & Data
Core business rules, identity, and relational persistence
- Pydantic rules · Email, password, progress, and date constraints
- Password hashing · bcrypt-based password hashing and verification
encriptar.py - RBAC policy · Admin and user role checks plus ownership enforcement
app.py - User identity · Authenticated user and role carried on request state
- Usuarios · Accounts, roles, and credentials
base.sql - Categorias · Category lookup data
base.sql - Metas · Goals with user and category links
base.sql - MySQL · Relational backing store accessed through PyMySQL
- Schema SQL · Database schema and seedable structure
base.sql
Support
Documentation and environment checks
- DB test · Lightweight MySQL connectivity check
test.py - Dependencies · Python runtime and library requirements
requirements.txt
Connections 28
- 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