Skip to content

YOricH/TestShortener

Repository files navigation

Test Shortener

Русский|English

Example of a service - abbreviation of links (like bitly.com or tinyurl.com). Users are defined by session. PostgreSQL is used as a base, but you can easily configure it to any other available Django.

Features

  • Specifying your aliases
  • List of aliases created by the current user with pagination
  • caching aliases in Redis
  • Auto deletion of old aliases
  • REST API
  • Easy building with Docker compose or Kubernetes

Used technologies

Requirements

It is recommended to use a Unix-like system. The application is tested on Manjaro and Ubuntu.

To run without containerization

  • Python >= 3.11
  • pipenv >= 2022.10
  • PostgreSQL >= 14
  • Redis >= 7

To run on Docker Compose

  • GNU Make >= 4
  • Docker >= 20
  • Docker Compose >= 2

To run on Kubernetes

The build is tested and tuned for Minikube

  • GNU Make >= 4
  • Minikube >= 1.27

Installation

Download from source (requires GIT)

$ git clone https://github.com/yorich/TestShortener.git
$ cd TestShortener

Without containerization

  1. Install dependencies:
$ pipenv shell
$ pipenv install --ignore-pipfile
  1. Create an empty database PostgreSQL.
  2. If necessary, set environment variables. Or create a .env file in the root of the project where specify them. You can use the .env.dist file as an example.
  3. Create tables in the specified database:
$ python manage.py migrate
  1. If everything is successful, run the test server on the local system:
$ python manage.py runserver 8000

Docker Compose

  1. If necessary, configure the environment variables in the .env.compose file.
  2. Run with Make:
$ make compose.run
  1. To stop use the command
$ make compose.stop
  1. To remove containers, related networks and volumes:
$ make compose.clear

Downloaded images are not deleted.

Minikube

  1. If necessary, configure the environment variables in the .env.compose file.
  2. Run with Make:
$ make kube.run
  1. To stop and delete all created objects:
$ make kube.clear

Settings

All settings are made through the environment variables, or through the .env file (.env.compose if you use containers).

  • DATABASE_URI - database connection string. Mandatory variable.
  • ALLOWED_HOSTS - list of hosts / domains for which the current site can operate. Denoted by a comma separated string (example: 'example.com,foo.com'). Default: 'localhost,0.0.0.0,127.0.0.1'.
  • SECRET_KEY - see the documentation.
  • DEBUG - see the documentation. Default: False.
  • CACHE_URL - Redis connection string for cache. Mandatory variable.
  • BROKER_URL - connection string to the Redis base for Celery (message broker). Mandatory variable.
  • RESULT_BACKEND - connection string to the Redis base for Celery (task results). Mandatory variable.
  • USE_CACHE - use cache or not. Default: True.
  • LOG_LEVEL - logging level. Default: INFO.
  • USE_LOG_FILE - use log files or not. Default: False.
  • LOG_FILE_NAME - log file name. Default: 'shortener.log'.
  • PAGINATION_PAGE_SIZE - the number of objects returned through the REST API at one time. Default: 20.
  • BASE_ENCODING - the string that encodes the original link. Default: Base56.
  • SUBPART_HASH_LEN - the length of the hash used to encode the section. The higher the number, the longer the resulting link. Default: 11.
  • SESSION_COOKIE_AGE - lifetime of session cookies in seconds. Default: 1209600 (two weeks in seconds).
  • DIRECTION_LIFETIME_SEC - alias lifetime in seconds. By default, is equal to SESSION_COOKIE_AGE.
  • CACHE_ON_CREATE - save an alias in cache when creating. Default: True.
  • SCHEDULE_CLEAR_DATA_MINUTES - the period of the task to remove obsolete aliases in minutes. Default: 60.
  • LINES_ON_PAGE - the number of lines of aliases created by the user per page. Default: PAGINATION_PAGE_SIZE.
  • LAST_TRY_NUM - the number of attempts to create a random alias in the case that the alias by hash coincides with the existing one. Default: 10.

The rest of the settings are standard for Django and you can set it how you want in settings.py file.

REST API

End points

/api/directions/ - short aliases for links.

Available methods

GET, POST, HEAD, OPTIONS

/api/userdirections/ - short links, associated with users. You can only work with objects belonging to the current user (defined through the session).

Available methods

GET, POST, HEAD, OPTIONS

More details can be found by logging in through the browser to <you_shortener_host>/api/.