A robust, scalable, and professional REST API built with NestJS for shortening, managing, and tracking URLs. This project is fully containerized with Docker and follows industry best practices for code quality, testing, and observability.
- JWT Authentication: Secure user registration and login system.
- URL Shortening & Management: Full CRUD operations for both anonymous and authenticated users.
- Public Redirection: High-performance
GET /:shortCodeendpoint for redirection and click tracking. - Dependency Inversion: Built with a decoupled architecture using token-based injection for services.
- Interactive API Docs: Explore and test the API with Swagger at
/api-docs. - Dockerized Environment: Run the entire application and database with a single command.
- Observability Foundations:
- Structured Logging: Production-ready JSON logs via Pino.
- Health Checks:
GET /healthendpoint for monitoring application status.
- Fully Tested: Comprehensive and stable test suite covering all business logic (unit and E2E).
- Professional Tooling: Code quality enforced by ESLint, Prettier, and Husky pre-commit hooks.
- Docker
- Docker Compose (usually included with Docker Desktop)
- Node.js (v18+) and Yarn for local development outside Docker.
This is the simplest way to get the entire environment up and running.
-
Clone the repository:
git clone https://github.com/Pizzy-23/url-shorter.git cd url-shorter -
Create your environment file: Copy the example environment file and fill in your details.
cp .env.example .env
Important: Make sure to set a strong, unique
JWT_SECRET. -
Build and run the containers:
docker-compose up --build
- The API will be available at
http://localhost:3000. - The interactive Swagger documentation will be at
http://localhost:3000/api-docs. - To run in the background, use
docker-compose up -d --build.
- The API will be available at
-
To stop the application:
docker-compose down
This project has a full suite of unit tests and end-to-end tests. You can run them with:
# Run all unit tests
yarn test
# Run only service-level unit tests
yarn test:services
# Run only controller-level unit tests
yarn test:controllers
# Run only end-to-end tests
yarn test:e2e
# Run tests in watch mode
yarn test:watch
---This project is instrumented with the foundations for a robust observability setup, essential for operating in a production environment.
The application uses Pino for high-performance, structured (JSON) logging, which is ideal for production environments.
- In Development: Logs are formatted with
pino-prettyfor readability. - In Production: Logs are output as JSON, ready for ingestion by services like Datadog, Splunk, or the ELK stack.
The log level can be configured with the
LOG_LEVELenvironment variable.
The application exposes a /metrics endpoint with metrics in the Prometheus format. Custom metrics include:
url_shortened_total: Total number of URLs shortened.url_redirect_total: Total redirects, labeled byshort_code.
Distributed tracing is implemented using the OpenTelemetry standard.
- To enable, set
OTEL_ENABLED=true. - To configure a backend (like Jaeger or Datadog), set the
OTEL_EXPORTER_OTLP_TRACES_ENDPOINTvariable.
For a horizontally-scaled, high-throughput environment, the following challenges and improvements should be considered:
- Challenge: In a distributed system with multiple API instances running, a race condition could occur where two instances generate the same nanoid short code simultaneously before either has saved it to the database, causing a unique constraint violation.
- Solution: Implement a retry-on-conflict mechanism in the service layer. For very high-scale scenarios, the best solution is a dedicated ID generation service (like Twitter's Snowflake or a simple key-value store like Redis) to provide pre-generated, guaranteed-unique short codes to the API instances.
- Challenge: To improve performance, a cache (like Redis or Memcached) would be added to store frequently accessed URLs. However, when a user updates or deletes a URL on one instance, all other instances might continue serving stale data from their local or shared cache.
- Solution: Implement a Pub/Sub mechanism (e.g., Redis Pub/Sub). When a URL is modified, a cache invalidation event is published. All API instances subscribe to this channel and, upon receiving an event, know to evict the specific shortCode from their cache.
- Challenge: Managing
.envfiles across multiple instances and environments is not secure or scalable. Committing secrets to a repository is a major security risk. - Solution: Adopt a secret management service like HashiCorp Vault, AWS Parameter Store, or Azure Key Vault allow you to centralize and securely manage application configuration, injecting them into the application environment at runtime instead of relying on local files.
This project is UNLICENSED.