From eaaf04991ae1e59288aa0df2bb96d10cc309351f Mon Sep 17 00:00:00 2001 From: Nikolay Kiryanov Date: Fri, 14 Jun 2024 10:33:50 +0300 Subject: [PATCH] How to run inside docker and how to start --- Dockerfile | 14 ++++++++ README.md | 73 ++++++++++++++++++++++++++++++++++++---- src/app/conf/settings.py | 2 +- 3 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..8061f1a --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +ARG PYTHON_VERSION +FROM python:${PYTHON_VERSION}-slim-bookworm + +ADD requirements.txt / + +RUN pip install --no-cache-dir -r /requirements.txt + +WORKDIR /src + +ADD src/ . + +USER nobody + +ENTRYPOINT ["python", "entrypoint.py"] diff --git a/README.md b/README.md index 330f97b..845fd77 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,69 @@ -### websockets-notifications demo +### websockets-notifications Demo -#### How to set JWK public key to validate jwt tokens -One of the options: -1. By setting the key as an environment variable named `JWT_PUBLIC_KEY`. -2. By placing the key in a file named `jwt_public_key.pem` inside the `secrets` folder in repo root. +This project requires Python 3.12+. +Dependencies are managed by [uv](https://github.com/astral-sh/uv) and should be installed. +The list of requirements is stored in `pyproject.toml`. -If both options are set, the application will not start. +### Some Facts About Environment Variables: +1. When running in a container, do not forget to set `WEBSOCKETS_HOST` in the environment variables. In most cases, it should be `0.0.0.0`. +2. The app requires a JWT public key for authentication. It can be set using one of two options (if both are set, the app will not start): + 1. Environment variable `JWT_PUBLIC_KEY`. Ensure it has no newlines. Check `env.example` for the correct format. + 2. A file named `jwt_public_key.pem` inside the `src` directory. + +### Develop on a Local Machine +Install and activate a virtual environment: +```bash +uv venv +source venv/bin/activate +``` + +Set environment variables +```bash +cp env.example ./src/.env # default environment variables +``` + +Install requirements: +```bash +make # compile and install deps +``` + +Run message broker: +```bash +docker compose up -d +``` + +Run server: +```bash +cd python src/entrypoint.py +``` + +Connect to the server in cli: +```bash +python -m websockets ws://localhost:{% port %}/{% websockets_path % } +``` + +Format code with ruff +```bash +make fmt +``` + +Run linters +```bash +make lint +``` + +Run tests +```bash +make test +``` + +## Build docker image +If you need to set python version manually +```bash +docker build --build-arg "PYTHON_VERSION=3.11.6" --tag websocket-notifications . +``` + +Preferred way is to use `.python-version` file +```bash +docker build --build-arg "PYTHON_VERSION=$(cat .python-version)" --tag websocket-notifications . +``` diff --git a/src/app/conf/settings.py b/src/app/conf/settings.py index b5c1409..26ce0b9 100644 --- a/src/app/conf/settings.py +++ b/src/app/conf/settings.py @@ -26,7 +26,7 @@ class Settings(BaseSettings): env_file=".env", env_file_encoding="utf-8", case_sensitive=True, - secrets_dir="../secrets", + secrets_dir=".", )