URL Shortener
A simple URL shortener service written in Go (Golang) that provides a RESTful API for creating and retrieving shortened URLs.
Features
- Create Short URLs: Generate shortened URLs that redirect to the original addresses.
- Custom Short Codes: Optionally specify custom codes for your URLs.
- Redis Integration: Uses Redis for fast storage and retrieval.
- Docker Support: Easily deployable with Docker.
- Unit Tests: Includes tests to ensure reliability.
Installation
Prerequisites
- Go (version 1.16 or higher)
- Redis
- Docker (optional, for containerization)
Clone the Repository
git clone https://github.com/mikandro/url_shortener.git
cd url_shortener
Create a .env file in the root directory and set the necessary environment variables:
REDIS_ADDR=localhost:6379
REDIS_PASSWORD=
REDIS_DB=0
SERVER_PORT=8080
Run the Application
Using Go
go run cmd/url_shortener/main.go
Using Docker
docker build -t url_shortener .
docker run -p 8080:8080 --env-file .env url_shortener
Usage
API Endpoints
-
POST /shorten
Create a shortened URL.
Request Body:
{
"url": "https://www.example.com",
"custom_code": "example"
}
Note: The custom_code field is optional.
Response:
{
"short_url": "http://localhost:8080/{code}"
}
-
GET /{code}
Redirects to the original URL associated with the given code.
Example
Create a Short URL
curl -X POST -H "Content-Type: application/json" -d '{"url":"https://www.example.com"}' http://localhost:8080/shorten
Response:
{
"short_url": "http://localhost:8080/abc123"
}
Redirect Using Short URL
Navigate to http://localhost:8080/abc123 in your browser to be redirected to https://www.example.com.
Dependencies
Testing
Run the unit tests using:
go test ./...
Contributing
Contributions are welcome! Please open an issue or submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Created by mikandro. Feel free to reach out!