Skip to content

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.

License

Notifications You must be signed in to change notification settings

letsmakecakes/urlshortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener Service

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.

Features

  • ✨ Create shortened URLs
  • 🔄 Update existing URLs
  • 📊 Track URL access statistics
  • 🚀 RESTful API
  • 🔒 Input validation
  • 📝 Structured logging
  • 🐳 Docker support
  • 🧪 Comprehensive tests

Tech Stack

  • Go 1.21+
  • MongoDB
  • Docker & Docker Compose
  • Zap Logger
  • Gorilla Mux
  • Testify (testing)

Prerequisites

  • Go 1.21 or higher
  • Docker and Docker Compose
  • MongoDB (if running locally)

Getting Started

Running with Docker

  1. Clone the repository:
git clone https://github.com/letsmakecakes/urlshortener.git
cd urlshortener
  1. Start the services:
docker-compose up -d

The following services will be available:

Running Locally

  1. Set up environment variables:
cp .env.example .env
# Edit .env with your configuration
  1. Install dependencies:
go mod download
  1. Run the application:
go run cmd/api/main.go

API Documentation

Create Short URL

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 Original URL

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"
}

Update URL

PUT /shorten/{shortCode}
Content-Type: application/json

{
    "url": "https://example.com/updated/url"
}

Delete URL

DELETE /shorten/{shortCode}

Get URL Statistics

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"
}

Running Tests

Unit Tests

go test ./internal/... -v

Integration Tests

# Ensure the service is running
INTEGRATION_TEST=true go test ./tests/integration -v

Project Structure

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

Development

Adding New Endpoints

  1. Define the handler in internal/api/handlers/url_handler.go
  2. Add the route in internal/api/routes/routes.go
  3. Implement business logic in internal/service/url_service.go
  4. Add tests for the new functionality

Logging

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

Monitoring

Monitor the application using:

# View logs
docker-compose logs -f app

# Access MongoDB Express
open http://localhost:8081

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

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.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published