# devfullcycle/fc4-desafio-docker-node-api architecture

> Architecture diagram of the GitHub repository devfullcycle/fc4-desafio-docker-node-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/devfullcycle/fc4-desafio-docker-node-api
- Repository: https://github.com/devfullcycle/fc4-desafio-docker-node-api

## Overview

1. **What this repo is**
   - A small **Node.js + TypeScript REST API** for managing **feature flags**.
   - It persists data in **PostgreSQL** and exposes two main HTTP surfaces: **`/flags`** and **`/health`**.
   - The architecture is intentionally simple: HTTP layer → validation/error handling → repository/data access → PostgreSQL.

2. **Application bootstrap and process lifecycle**
   - `src/server.ts` is the runtime entrypoint that likely starts the HTTP listener and handles **graceful shutdown** on `SIGTERM`/`SIGINT`.
   - `src/app.ts` appears to be the app composition layer: it wires routers, middleware, and error handling together.
   - This split separates **server lifecycle** from **request handling**, which is useful for tests and clean shutdown behavior.

3. **HTTP boundary**
   - `src/flags/flags.router.ts` defines the CRUD API for feature flags.
   - `src/health/health.router.ts` defines the health endpoint.
   - `src/http/async-handler.ts`, `src/http/error-handler.ts`, and `src/http/http-error.ts` form a shared HTTP support layer:
     - async request handlers are wrapped consistently,
     - application errors are normalized,
     - HTTP status mapping is centralized.

4. **Feature flags subsystem**
   - The feature-flag domain is isolated under `src/flags/`.
   - `flags.router.ts` handles request/response wiring.
   - `flags.validation.ts` is the request boundary for validating payloads and parameters before they reach persistence.
   - `flags.repository.ts` is the data-access boundary for CRUD operations against PostgreSQL.
   - For an architecture diagram, this is the main business subsystem in the repo.

5. **Persistence layer**
   - `src/db/pool.ts` centralizes PostgreSQL connection pooling.
   - The repository layer depends on this pool rather than opening connections directly.
   - This creates a clean boundary: HTTP/business code does not know connection details; it only consumes repository methods.

6. **Migrations and schema lifecycle**
   - `src/db/migrate.ts` is the migration runner.
   - `src/db/migrations/` contains the versioned migration definitions:
     - `001-create-flags.ts` is the initial schema migration,
     - `index.ts` and `types.ts` organize migration registration and typing.
   - The repo treats migrations as a separate operational path from request serving, but they use the same database configuration and pool infrastructure.

7. **Configuration management**
   - `src/config/env.ts` centralizes environment-variable reading and defaults.
   - The app is designed to be configured externally rather than hardcoded, especially for:
     - `PORT`
     - PostgreSQL host/port/user/password/database
   - This is the main configuration boundary between runtime environment and application code.

8. **Health checking**
   - `GET /health` is not just a liveness check; per the README it confirms both the app and the database connection are healthy.
   - That means the health router likely depends on the same DB pool/repository infrastructure, making it a real dependency check rather than a static “process is up” endpoint.

9. **Data flow for a typical flag request**
   - Client sends HTTP request to `/flags`.
   - Router forwards to a handler wrapped by the async helper.
   - Validation checks the request shape and inputs.
   - Handler calls the repository.
   - Repository talks to PostgreSQL through the shared pool.
   - Result is returned through the HTTP layer with centralized error handling if something fails.

10. **Operational characteristics**
   - The README indicates the app supports:
     - `dev` mode with reload,
     - `build` to emit compiled output,
     - `start` to run compiled code,
     - `db:migrate` for schema updates.
   - That suggests the repo is organized for a **compile-then-run** production model, with migrations run as a distinct operational step.

11. **Boundaries to show in a diagram**
   - External boundary 1: **HTTP clients** calling the API.
   - External boundary 2: **PostgreSQL** as the only persistence dependency.
   - Internal boundaries:
     - routing vs validation,
     - business/data access vs connection management,
     - runtime server lifecycle vs application composition,
     - application requests vs migration runner.

12. **What is not present**
   - No frontend, queue worker, cache, or third-party API integration is visible in the tree.
   - So the architecture is a focused **single-service API** with one database-backed domain and a small shared HTTP/database support layer.

Main entry points: [`src/app.ts`](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/app.ts) (App), [`src/flags/flags.router.ts`](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.router.ts) (Flags router), [`src/db/pool.ts`](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/pool.ts) (Pool), [`src/db/migrate.ts`](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrate.ts) (Migrate)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_external["External"]
  node_clients(("Clients<br/>http client"))
  node_postgres[("PostgreSQL<br/>database")]
end

subgraph group_runtime["Runtime"]
  node_server["Server<br/>entrypoint<br/>[server.ts]"]
  node_app["App<br/>composition<br/>[app.ts]"]
  node_env["Env<br/>config<br/>[env.ts]"]
end

subgraph group_http["HTTP"]
  node_async_handler["Async handler<br/>http helper<br/>[async-handler.ts]"]
  node_error_handler["Error handler<br/>http helper<br/>[error-handler.ts]"]
  node_http_error["HTTP error<br/>error model<br/>[http-error.ts]"]
  node_health_router["Health router<br/>[health.router.ts]"]
end

subgraph group_flags["Flags"]
  node_flags_router["Flags router<br/>[flags.router.ts]"]
  node_flags_validation["Flags validation"]
  node_flags_repo["Flags repo"]
end

subgraph group_db["Database"]
  node_pool[("Pool<br/>[pool.ts]")]
end

subgraph group_migrations["Migrations"]
  node_migrate["Migrate<br/>migration runner<br/>[migrate.ts]"]
  node_migration_index["Migrations index<br/>migration registry<br/>[index.ts]"]
  node_migration_001["Create flags<br/>migration"]
  node_migration_types["Migration types<br/>[types.ts]"]
end

node_clients -->|"HTTP requests"| node_app
node_server -->|"bootstraps"| node_app
node_env -->|"port/runtime"| node_server
node_env -->|"config"| node_app
node_app -->|"mounts"| node_flags_router
node_app -->|"mounts"| node_health_router
node_app -->|"uses"| node_error_handler
node_flags_router -->|"wraps"| node_async_handler
node_flags_router -->|"validates"| node_flags_validation
node_flags_router -->|"calls"| node_flags_repo
node_flags_validation -->|"raises"| node_http_error
node_flags_repo -->|"queries"| node_pool
node_pool -->|"connects to"| node_postgres
node_health_router -->|"checks"| node_pool
node_migrate -->|"uses"| node_pool
node_migrate -->|"loads"| node_migration_index
node_migration_index -->|"includes"| node_migration_001
node_migration_index -->|"typed by"| node_migration_types
node_migrate -->|"applies to"| node_postgres

click node_server "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/server.ts"
click node_app "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/app.ts"
click node_env "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/config/env.ts"
click node_async_handler "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/async-handler.ts"
click node_error_handler "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/error-handler.ts"
click node_http_error "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/http-error.ts"
click node_flags_router "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.router.ts"
click node_flags_validation "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.validation.ts"
click node_flags_repo "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.repository.ts"
click node_health_router "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/health/health.router.ts"
click node_pool "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/pool.ts"
click node_migrate "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrate.ts"
click node_migration_index "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/index.ts"
click node_migration_001 "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/001-create-flags.ts"
click node_migration_types "https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/types.ts"

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_clients,node_postgres toneBlue
class node_server,node_app,node_env toneAmber
class node_async_handler,node_error_handler,node_http_error,node_health_router toneMint
class node_flags_router,node_flags_validation,node_flags_repo toneRose
class node_pool toneIndigo
class node_migrate,node_migration_index,node_migration_001,node_migration_types toneTeal
```

## Components

### External

Outside actors and services

- **Clients**: http client · API consumers calling the service
- **PostgreSQL**: database · Primary persistence store

### Runtime

Process startup and shared configuration

- [Server](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/server.ts): entrypoint · Starts HTTP listener and handles shutdown · `src/server.ts`
- [App](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/app.ts): composition · Wires middleware, routers, and errors · `src/app.ts`
- [Env](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/config/env.ts): config · Reads runtime environment settings · `src/config/env.ts`

### HTTP

Request handling and shared HTTP support

- [Async handler](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/async-handler.ts): http helper · Wraps async route handlers · `src/http/async-handler.ts`
- [Error handler](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/error-handler.ts): http helper · Normalizes application errors · `src/http/error-handler.ts`
- [HTTP error](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/http/http-error.ts): error model · Maps domain failures to status codes · `src/http/http-error.ts`
- [Health router](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/health/health.router.ts): router · Health and database check endpoint · `src/health/health.router.ts`

### Flags

Feature-flag domain

- [Flags router](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.router.ts): router · CRUD API for feature flags · `src/flags/flags.router.ts`
- [Flags validation](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.validation.ts): validation · Validates flag requests and parameters · `src/flags/flags.validation.ts`
- [Flags repo](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/flags/flags.repository.ts): repository · CRUD access for feature flags · `src/flags/flags.repository.ts`

### Database

PostgreSQL access and schema support

- [Pool](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/pool.ts): db pool · Shared PostgreSQL connection pool · `src/db/pool.ts`

### Migrations

Database migration flow

- [Migrate](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrate.ts): migration runner · Runs schema migrations · `src/db/migrate.ts`
- [Migrations index](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/index.ts): migration registry · Registers available migrations · `src/db/migrations/index.ts`
- [Create flags](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/001-create-flags.ts): migration · Initial flags table schema · `src/db/migrations/001-create-flags.ts`
- [Migration types](https://github.com/devfullcycle/fc4-desafio-docker-node-api/blob/main/src/db/migrations/types.ts): types · Migration typing support · `src/db/migrations/types.ts`

## Connections

- Clients → App: HTTP requests · Clients call the API surface
- Server → App: bootstraps · Server starts the composed app
- Env → Server: port/runtime · Provides process configuration
- Env → App: config · Feeds application setup
- App → Flags router: mounts · Registers feature-flag routes
- App → Health router: mounts · Registers health routes
- App → Error handler: uses · Installs centralized error handling
- Flags router → Async handler: wraps · Async route handlers are standardized
- Flags router → Flags validation: validates · Checks flag inputs before processing
- Flags router → Flags repo: calls · Routes delegate to repository operations
- Flags validation → HTTP error: raises · Validation failures become HTTP errors
- Flags repo → Pool: queries · Repository uses shared connection pool
- Pool → PostgreSQL: connects to · Pool manages PostgreSQL connections
- Health router → Pool: checks · Health endpoint verifies database reachability
- Migrate → Pool: uses · Migration runner shares the DB pool
- Migrate → Migrations index: loads · Discovers registered migrations
- Migrations index → Create flags: includes · Versioned schema change set
- Migrations index → Migration types: typed by · Migration contract definitions
- Migrate → PostgreSQL: applies to · Executes schema updates in PostgreSQL
