Skip to content

Latest commit

 

History

History
97 lines (68 loc) · 1.79 KB

README.md

File metadata and controls

97 lines (68 loc) · 1.79 KB

URL Shortener Service

A simple URL shortener service written in Go. This service allows users to submit any URL to be shortened and receive a valid shortened URL that will forward the user's request to the original URL.

Features

  • Shorten any valid URL
  • Redirect to the original URL using the shortened URL
  • JSON responses for errors and successful operations

Prerequisites

  • Go (version 1.18 or higher)
  • Docker (optional, for containerized testing)
  • Git

Getting Started

Clone the Repository

git clone https://github.com/arshadsiddique/url-shortner.git
cd url-shortner

Running Locally

  1. Install Dependencies:

    go mod download
  2. Run the Application:

    go run main.go
  3. Test the Application:

    go test -v ./...

Using Docker

  1. Build the Docker Image:

    docker build -t urlshortener:latest .
  2. Run the Docker Container:

    docker run -p 8080:8080 urlshortener:latest

API Endpoints

  • Shorten URL:

    PUT /shorten
    Content-Type: application/json
    
    Request Body:
    {
       "destination": "https://www.google.com"
    }
      
    Response Body:
    {
       "shortened_url": "http://localhost:8080/abc123"
    }
  • Redirect to Original URL:

    GET /{shortcode}
    
    Example:
    GET /abc123

Testing the Endpoints

  • Shorten a URL:

    curl -X PUT http://localhost:8080/shorten -H "Content-Type: application/json" -d '{"destination": "https://www.google.com"}'
  • Redirect to the Original URL:

    Assuming the shortened URL returned is http://localhost:8080/abc123, you can use:

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