NOTE: This document is AI-generated
A high-performance URL redirection service built with Fastify, TypeScript, MikroORM(PostgreSQL) and Redis. This service handles the actual redirection of shortened URLs to their original destinations.
- ⚡ Fast URL redirection with Redis caching
- 🔒 Password protection for shortened URLs
- 📊 URL analytics tracking - capture visit count
- 💾 Database operation with MikroORM
- 🧪 Comprehensive test suite with Jest
- 🐳 Docker support
- 🔄 Automatic URL expiration handling
- Framework: Fastify
- Language: TypeScript
- Database: MikroORM with PostgreSQL
- Caching: Redis
- Testing: Jest
- Container: Docker
- Node.js 20.x or later
- Redis
- PostgreSQL(migration done with Prisma - refer backend-management/)
- npm
-
Clone the repository
-
Install dependencies:
npm install
-
Create a
.env
file based onsample.env
:cp sample.env .env
-
Set up the database:
Migrations done on backend-management/
-
Start the development server:
npm run dev
The service will be available at http://localhost:3001.
Create a .env.e2e
for connecting to test environment
# Run unit tests
npm test
# Run E2E tests
npm run test:e2e
npm run build
Build the Docker image:
docker build -t url-shortener-redirector .
# or use the docker compose file in project root
Run the container:
docker run -p 3001:3001 --env-file=.env url-shortener-redirector
# or use the docker compose file in project root
Redirects to the original URL.
Parameters:
shortCode
(path): The shortened URL code
Response:
- 302: Redirects to the original URL
- 404: URL not found/URL expired
- 401: Password required
- 400: Malformed request
Verifies password for protected URLs.
Parameters:
shortCode
(path): The shortened URL codepassword
(body): The password to verify
Response:
- 200: Password verified
- 401: Invalid password
- 404: URL not found/URL expired
- 400: Malformed request
backend-redirector/
├── src/
├── __e2e__ # End-to-end tests
├── modules/
│ ├── common/ # Shared utilities and types
│ └── url/ # URL redirection handlers
├── plugins/ # Fastify plugins, db, redis
├── app.ts # Fastify boostraper
├── server.ts # Application entry point
├── mikro-orm.config.ts # dbconfig
└── test/ # test setup
PORT
: Server port (default: 3002)DATABASE_URL
: PostgreSQL connection stringREDIS_URL
: Redis connection stringNODE_ENV
: Environment (development/production/e2e)
- Create a feature branch
- Make your changes
- Run tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.