Skip to content

pj1527/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener

A simple, high-performance URL shortener service built with Go and Gin web framework, featuring multiple storage backends and a clean architecture.

Features

  • URL Shortening: Convert long URLs to short, memorable links
  • Automatic Redirection: Short URLs automatically redirect to their original destination
  • Multiple Storage Backends:
    • In-memory (default, non-persistent)
    • Redis (persistent, recommended for production)
  • RESTful API: Simple and intuitive API endpoints
  • Health Check: Built-in health check endpoint
  • Environment-based Configuration: Easy configuration through environment variables
  • Concurrent-safe: Thread-safe implementation for high concurrency

Getting Started

Prerequisites

  • Go 1.16 or higher
  • Git (for cloning the repository)
  • (Optional) Redis (for persistent storage)

Environment Variables

Create a .env file in the cmd/url-shortener directory with the following variables:

PORT=8080
REDIS_ADDR=localhost:6379  # Only required if using Redis
  • (Optional) Redis server if using Redis storage

Installation

  1. Clone the repository:

    git clone https://github.com/pj1527/url-shortner.git
    cd url-shortner
  2. Install dependencies:

    go mod download

Running the Application

Using In-Memory Storage (Default)

go run cmd/url-shortener/main.go

Using Redis Storage

  1. Make sure Redis is running
  2. Set up your .env file with Redis configuration
  3. Run the application:
    go run cmd/url-shortener/main.go

API Documentation

1. Shorten a URL

Request

curl -X POST http://localhost:8080/api/shorten \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com/very/long/url"}'

Response

{
  "short_url": "http://localhost:8080/abc123"
}

2. Redirect to Original URL

Request

curl -L http://localhost:8080/abc123

3. Health Check

Request

curl http://localhost:8080/health

Response:

{
  "status": "UP"
}

Architecture

The application follows a clean architecture with clear separation of concerns:

  • Handler: HTTP request handling and response formatting
  • Service: Business logic and use cases
  • Repository: Data access layer with support for multiple storage backends
  • Config: Environment configuration management
  • Utils: Helper functions and utilities

Configuration

Environment Variable Default Description
PORT 8080 Port to run the server on
REDIS_ADDR localhost:6379 Redis server address (if using Redis)

About

A RESTfull URL shortener built using Golang and Gin

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages