URL Shortener
Description
A URL shortener service built with Go. This application allows users to shorten long URLs and retrieve the original URLs using the shortened versions. It uses PostgreSQL for persistent storage and Redis for caching.
Features
- Shorten long URLs into 8-character short URLs.
- Retrieve the original URL using the short URL.
- Rate limiting to prevent abuse.
- Caching with Redis for faster lookups.
- Graceful shutdown of the server.
Prerequisites
- Docker and Docker Compose installed.
- Go 1.24 or higher installed (if running locally without Docker).
Installation
Using Docker
- Clone the repository:
git clone https://github.com/mojtabamovahedi/url-shorter.git
cd url-shorter
- Build and start the services:
docker-compose up --build
- The application will be available at
http://localhost:8080.
Running Locally
- Clone the repository:
git clone https://github.com/mojtabamovahedi/url-shorter.git
cd url-shorter
- Install dependencies:
go mod download
- Start PostgreSQL and Redis services (e.g., using Docker Compose):
docker-compose up postgres redis
- Run the application:
go run ./cmd/main.go
Usage
Shorten a URL
Send a POST request to /new with the following JSON body:
{
"url": "https://example.com"
}
Response:
{
"message": "success",
"short": "abcdefgh"
}
Redirect to the Original URL
Send a GET request to /:short (e.g., /abcdefgh). The server will redirect to the original URL.
Configuration
The application is configured using the config.yaml file. Update the file to change database, Redis, or server settings.
Project Structure
cmd/main.go: Entry point of the application.
internal/: Contains core business logic, services, and repositories.
api/handler/http/: HTTP handlers and middleware.
config/: Configuration-related files.
pkg/: Utility packages (e.g., database and cache).
License
This project is licensed under the MIT License. See the LICENSE file for details.