Skip to content

Repository files navigation

Shortie

Simple API written in Python with Tornado framework and MongoDB as a storage to shortify URLs.

How to run it?

Requirements

  • Python >= 3.6
  • MongoDB >= 3.2

Using Docker and docker-compose

Install docker and docker-compose

To start Shortie just run in your terminal:

$ docker-compose build && docker-compose up -d

Because of use jwilder/nginx-proxy and docker-compose it's easy to scale the application:

$ docker-compose scale app=5

It also has a little impact on requests, each request should have added header Host: shortie.local.

Standard way

Make sure you have installed proper version of Python and MongoDB!

  1. Create and activate virtualenv
    $ python3 -m venv /path/to/virtual/env
    $ source /path/to/virtual/env/bin/activate
  2. Install required python libraries
    $ pip install -r requirements.txt
  3. Generate secret:
    $ python generate_secret.py
  4. Set proper MongoDB connection string in constant variable MONGO_URL in file shortie/settings/production.py
  5. Start Shortie application
    $ SHORTIE_SETTINGS=.production python -m shortie.run --port=8000 --host=0.0.0.0

API endpoints

Register user

Creates new user in database and returns authorization token.

POST /api/register

Parameters

Name Type Description
email string User email address
password string User password
{
    "email": "user_email",
    "password": "user_password"
}

CURL Example

$ curl -X POST -H "Host: shortie.local" -d '{"email": "user_email", "password": "user_password"}' localhost/api/register

Response

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo"
}

Authorize

Search for user in database and returns authorization token.

POST /api/auth

Parameters

Name Type Description
email string User email address
password string User password
{
    "email": "user_email",
    "password": "user_password"
}

CURL Example

$ curl -X POST -H "Host: shortie.local" -d '{"email": "user_email", "password": "user_password"}' localhost/api/auth

Response

{
    "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo"
}

Generate shortie

Generates short url.

POST /api/shortie

Parameters

Name Type Description
url string URL address to shortify
name string Optional Custom user name for created shortie
{
    "url": "http://example.com"
}
{
    "url": "http://example.com",
    "name": "myName"
}

CURL Example

$ curl -X POST -H "Host: shortie.local" -d '{"url": "http://example.com"}' localhost/api/shortie

Response

{
    "shortie": "http://localhost/api/shortie/lejRe"
}

CURL Example

$ curl -X POST -H "Host: shortie.local" -d '{"url": "http://example.com", "name": "myName"}' localhost/api/shortie

Response

{
    "shortie": "http://localhost/api/shortie/myName"
}

Adding Authorization header assigns created shortie to user.

CURL Example

$ curl -X POST -H "Host: shortie.local" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo" -d '{"url": "http://example.com"}' localhost/api/shortie

Get full URL

Returns full URL.

GET /api/shortie/:shortie

CURL Example

$ curl -H "Host: shortie.local" localhost/api/shortie/lejRe

Response

{
    "url": "http://example.com"
}

Get shortie info

GET /api/info

Returns all informations about single shortie or returns all user shorties.

Name Type Description
shortie string Shortie name
email string User email

CURL Example

$ curl -H "Host: shortie.local" localhost/api/info?shortie=lejRe

Response

{
    "url": "http://example.com",
    "shortie": "lejRe",
    "clicks": [
        "2017-02-12 18:44:13.60100"
    ],
    "created_at" "2017-02-11 12:45:23.98700"
}

CURL Example

$ curl -H "Host: shortie.local" localhost/api/info?email=test@test.com

Response

{
    "shorties": [
        {
            "url": "http://example.com",
            "shortie": "lejRe",
            "clicks": [
                "2017-02-12 18:44:13.60100"
            ],
            "created_at" "2017-02-11 12:45:23.98700"
        },
        {
            "url": "http://test.com",
            "shortie": "myName",
            "clicks": [
                "2017-02-12 10:55:33.60100"
            ],
            "created_at" "2017-02-10 08:05:23.12870"
        }
    ]
}

Tests

Tests are prepared to run in docker to make the dev/production environment clear.

To run tests go to ./tests directory and run:

$ ./test.sh

About

Simple API written in Python with Tornado framework and MongoDB as a storage to shortify URLs.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages