# adrirepa23/backend-rally architecture

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

- Interactive diagram: https://gitdiagram.com/adrirepa23/backend-rally
- Repository: https://github.com/adrirepa23/backend-rally

## Overview

##### Project type and purpose
- This is a RESTful backend service (“Backend Rally Fotográfico”) built with Node.js and Express. It exposes CRUD endpoints for users, rallies, publications, votes, comments and statistics.
- Although the README mentions MongoDB/Mongoose, your custom instruction specifies “Using MySQL,” so the system will use a MySQL database (initialized via basededatos.sql) accessed by an ORM.
- It also integrates with Cloudinary (image storage) and SendGrid (email notifications).

##### File structure
- Top-level files:
- basededatos.sql → MySQL schema & seed script.
- package.json, nodemon.json → build/run scripts & dependencies.
- src/config → configuration modules for DB, Cloudinary, SendGrid.
- src/routes → route definitions (one file per resource).
- src/controllers → handlers implementing business logic.
- src/models → data models (replace Mongoose models with ORM entities for MySQL).
- src/middlewares → shared middleware (auth, optional-auth, error handling).
- src/server.js → main entry point that wires everything together.

##### README for additional insights
- Security layers: Helmet, rate limiting, CORS, input validation.
- JWT-based authentication.
- Clear separation of config, models, controllers, routes and middleware, following an MVC-style layering.

a. Main components
- Client(s): Web or mobile frontend(s) consuming the API.
- API Gateway / Express Server:
– Routes layer (src/routes)
– Middleware layer (auth, CORS, rate limiter, error handler)
– Controller layer (src/controllers)
– ORM / Data access layer (src/models)
- MySQL Database cluster (based on schema in basededatos.sql)
- External Services: Cloudinary (image storage) and SendGrid (email service)

b. Relationships & data flow
1. Client → HTTP request → Express Server
2. Server → CORS → Rate Limiter → Auth Middleware → Route resolution
3. Route handler calls Controller → Controller uses ORM to query/update MySQL
4. Controller may call Cloudinary SDK to upload/fetch images
5. Controller may call SendGrid SDK to send emails
6. Controller returns data → Middleware error handling → Response → Client

c. Architectural patterns & design principles
- Layered (MVC) architecture with clear separation of concerns
- Stateless REST endpoints secured with JWT
- Centralized error handling and input validation
- Externalizing configuration via environment variables

d. Key technologies/frameworks
- Node.js, Express.js
- MySQL (with an ORM such as Sequelize or TypeORM)
- Cloudinary API
- SendGrid API
- Security: Helmet, express-rate-limit, CORS
- JWT for auth

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client
    Client["Client (Web/Mobile)"]:::external

    %% Express Server with layers
    subgraph "Express Server" 
        direction TB
        subgraph "Configuration" 
            direction TB
            DBConfig["MySQL Config"]:::internal
            CloudConfig["Cloudinary Config"]:::internal
            SendGridConfig["SendGrid Config"]:::internal
        end
        subgraph "Middleware"
            direction TB
            CORS["CORS"]:::internal
            RateLimiter["Rate Limiter"]:::internal
            AuthMiddleware["Auth Middleware"]:::internal
            OptionalAuthMiddleware["Optional Auth Middleware"]:::internal
            ErrorHandler["Error Handler"]:::internal
        end
        subgraph "Routes"
            direction TB
            AuthRoutes["Auth Routes"]:::internal
            UserRoutes["User Routes"]:::internal
            RallyRoutes["Rally Routes"]:::internal
            PublicationRoutes["Publication Routes"]:::internal
            CommentRoutes["Comment Routes"]:::internal
            VoteRoutes["Vote Routes"]:::internal
            StatsRoutes["Statistics Routes"]:::internal
        end
        subgraph "Controllers"
            direction TB
            AuthController["Auth Controller"]:::internal
            UserController["User Controller"]:::internal
            RallyController["Rally Controller"]:::internal
            PublicationController["Publication Controller"]:::internal
            CommentController["Comment Controller"]:::internal
            VoteController["Vote Controller"]:::internal
            StatsController["Statistics Controller"]:::internal
        end
        subgraph "Models (ORM)"
            direction TB
            UserModel["Usuario Entity"]:::internal
            RallyModel["Rally Entity"]:::internal
            PublicationModel["Publicacion Entity"]:::internal
            CommentModel["Comentario Entity"]:::internal
            VoteModel["Votacion Entity"]:::internal
        end
        Server["Server Entry (src/server.js)"]:::internal
    end

    %% Database and Initialization
    MySQL["MySQL Database"]:::db
    DBInit["DB Init Script"]:::db

    %% External Services
    Cloudinary["Cloudinary Service"]:::external
    SendGrid["SendGrid Service"]:::external

    %% Connections
    Client -->|"HTTP Request"| CORS
    CORS --> RateLimiter
    RateLimiter --> AuthMiddleware
    AuthMiddleware --> OptionalAuthMiddleware
    OptionalAuthMiddleware --> AuthRoutes
    OptionalAuthMiddleware --> UserRoutes
    OptionalAuthMiddleware --> RallyRoutes
    OptionalAuthMiddleware --> PublicationRoutes
    OptionalAuthMiddleware --> CommentRoutes
    OptionalAuthMiddleware --> VoteRoutes
    OptionalAuthMiddleware --> StatsRoutes

    AuthRoutes --> AuthController
    UserRoutes --> UserController
    RallyRoutes --> RallyController
    PublicationRoutes --> PublicationController
    CommentRoutes --> CommentController
    VoteRoutes --> VoteController
    StatsRoutes --> StatsController

    AuthController --> UserModel
    UserController --> UserModel
    RallyController --> RallyModel
    PublicationController --> PublicationModel
    CommentController --> CommentModel
    VoteController --> VoteModel
    StatsController --> UserModel

    UserModel -->|"SQL Queries"| MySQL
    RallyModel --> MySQL
    PublicationModel --> MySQL
    CommentModel --> MySQL
    VoteModel --> MySQL

    StatsController -->|"SQL Queries"| MySQL

    PublicationController -->|"Cloudinary SDK"| Cloudinary
    CommentController -->|"Cloudinary SDK"| Cloudinary

    AuthController -->|".send()"| SendGrid

    AuthMiddleware --->|uses| DBConfig
    AuthController --->|uses| DBConfig
    AllConfig["config modules"] -.-> Server

    Server --> CORS
    Server --> DBConfig
    Server --> CloudConfig
    Server --> SendGridConfig
    Server --> Server

    DBInit --> MySQL

    %% Error handling
    AuthController --> ErrorHandler
    UserController --> ErrorHandler
    RallyController --> ErrorHandler
    PublicationController --> ErrorHandler
    CommentController --> ErrorHandler
    VoteController --> ErrorHandler
    StatsController --> ErrorHandler
    ErrorHandler --> Client

    %% Click events
    click Server "https://github.com/adrirepa23/backend-rally/blob/main/src/server.js"
    click DBInit "https://github.com/adrirepa23/backend-rally/blob/main/basededatos.sql"
    click DBConfig "https://github.com/adrirepa23/backend-rally/blob/main/src/config/db.js"
    click CloudConfig "https://github.com/adrirepa23/backend-rally/blob/main/src/config/cloudinary.js"
    click SendGridConfig "https://github.com/adrirepa23/backend-rally/blob/main/src/config/sendgrid.js"
    click AuthMiddleware "https://github.com/adrirepa23/backend-rally/blob/main/src/middlewares/authMiddleware.js"
    click OptionalAuthMiddleware "https://github.com/adrirepa23/backend-rally/blob/main/src/middlewares/optionalAuthMiddleware.js"
    click ErrorHandler "https://github.com/adrirepa23/backend-rally/blob/main/src/middlewares/errorHandler.js"
    click AuthRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/authRoutes.js"
    click UserRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/userRoutes.js"
    click RallyRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/rallyRoutes.js"
    click PublicationRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/publicacionRoutes.js"
    click CommentRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/comentarioRoutes.js"
    click VoteRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/votacionRoutes.js"
    click StatsRoutes "https://github.com/adrirepa23/backend-rally/blob/main/src/routes/estadisticasRoutes.js"
    click AuthController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/authController.js"
    click UserController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/userController.js"
    click RallyController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/rallyController.js"
    click PublicationController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/publicacionController.js"
    click CommentController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/comentarioController.js"
    click VoteController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/votacionController.js"
    click StatsController "https://github.com/adrirepa23/backend-rally/blob/main/src/controllers/estadisticasController.js"
    click UserModel "https://github.com/adrirepa23/backend-rally/blob/main/src/models/Usuario.js"
    click RallyModel "https://github.com/adrirepa23/backend-rally/blob/main/src/models/Rally.js"
    click PublicationModel "https://github.com/adrirepa23/backend-rally/blob/main/src/models/Publicacion.js"
    click CommentModel "https://github.com/adrirepa23/backend-rally/blob/main/src/models/Comentario.js"
    click VoteModel "https://github.com/adrirepa23/backend-rally/blob/main/src/models/Votacion.js"

    %% Styles
    classDef internal fill:#bbdefb,stroke:#0d47a1,color:#0d47a1
    classDef db fill:#c8e6c9,stroke:#1b5e20,color:#1b5e20
    classDef external fill:#ffe0b2,stroke:#e65100,color:#e65100
    classDef async stroke-dasharray: 5 5
```
