Skip to content

gnik-nadroj/url-shortener

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

URL Shortener

A simple and fast URL shortener service written in Go.

Features

  • Exposes a REST API with two endpoints: /shortener and /stats
  • Uses a base62 encoder to generate short URLs from an incremental index
  • Stores data in a Redis database with key-value pairs and lists
  • Tracks the number of clicks for each shortened URL

Architecture

alt text

Installation

To run this project, you need to have Go and Redis installed on your system.

  • Clone this repository: git clone https://github.com/gnik-nadroj/url-shortener
  • Change directory to the project folder: cd url-shortener/server
  • Deploy: docker-compose up
  • The server will listen on http://localhost:3000

Usage

The API has two endpoints: /shortener and /stats.

/shortener

This endpoint accepts a POST request with a JSON body containing the original URL to be shortened. For example:

{
  "url": "https://www.google.com"
}

The response will be a JSON object with the shortened URL and the original URL. For example:

{
  "shortURL": "http://localhost:8080/4fRg3"
}

The shortened URL will have at least 5 characters, and will be generated by adding the value of 62^4 to the current index of the shortened URLs, and then converting it to base62. For example, if the current index is 123, the shortened URL will be 62^4 + 123 = 14776351, and then base62(14776351) = 4fRg3.

/stats

This endpoint accepts a GET request and returns a JSON array with all the shortened URLs and their corresponding original URLs and click counts. For example:

[
  {
    "shortURL": "http://localhost:8080/4fRg3",
    "OriginalURL": "https://github.com/gnik-nadroj/url-shortener/tree/main/server/encoding",
    "clicks": 12
  },
  {
    "shortURL": "http://localhost:8080/6tUx8",
    "OriginalURL": "https://google.com",
    "clicks": 7
  },
  {
    "shortURL": "http://localhost:8080/9aFb2",
    "OriginalURL": "https://bing.com",
    "clicks": 3
  }
]

The click count for each shortened URL is incremented every time the short URL is visited and redirected to the original URL.

/signup

This endpoint accepts a POST request with a JSON body containing the username and password. For example:

{
  "username": "ali",
  "password": "4dL&a8mok$5TjL&j"
}

The response will be a JSON object with a status message. For example:

{
  "message": "User registered successfully"
}

/login

This endpoint accepts a POST request with a JSON body containing the username and password . For example:

{
  "username": "ali23429",
  "password": "4dL&a8mok$5TjL&j"
}

The response will be a JSON object with a status message. For example:

{
  "message": "You are logged in"
}

The session of the user is manage by the server that send a cookie to the client representing is current session

/logout

To logout the user.

License

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

About

A simple implementation for a url shortener in Go

Resources

License

Stars

Watchers

Forks