How devfullcycle/fc4-desafio-docker-node-api works
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: app.ts App · flags.router.ts Flags router · pool.ts Pool · migrate.ts Migrate
Components 17
External
Outside actors and services
- Clients · API consumers calling the service
- PostgreSQL · Primary persistence store
Runtime
Process startup and shared configuration
HTTP
Request handling and shared HTTP support
- Async handler · Wraps async route handlers
src/http/async-handler.ts - Error handler · Normalizes application errors
src/http/error-handler.ts - HTTP error · Maps domain failures to status codes
src/http/http-error.ts - Health router · Health and database check endpoint
src/health/health.router.ts
Flags
Feature-flag domain
- Flags router · CRUD API for feature flags
src/flags/flags.router.ts - Flags validation · Validates flag requests and parameters
src/flags/flags.validation.ts - Flags repo · CRUD access for feature flags
src/flags/flags.repository.ts
Migrations
Database migration flow
- Migrate · Runs schema migrations
src/db/migrate.ts - Migrations index · Registers available migrations
src/db/migrations/index.ts - Create flags · Initial flags table schema
src/db/migrations/001-create-flags.ts - Migration types · Migration typing support
src/db/migrations/types.ts
Connections 19
- 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