A robust URL shortening service built with Go, featuring a RESTful API, MongoDB storage, and Docker support. This service allows you to create, manage, and track shortened URLs with comprehensive statistics.
- β¨ Create shortened URLs
- π Update existing URLs
- π Track URL access statistics
- π RESTful API
- π Input validation
- π Structured logging
- π³ Docker support
- π§ͺ Comprehensive tests
- Go 1.21+
- MongoDB
- Docker & Docker Compose
- Zap Logger
- Gorilla Mux
- Testify (testing)
- Go 1.21 or higher
- Docker and Docker Compose
- MongoDB (if running locally)
- Clone the repository:
git clone https://github.com/letsmakecakes/urlshortener.git
cd urlshortener- Start the services:
docker-compose up -dThe following services will be available:
- URL Shortener API: http://localhost:8080
- MongoDB Express UI: http://localhost:8081 (admin/password)
- Set up environment variables:
cp .env.example .env
# Edit .env with your configuration- Install dependencies:
go mod download- Run the application:
go run cmd/api/main.goPOST /shorten
Content-Type: application/json
{
"url": "https://example.com/some/long/url"
}Response:
{
"id": "1",
"url": "https://example.com/some/long/url",
"shortCode": "abc123",
"createdAt": "2024-01-02T12:00:00Z",
"updatedAt": "2024-01-02T12:00:00Z"
}GET /shorten/{shortCode}Response:
{
"id": "1",
"url": "https://example.com/some/long/url",
"shortCode": "abc123",
"createdAt": "2024-01-02T12:00:00Z",
"updatedAt": "2024-01-02T12:00:00Z"
}PUT /shorten/{shortCode}
Content-Type: application/json
{
"url": "https://example.com/updated/url"
}DELETE /shorten/{shortCode}GET /shorten/{shortCode}/statsResponse:
{
"id": "1",
"url": "https://example.com/some/long/url",
"shortCode": "abc123",
"accessCount": 42,
"createdAt": "2024-01-02T12:00:00Z",
"updatedAt": "2024-01-02T12:00:00Z"
}go test ./internal/... -v# Ensure the service is running
INTEGRATION_TEST=true go test ./tests/integration -vurlshortener/
βββ cmd/
β βββ api/
β βββ main.go # Application entry point
βββ internal/
β βββ api/ # HTTP handlers and routes
β βββ config/ # Configuration management
β βββ domain/ # Domain models and interfaces
β βββ pkg/ # Internal packages
β βββ service/ # Business logic
βββ pkg/ # Public packages
βββ tests/ # Integration tests
βββ docker-compose.yml # Docker composition
- Define the handler in
internal/api/handlers/url_handler.go - Add the route in
internal/api/routes/routes.go - Implement business logic in
internal/service/url_service.go - Add tests for the new functionality
The service uses Zap for structured logging. Logs are configured based on the environment:
- Development: Console-friendly, colored output
- Production: JSON format for better parsing
Monitor the application using:
# View logs
docker-compose logs -f app
# Access MongoDB Express
open http://localhost:8081- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.