Code Monkey home page Code Monkey logo

tinyurl-example's Introduction

Url Shortener

URL shortener with a JSON RESTful API containing 3 endpoints.

  • GET method that redirects the user to the original URL.
  • POST method to create a shortened URL
  • GET method to return the app statistics

Build

Execute migration script in first build:
docker-compose up --detach --build; docker-compose exec app npm run tables:create

Database seeds

To seed the database with short URL id:example and statistic data for period:weeks execute npm script:
npm run tables:seedWeeks

Localhost environment variables
Create a .env file in the root of the project and populate it with variables:

DATABASE_URL=
REDIS_URL=
SERVER_PORT=
HOST=
NODE_ENV=development

API Endpoints

Shorten url

Accepts an optional short property that defines what the shortened URL should look like
If no short property was passed, pseudo-randomly generated string is used for ID

[POST] URI: `/api/shorten`  

HEADERS

Field Required  Description
x-api-key true  user api key, in this version it is required just to not be an empty string

BODY PAYLOAD

Field Required  Description
full true full url ready for shortening
 short  false custom shortening id

Redirect to origin

Redirects to the full-size URL

[GET] URI: `/:id`  

URL PARAMS

Field Required  Description
id true  id of the short url

Fetch statistics data

Returns valid data containing app usage statistics.

[GET] URI: `/api/statistics/:id`  

HEADERS

Field Required  Description
x-api-key true  user api key, in this version it is required just to not be an empty string

URL PARAMS

Field Required  Description
id true  id of the short url

URL QUERY

Field Required  Description Requirements
period true period of data  "week", "year", "day", "hour", "month"
order  false type of ordering "asc", "desc"
cursor  false ID of the last short URL from the list
take  false quantity of returned statistics data  no more than 100
from false  date as a starting point of statistics "YYYY-MM-DD"

Scaling features

Caching

Instead of connecting to database on every request, Redis is used as a cache layer. Redis keys are checked first, and then if data is not found on Redis, server connects to the Database and fetches the data.

Counting visits

To prevent many connections to the database, Redis is used as a data aggregator. When a user uses a short URL, the backend increases Redis correlated keys to that short URL and increases them by 1, also Redis sets the timer key to expire in N milliseconds.
After the timer key expires, Redis publishes an event that tigers the job which persists Redis data into a database.

tinyurl-example's People

Contributors

loncaa avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.