Skip to content

Latest commit

 

History

History
150 lines (122 loc) · 4.39 KB

README.md

File metadata and controls

150 lines (122 loc) · 4.39 KB

Server application

Requirements

Python 3.10+

Installation

Install Python (3.12) if it is not available in your package manager

These instructions are for Ubuntu 22.04. If you're on a different distribution, or - God forbid! - Windows, you should adjust these accordingly.

Also, these instructions are about using Poetry with Pyenv-managed (non-system) Python.

Step 1: Update and Install Dependencies

Before we install pyenv, we need to update our package lists for upgrades and new package installations. We also need to install dependencies for pyenv.

Open your terminal and type:

sudo apt-get update
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils \
tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

Step 2: Install Pyenv

We will clone pyenv from the official GitHub repository and add it to our system path.

git clone https://github.com/pyenv/pyenv.git ~/.pyenv
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
exec "$SHELL"

Step 3: Install Python 3.12

Now that pyenv is installed, we can install different Python versions. To install Python 3.12, use the following command:

pyenv install 3.12

Step 4: Connect Poetry to it

Do this in the template dir. Pycharm will automatically connect to it later

poetry env use ~/.pyenv/versions/3.12.1/bin/python

(change the version number accordingly to what is installed)

Finally, verify that Poetry indeed is connected to the proper version:

poetry enf info
  1. If you don't have Poetry installed run:
pip install poetry
  1. Install dependencies:
poetry config virtualenvs.in-project true
poetry install --no-root --with dev,test
  1. Launch the project:
poetry run uvicorn app.main:app [--reload]

or do it in two steps:

poetry shell
uvicorn app.main:app
  1. Running tests:
poetry run pytest

You can test the application for multiple versions of Python. To do this, you need to install the required Python versions on your operating system, specify these versions in the tox.ini file, and then run the tests:

poetry run tox

Package

To generate and publish a package on pypi.org, execute the following commands:

poetry config pypi-token.pypi <pypi_token>
poetry build
poetry publish

pypi_token - API token for authentication on PyPI. https://pypi.org/help/#apitoken

Docker

Build a docker image and run a container:

docker build . -t <image_name>:<image_tag>
docker run <image_name>:<image_tag>

Upload the Docker image to the repository:

docker login -u <username>
docker push <image_name>:<image_tag>

https://docs.docker.com/

Helm chart

Authenticate your Helm client in the container registry:

helm registry login <repo_url> -u <username>

Create a Helm chart:

helm package charts/<chart_name>

Push the Helm chart to container registry:

helm push <helm_chart_package> <repo_url>

Deploy the Helm chart:

helm repo add <repo_name> <repo_url>
helm repo update <repo_name>
helm upgrade --install <release_name> <repo_name>/<chart_name>

https://helm.sh/ru/docs/

OpenaAPI schema

To manually generate the OpenAPI schema, execute the command from the project root folder:

poetry --directory server run python ./tools/extract_openapi.py app.main:app --app-dir ./server --out ./api/openapi.yaml --app_version_file ./VERSION

Prometheus metrics

The application includes prometheus-fastapi-instrumentator for monitoring performance and analyzing its operation. It automatically adds an endpoint /metrics where you can access application metrics for Prometheus. These metrics include information about request counts, request execution times, and other important indicators of application performance. More on that at (Prometheus FastAPI Instrumentator)[https://github.com/trallnag/prometheus-fastapi-instrumentator]

Classy-FastAPI

Classy-FastAPI allows you to easily do dependency injection of object instances that should persist between FastAPI routes invocations, e.g. database connections. More on that (with examples) at Classy-FastAPI GitLab page.