A URL shortening service built with Spring Boot, using Redis as the primary datastore. The service allows for secure random URL generation with support for TTL (Time-To-Live), which automatically deletes URLs from Redis when they expire.
- Secure URL Shortening: Generates unique, random, non-sequential short URLs using
SecureRandom
. - TTL Management: Allows setting an expiration time (TTL) for each shortened URL, with Redis automatically removing the expired entries.
- Redis for Storage: Redis is used for storing short URLs and efficiently managing their expiration.
- Redirection: Shortened URLs are redirected using HTTP status 302 (Found) for temporary redirection.
- Error Handling: Custom exceptions (
UrlNotFoundException
) and enhanced error handling.
- Java 17+ (or another compatible version)
- Maven
- Redis (for store and TTL management)
-
Clone the repository:
git clone https://github.com/vusalaxndzde/url-shortener.git cd url-shortener
-
Build the project:
mvn clean install
-
Configure the application properties in
src/main/resources/application.properties
orapplication.yml
to connect to Redis:spring.redis.host=localhost spring.redis.port=6379
-
If you don't have Redis running locally, you can use Docker to set it up:
docker-compose up
-
Start the application:
mvn spring-boot:run
Once the application is running, you can shorten URLs, manage their TTL, and use the shortened URLs to perform redirects.
-
POST
/api/v1/urls
- Description: Shortens a given long URL with an optional TTL.
- Request Body:
{ "url": "https://www.example.com", "ttl": { "days": 1, "hours": 5, "minutes": 30 } }
- Response:
{ "shortedUrl": "http://localhost:8080/abc123", "originalUrl": "https://www.example.com", "expiresAt": "2024-09-14 13:48:40" }
-
GET
/api/v1/urls/{shortedUrl}
- Description: Redirects the shortened URL to the original long URL.
- Example:
http://localhost:8080/api/v1/urls/abc123
- Spring Boot: For building the application.
- Maven: For project dependency management.
- Redis: Used for storing short URLs and managing TTL.
- Docker: Provides an easy setup for Redis.
If Redis is not running locally, you can quickly set it up using Docker:
- Ensure Docker and Docker Compose are installed.
- Run Redis:
docker-compose up -d
This will start Redis in the background.
- Fork the repository.
- Create a new branch:
git checkout -b feature/your-feature
- Commit your changes:
git commit -m "Add your commit message here"
- Push to the branch:
git push origin feature/your-feature
- Open a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.