# castrozan/api-cepol-clean-code architecture

> Architecture diagram of the GitHub repository castrozan/api-cepol-clean-code, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/castrozan/api-cepol-clean-code
- Repository: https://github.com/castrozan/api-cepol-clean-code

## Overview

##### Project type and purpose
- This is a backend API microservice deployed as a Cloudflare Worker using Hono + Chanfana to auto-generate and validate an OpenAPI 3.1 schema.
- It implements CRUD endpoints for multiple domains (about, articles, equipments, professionals, researchs), authentication via Supabase, and image upload to a bucket.

##### Key layers/components (from the file tree)
- Presentation Layer (src/presentation/controllers, src/presentation/dtos)
- Application Layer (src/application/use-cases & dtos)
- Domain Layer (src/domain/entities & interfaces)
- Infrastructure Layer
- Database repositories (src/infrastructure/database/repositories)
- External services (Supabase Auth, Cloudflare bucket)
- Middleware (CORS, auth, rate limit)
- Web server/OpenAPI (src/infrastructure/web/open-api)
- Shared utilities/config (src/shared & src/env)
- Entry point (src/index.ts) and Worker config (wrangler.toml)

##### Architectural pattern
- Clean/Hexagonal Architecture
- Each controller calls a UseCase → operates on Domain Entities via Repository interfaces → concrete Infrastructure implementations interact with Supabase DB or bucket.
- Hono router (chanfana) sits in front to route requests, apply middleware, generate OpenAPI spec, and dispatch to controllers.

##### Components and relationships
a. External clients
- Browsers or HTTP clients calling REST endpoints
b. Cloudflare Worker runtime
- Wrangler-deployed edge functions executing src/index.ts
- Hono + Chanfana OpenAPI router
- Middleware (CORS, auth, rate limiting)
c. Presentation Layer
- Controllers (one per resource + upload + auth)
- Request/response DTOs
d. Application Layer
- UseCase classes (one per operation)
- Request/response DTOs
e. Domain Layer (center)
- Entities (About, Article, Equipment, Professional, Research + their Image variants)
- Repository & AuthService interfaces
f. Infrastructure Layer
- Database Repositories → Supabase (via supabaseClient.ts)
- SupabaseAuthService → Supabase Auth API
- Cloudflare bucket service → Cloudflare R2 or Images API
- OpenAPI server generator (chanfana)
g. Configuration & Utilities
- Environment loader (src/env)
- Shared error handling & validation
h. External systems
- Supabase (Postgres DB + Auth service)
- Cloudflare bucket (R2 or Images)
- Cloudflare Workers edge network

##### Relationships / Data flow
1. Client → HTTP request at Worker edge
2. Hono router → matching route → middleware (CORS, auth, rate limiting)
3. Controller receives request DTO → calls corresponding UseCase
4. UseCase invokes Domain logic → calls Repository/AuthService interface
5. Infrastructure implements interface → translates call to Supabase or bucket API → returns data/entities
6. UseCase returns domain entity → Controller maps to response DTO → Hono sends HTTP response
7. Chanfana auto-generates /openapi.json from controllers and DTO annotations

##### Putting it all together
- Inside Worker: Router + Middleware → Controllers → UseCases → Domain Interfaces → Infrastructure → External services.
- On one side, attach the auto-generated OpenAPI spec endpoint (/openapi.json).
- On another side, list shared config & utils as supporting components.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Clients
    CLIENT["Client (Browser/HTTP)"]:::external
    CLIENT --> WORKER_ENTRY

    %% Worker Entry and Config
    WORKER_ENTRY["Worker Entry Point\n(src/index.ts)"]:::runtime
    click WORKER_ENTRY "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/index.ts"
    WRANGLER_CFG["wrangler.toml"]:::config
    click WRANGLER_CFG "https://github.com/castrozan/api-cepol-clean-code/blob/master/wrangler.toml"
    WORKER_ENTRY --> WRANGLER_CFG
    WORKER_ENTRY --> ROUTER

    %% Router and Middleware
    ROUTER["Hono + Chanfana Router"]:::runtime
    click ROUTER "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/infrastructure/web/open-api/server.ts"
    ROUTER --> OPENAPI_SPEC
    ROUTER --> MIDDLEWARE

    OPENAPI_SPEC["OpenAPI Spec (/openapi.json)"]:::runtime

    MIDDLEWARE["Middleware\n(CORS, Auth, Rate Limit)"]:::infra
    click MIDDLEWARE "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/infrastructure/middleware"
    ROUTER --> MIDDLEWARE

    %% Presentation Layer
    CONTROLLERS["Controllers\n(src/presentation/controllers)"]:::presentation
    click CONTROLLERS "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/presentation/controllers"
    DTOs["Request/Response DTOs\n(UserDTO.ts)"]:::presentation
    click DTOs "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/presentation/dtos/UserDTO.ts"
    MIDDLEWARE --> CONTROLLERS
    CONTROLLERS --> DTOs
    CONTROLLERS --> USECASES

    %% Application Layer
    USECASES["Use Cases\n(src/application/use-cases)"]:::application
    click USECASES "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/application/use-cases"
    UC_DTOs["Use Case DTOs\n(src/application/dtos)"]:::application
    click UC_DTOs "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/application/dtos"
    CONTROLLERS --> USECASES
    USECASES --> UC_DTOs
    USECASES --> DOMAIN_INTERFACES

    %% Domain Layer
    DOMAIN_INTERFACES["Domain Interfaces\n(src/domain/interfaces)"]:::domain
    click DOMAIN_INTERFACES "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/domain/interfaces"
    ENTITIES["Entities\n(src/domain/entities)"]:::domain
    click ENTITIES "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/domain/entities"
    USECASES --> DOMAIN_INTERFACES
    DOMAIN_INTERFACES --> ENTITIES

    %% Infrastructure Layer
    subgraph Infrastructure Layer
        INFRA_REPOS_DB["Database Repositories\n(src/infrastructure/database/repositories)"]:::infra
        click INFRA_REPOS_DB "https://github.com/castrozan/api-cepol-clean-code/tree/master/src/infrastructure/database/repositories"
        SUPABASE_USER_REPO["Supabase User Repo\nSupabaseUserRepository.ts"]:::infra
        click SUPABASE_USER_REPO "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/infrastructure/database/supabase/SupabaseUserRepository.ts"
        AUTH_SERVICE_IMPL["Supabase Auth Service\nSupabaseAuthService.ts"]:::infra
        click AUTH_SERVICE_IMPL "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/infrastructure/services/supabase/SupabaseAuthService.ts"
        BUCKET_SERVICE["Cloudflare Bucket Service\ncloudflare.ts"]:::infra
        click BUCKET_SERVICE "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/infrastructure/services/bucket/cloudflare.ts"
    end
    DOMAIN_INTERFACES --> INFRA_REPOS_DB
    INFRA_REPOS_DB --> SUPABASE_USER_REPO
    DOMAIN_INTERFACES --> AUTH_SERVICE_IMPL
    DOMAIN_INTERFACES --> BUCKET_SERVICE

    %% External Systems
    SUPABASE_DB["Supabase DB (Postgres)"]:::external
    SUPABASE_AUTH["Supabase Auth API"]:::external
    CLOUDFLARE_BUCKET["Cloudflare Bucket (R2/Images)"]:::external
    SUPABASE_USER_REPO --> SUPABASE_DB
    AUTH_SERVICE_IMPL --> SUPABASE_AUTH
    BUCKET_SERVICE --> CLOUDFLARE_BUCKET

    %% Shared Config & Utils
    ENV_LOADER["Env Loader\n(src/env/index.ts)"]:::config
    click ENV_LOADER "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/env/index.ts"
    ENV_EXAMPLE["Env Example\n(src/env/env.example.ts)"]:::config
    click ENV_EXAMPLE "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/env/env.example.ts"
    SUPABASE_CLIENT["Supabase Client\n(src/shared/config/supabaseClient.ts)"]:::config
    click SUPABASE_CLIENT "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/shared/config/supabaseClient.ts"
    APP_ERROR["AppError\n(src/shared/errors/AppError.ts)"]:::config
    click APP_ERROR "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/shared/errors/AppError.ts"
    VALIDATOR["Validator\n(src/shared/utils/Validator.ts)"]:::config
    click VALIDATOR "https://github.com/castrozan/api-cepol-clean-code/blob/master/src/shared/utils/Validator.ts"
    WORKER_ENTRY --> ENV_LOADER
    ENV_LOADER --> ENV_EXAMPLE
    USECASES --> SUPABASE_CLIENT
    USECASES --> APP_ERROR
    USECASES --> VALIDATOR

    %% Classes
    classDef external fill:#e0f7e9,stroke:#2e7d32,color:#2e7d32,stroke-width:1px;
    classDef runtime fill:#e3f2fd,stroke:#1e88e5,color:#1e88e5,stroke-width:1px;
    classDef presentation fill:#e1f5fe,stroke:#0288d1,color:#0288d1,stroke-width:1px;
    classDef application fill:#e3f2fd,stroke:#1976d2,color:#1976d2,stroke-width:1px;
    classDef domain fill:#fff3e0,stroke:#ef6c00,color:#ef6c00,stroke-width:1px;
    classDef infra fill:#f3e5f5,stroke:#8e24aa,color:#8e24aa,stroke-width:1px;
    classDef config fill:#eceff1,stroke:#546e7a,color:#546e7a,stroke-width:1px;
```
