# italo-gouveia/myanimeapi architecture

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

- Interactive diagram: https://gitdiagram.com/italo-gouveia/myanimeapi
- Repository: https://github.com/italo-gouveia/myanimeapi

## Overview

##### Project type and purpose
- MyAnimeAPI is a full-stack web application.
- The frontend is a Next.js/React app that handles user interactions (login, register, profile, browsing anime).
- The backend is a Go REST API (using Gorilla Mux) providing CRUD endpoints for anime, users, reviews, plus JWT auth, rate limiting, and validation.
- It persists data in PostgreSQL (via GORM), is containerized with Docker, and uses GitHub Actions for CI/CD.

##### File structure
- Top-level directories:
- cmd/ – application entrypoints, Swagger docs generator
- api/ – HTTP handlers, routes, middleware, mocks, auth, database wrappers
- internal/ – config loader, database migrations, DB interface, utilities, error definitions
- frontend/ – Next.js code (pages, components, API client, hooks)
- tests/ – unit, integration, E2E, smoke test code
- assets/, Dockerfile, docker-compose.yml, .github/workflows – deployment and CI/CD
- The Go code is organized around handlers → middleware → routers → models → internal services
- The frontend is a self-contained client that calls the Go API at /v1 endpoints.

##### README for additional insights
- Describes API endpoints, technologies, features (auth, pagination, rate limiting)

##### Diagramming the system architecture
a. Main components:
- Client (browser/mobile) running the Next.js frontend
- Next.js Server (SSR/static assets)
- Go REST API Server (Gorilla Mux)
- PostgreSQL database
- Middleware chain (auth, rate limit, logging, sanitization, error handling)
- CI/CD pipeline (GitHub Actions)
- Docker & Docker-Compose orchestration
- External: Swagger UI, JWT issuer, possibly third-party auth (if any)
b. Relationships & interactions:
- Client → (HTTP/HTTPS) → Next.js server → (HTTP/HTTPS) → Go API
- Go API → (SQL via GORM) → PostgreSQL
- Go API ←→ internal/config for environment variables
- GitHub pushes trigger CI workflow → build Docker image → run tests → push to registry → deploy
c. Architectural patterns & principles:
- Layered architecture (HTTP layer → middleware → handler → service → data layer)
- Separation of concerns: “api” for HTTP, “internal” for business logic/config/db, “frontend” for UI
- Stateless services, JWT for auth, rate limiting for resiliency
- Micro-components approach: each middleware is a discrete unit
d. Key technologies & frameworks:
- Frontend: Next.js, React, TypeScript, ESLint, PostCSS
- Backend: Go, Gorilla Mux, GORM, JWT
- Database: PostgreSQL
- Containerization: Docker, Docker-Compose
- CI/CD: GitHub Actions
- Documentation: Swagger

##### Detailed capture of essential elements
- For the frontend, show Next.js SSR vs. static assets vs. API invocation.
- .env/config component feeding settings into both frontend and backend.
- Docker network connections between containers.
- Port mappings (e.g., :8080 for API, :3000 for Next.js if applicable).

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Frontend Layer
    subgraph "Frontend Layer"
        direction TB
        client["Browser/Mobile Client"]:::frontend
        pages["Pages & Layout"]:::frontend
        components["Components (ClientLayout.tsx, Providers.tsx)"]:::frontend
        apiClient["API Client (lib/api.ts)"]:::frontend
        authHook["Auth Hook (useAuth.ts)"]:::frontend
        configFE["Config & Tooling"]:::frontend
        nextServer["Next.js SSR/Static Server"]:::frontend
    end

    %% Backend Layer
    subgraph "Backend Layer"
        direction TB
        server["Go REST API Server"]:::backend
        docsGen["Swagger Docs Generator"]:::backend
        routes["Route Definitions"]:::backend
        subgraph "Middleware Chain"
            direction TB
            authMW["Auth Middleware"]:::backend
            rateMW["Rate Limit Middleware"]:::backend
            logMW["Logging Middleware"]:::backend
            sanitizeMW["Sanitize Payload Middleware"]:::backend
            errorMW["Error Handling Middleware"]:::backend
            httpsMW["HTTPS Redirect Middleware"]:::backend
            contextMW["Context Middleware"]:::backend
        end
        subgraph "Data Layer"
            direction TB
            modelsAPI["API-facing Models"]:::backend
            dbWrapper["DB Wrapper"]:::backend
            dbInterface["DB Interface"]:::backend
            gormImpl["GORM Implementation"]:::backend
        end
        jwt["JWT Auth"]:::backend
        validation["Validation Utilities"]:::backend
        configBE["Config Loader"]:::backend
        migrations["DB Migrations"]:::backend
        errors["Error Definitions"]:::backend
        utilsBE["General Utilities"]:::backend
        db["PostgreSQL Database"]:::database
    end

    %% Infrastructure & CI/CD
    subgraph "Infrastructure & CI/CD"
        direction TB
        dockerCompose["Docker-Compose"]:::infra
        dockerfile["Dockerfile"]:::infra
        subgraph "CI/CD Pipeline"
            direction TB
            ciGitHub["GitHub Actions"]:::infra
            build["Build"]:::infra
            testStage["Test"]:::infra
            push["Push to Registry"]:::infra
            deploy["Deploy"]:::infra
        end
    end

    %% Connections
    client -->|"requests pages"| pages
    pages -->|"renders via SSR"| nextServer
    nextServer -->|"HTTP /v1 calls"| server
    server --> docsGen
    server --> routes
    routes --> authMW
    authMW --> rateMW
    rateMW --> logMW
    logMW --> sanitizeMW
    sanitizeMW --> errorMW
    errorMW --> httpsMW
    httpsMW --> contextMW
    contextMW -->|calls| routes
    routes -->|invokes| modelsAPI
    modelsAPI --> dbWrapper
    dbWrapper --> dbInterface
    dbInterface --> gormImpl
    gormImpl --> db
    jwt --> authMW
    validation --> routes
    configBE --> server
    migrations --> db
    errors --> server
    utilsBE --> errorMW
    dockerCompose --> server
    dockerCompose --> db
    ciGitHub --> build --> testStage --> push --> deploy

    %% Click Events
    click pages "https://github.com/italo-gouveia/myanimeapi/tree/main/frontend/src/app/"
    click components "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/src/components/ClientLayout.tsx"
    click components "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/src/components/Providers.tsx"
    click apiClient "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/src/lib/api.ts"
    click authHook "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/src/hooks/useAuth.ts"
    click configFE "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/next.config.ts"
    click configFE "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/package.json"
    click configFE "https://github.com/italo-gouveia/myanimeapi/blob/main/frontend/tsconfig.json"
    click server "https://github.com/italo-gouveia/myanimeapi/blob/main/cmd/main.go"
    click docsGen "https://github.com/italo-gouveia/myanimeapi/blob/main/cmd/docs/docs.go"
    click routes "https://github.com/italo-gouveia/myanimeapi/blob/main/api/routes/routes.go"
    click authMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/auth_middleware.go"
    click rateMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/rate_limit_middleware.go"
    click logMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/logging_middleware.go"
    click sanitizeMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/sanitize_payload_middleware.go"
    click errorMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/error_handling_middleware.go"
    click httpsMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/https_middleware.go"
    click contextMW "https://github.com/italo-gouveia/myanimeapi/blob/main/api/middleware/context_middleware.go"
    click modelsAPI "https://github.com/italo-gouveia/myanimeapi/blob/main/api/models/models.go"
    click dbWrapper "https://github.com/italo-gouveia/myanimeapi/blob/main/api/database/database.go"
    click dbInterface "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/db/db_interface.go"
    click gormImpl "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/db/gorm_db.go"
    click jwt "https://github.com/italo-gouveia/myanimeapi/blob/main/api/auth/auth.go"
    click validation "https://github.com/italo-gouveia/myanimeapi/blob/main/api/utils/validation.go"
    click configBE "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/config/config.go"
    click migrations "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/database/migrations/000003_create_favorites_table.up.sql"
    click errors "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/errors/errors.go"
    click utilsBE "https://github.com/italo-gouveia/myanimeapi/blob/main/internal/utils/utils.go"
    click dockerfile "https://github.com/italo-gouveia/myanimeapi/tree/main/Dockerfile"
    click dockerCompose "https://github.com/italo-gouveia/myanimeapi/blob/main/docker-compose.yml"
    click ciGitHub "https://github.com/italo-gouveia/myanimeapi/blob/main/.github/workflows/ci.yml"
    click ciGitHub "https://github.com/italo-gouveia/myanimeapi/blob/main/.github/workflows/deploy.yml"
    click ciGitHub "https://github.com/italo-gouveia/myanimeapi/blob/main/.github/workflows/pr-description.yml"

    %% Styles
    classDef frontend fill:#CCE5FF,stroke:#333,stroke-width:1px
    classDef backend fill:#CCFFCC,stroke:#333,stroke-width:1px
    classDef infra fill:#FFE5CC,stroke:#333,stroke-width:1px
    classDef database fill:#FFD700,stroke:#333,stroke-width:1px
```
