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 -d
The 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.go
POST /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}/stats
Response:
{
"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 -v
urlshortener/
├── 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.