Simple API written in Python with Tornado framework and MongoDB as a storage to shortify URLs.
- Python >= 3.6
- MongoDB >= 3.2
Install docker and docker-compose
To start Shortie just run in your terminal:
$ docker-compose build && docker-compose up -dBecause of use jwilder/nginx-proxy and docker-compose it's easy to scale the application:
$ docker-compose scale app=5It also has a little impact on requests, each request should have added header Host: shortie.local.
Make sure you have installed proper version of Python and MongoDB!
- Create and activate virtualenv
$ python3 -m venv /path/to/virtual/env $ source /path/to/virtual/env/bin/activate - Install required python libraries
$ pip install -r requirements.txt
- Generate secret:
$ python generate_secret.py
- Set proper MongoDB connection string in constant variable MONGO_URL in file
shortie/settings/production.py - Start Shortie application
$ SHORTIE_SETTINGS=.production python -m shortie.run --port=8000 --host=0.0.0.0
Creates new user in database and returns authorization token.
POST /api/register
| Name | Type | Description |
|---|---|---|
| string | User email address | |
| password | string | User password |
{
"email": "user_email",
"password": "user_password"
}$ curl -X POST -H "Host: shortie.local" -d '{"email": "user_email", "password": "user_password"}' localhost/api/register{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo"
}Search for user in database and returns authorization token.
POST /api/auth
| Name | Type | Description |
|---|---|---|
| string | User email address | |
| password | string | User password |
{
"email": "user_email",
"password": "user_password"
}$ curl -X POST -H "Host: shortie.local" -d '{"email": "user_email", "password": "user_password"}' localhost/api/auth{
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo"
}Generates short url.
POST /api/shortie
| 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 -X POST -H "Host: shortie.local" -d '{"url": "http://example.com"}' localhost/api/shortie{
"shortie": "http://localhost/api/shortie/lejRe"
}$ curl -X POST -H "Host: shortie.local" -d '{"url": "http://example.com", "name": "myName"}' localhost/api/shortie{
"shortie": "http://localhost/api/shortie/myName"
}Adding
Authorizationheader assigns created shortie to user.
$ curl -X POST -H "Host: shortie.local" -H "Authorization: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0ODY5NzAxNzUsImVtYWlsIjoidGVzdEB0ZXN0LmNvbSJ9.gk5Iktvj-YK1kUkAS-g7gqmJy-mtXED6SCSiIPvjXFo" -d '{"url": "http://example.com"}' localhost/api/shortieReturns full URL.
GET /api/shortie/:shortie
$ curl -H "Host: shortie.local" localhost/api/shortie/lejRe{
"url": "http://example.com"
}GET /api/info
Returns all informations about single shortie or returns all user shorties.
| Name | Type | Description |
|---|---|---|
| shortie | string | Shortie name |
| string | User email |
$ curl -H "Host: shortie.local" localhost/api/info?shortie=lejRe{
"url": "http://example.com",
"shortie": "lejRe",
"clicks": [
"2017-02-12 18:44:13.60100"
],
"created_at" "2017-02-11 12:45:23.98700"
}$ curl -H "Host: shortie.local" localhost/api/info?email=test@test.com{
"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 are prepared to run in docker to make the dev/production environment clear.
To run tests go to ./tests directory and run:
$ ./test.sh