A robust REST API built with FastifyJS for managing tasks, featuring metrics collection, OpenAPI documentation, and PostgreSQL database integration.
- RESTful API endpoints for task management
- PostgreSQL database with Drizzle ORM
- OpenAPI documentation (Swagger UI)
- Prometheus metrics collection
- Environment-based configuration
- TypeScript support
- Node.js
- PostgreSQL database
- pnpm (recommended) or npm
The application uses environment variables for configuration. Create a .env
file with the following variables:
PORT=4000 # API port (default: 4000)
HOST=0.0.0.0 # Host to bind (default: 0.0.0.0)
DATABASE_URL= # PostgreSQL connection URL
LOG_LEVEL=info # Logging level (default: info)
METRICS_PREFIX=app_ # Metrics prefix (default: app_)
# Install dependencies
pnpm install
# Generate database schema
pnpm run db:migrate
# Run the migrations
pnpm run db:migrate
# Push the migrations to the database
pnpm run db:push
# Open the database studio
pnpm run db:studio
# Development mode
pnpm run dev
# Production mode
pnpm run build
pnpm start
- Swagger UI:
http://localhost:4000/docs
- OpenAPI JSON:
http://localhost:4000/docs.json
- Health check:
http://localhost:4000/healthcheck
- Metrics:
http://localhost:4000/metrics
- Structured JSON logging
- Log levels (debug, info, warn, error)
- Request/Response correlation IDs
- Performance metrics logging
The application uses Prometheus and Grafana for metrics visualization:
# Start monitoring stack
docker compose up -d prometheus grafana
Access points:
- Grafana:
http://localhost:3000
(admin/password) - Prometheus:
http://localhost:9090
Available metrics:
- HTTP request counts and latencies
- Database query performance
- Node.js runtime metrics (memory, CPU, GC)
- Custom business metrics
The metrics endpoint (/metrics
) should NOT be publicly exposed because:
- Potential DoS vector through frequent polling
- Exposes internal system information
- High resource consumption for metric generation
Best practices:
- Use separate port for metrics
- Implement authentication
- Use reverse proxy with IP whitelist
- Set rate limits
Example secure nginx configuration:
location /metrics {
auth_basic "Metrics";
auth_basic_user_file /etc/nginx/.htpasswd;
proxy_pass http://localhost:4000;
allow 10.0.0.0/8; # Internal network
deny all;
}
src/
├── config.ts # Configuration management
├── db/ # Database setup and migrations
├── modules/ # Feature modules (tasks, etc.)
└── utils/
├── http.ts # HTTP utilities
├── logger.ts # Logging setup
├── metrics.ts # Prometheus metrics
└── server.ts # Fastify server setup
- Fastify - Web framework
- Drizzle ORM - Database ORM
- Zod - Schema validation
- prom-client - Prometheus metrics
This project is licensed under the MIT License.