From af9e3bf4131b2cd0754901b45975202de5edf916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20N=C3=B8rgaard?= Date: Mon, 6 May 2024 23:51:37 +0100 Subject: [PATCH] add docker --- .dockerignore | 9 +++++++++ .env.template | 2 ++ Dockerfile | 26 ++++++++++++++++++++++++++ README.md | 16 +++++++++++++++- config.template.toml | 20 ++++++++++---------- docker-compose.yaml | 42 ++++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 8 ++++---- redis.conf | 2 ++ 8 files changed, 110 insertions(+), 15 deletions(-) create mode 100644 .dockerignore create mode 100644 .env.template create mode 100644 Dockerfile create mode 100644 docker-compose.yaml create mode 100644 redis.conf diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..69e7796 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.git/ +.github/ +types_/ +config.template.toml +Dockerfile +LICENSE +migration.sql +pyproject.toml +README.md diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..272b827 --- /dev/null +++ b/.env.template @@ -0,0 +1,2 @@ +POSTGRES_USER=mystbin +POSTGRES_PASSWORD=mystbin diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5c0ee04 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,26 @@ +FROM python:3.12-slim + +LABEL org.opencontainers.image.source=https://github.com/pythonistaguild/mystbin-backend +LABEL org.opencontainers.image.description="Mystbin's Python Backend" +LABEL org.opencontainers.image.licenses=GPLv3 + +RUN mkdir -p /etc/apt/keyrings \ + && apt update -y \ + && apt-get install --no-install-recommends -y \ + # deps for building python deps + git \ + build-essential \ + libcurl4-gnutls-dev \ + gnutls-dev \ + libmagic-dev \ + && rm -rf /var/lib/apt/lists/* + +# copy project requirement files here to ensure they will be cached. +WORKDIR /app +COPY requirements.txt ./ + +# install runtime deps +RUN pip install -Ur requirements.txt + +COPY . /app/ +ENTRYPOINT python -O launcher.py diff --git a/README.md b/README.md index 1aa3c08..f3f6b08 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Easily share code and text. [Staging Website](https://staging.mystb.in) -### Running Locally +### Running without Docker **Requirements:** - Postgres @@ -20,3 +20,17 @@ Easily share code and text. - Install dependencies (Preferably to a `venv`): `pip install -Ur requirements.txt` - Optionally in `core/server.py` set `ignore_localhost=` to `False` in the RateLimit Middleware for testing. - Run: `python launcher.py` + +### Running with Docker +**Requirements** +- Docker +- Docker Compose + +**Setup:** +- Clone +- Copy `config.template.toml` into `config.toml` + - The default config for database (and redis) should work Out of Box. +- Optionally in `core/server.py` set `ignore_localhost=` to `False` in the RateLimit Middleware for testing. +- Run `docker compose up -d` to start the services. + - If you want to use redis for session/limit handling, run with the redis profile: `docker compose --profile redis up -d` + - The redis container doesn't expose connections outside of the network, but for added security edit `redis.conf` and change the password. diff --git a/config.template.toml b/config.template.toml index e095ef0..784aa61 100644 --- a/config.template.toml +++ b/config.template.toml @@ -2,23 +2,23 @@ host = "localhost" port = 8181 domain = "https://mystb.in" -session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64)) +session_secret = "" # Run: import secrets; print(secrets.token_urlsafe(64)) [DATABASE] -dsn = "postgres://USER:PASSWORD@localhost:5432/mystbin" +dsn = "postgres://mystbin:mystbin@database:5432/mystbin" [REDIS] -limiter = "" # Optional -sessions = "" # Optional +limiter = "redis://redis:6739/0" # Optional +sessions = "redis://redis:6739/1" # Optional [LIMITS] -paste_get = {rate=30, per=60, priority=1, bucket="ip"} -paste_get_day = {rate=7200, per=86400, priority=2, bucket="ip"} -paste_post = {rate=10, per=60, priority=1, bucket="ip"} -paste_post_day = {rate=1440, per=86400, priority=2, bucket="ip"} -global_limit = {rate=21600, per=86400, priority=1, bucket="ip"} +paste_get = { rate = 30, per = 60, priority = 1, bucket = "ip" } +paste_get_day = { rate = 7200, per = 86400, priority = 2, bucket = "ip" } +paste_post = { rate = 10, per = 60, priority = 1, bucket = "ip" } +paste_post_day = { rate = 1440, per = 86400, priority = 2, bucket = "ip" } +global_limit = { rate = 21600, per = 86400, priority = 1, bucket = "ip" } [PASTES] char_limit = 300_000 file_limit = 5 -name_limit = 25 \ No newline at end of file +name_limit = 25 diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..170da93 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,42 @@ +services: + mystbin: + build: + context: . + dockerfile: Dockerfile + container_name: mystbin + ports: + - 8181:8181 + restart: unless-stopped + depends_on: + database: + condition: service_healthy + restart: true + + database: + image: postgres:latest + container_name: mystbin-database + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}"] + interval: 10s + timeout: 5s + retries: 5 + env_file: .env + environment: + - PG_DATA=/var/lib/postgresql/data + - POSTGRES_DB=mystbin + volumes: + - mystbin_pg_data:/var/lib/postgresql/data + + redis: + image: redis:latest + container_name: mystbin-redis + restart: unless-stopped + profiles: + - redis + volumes: + - "./redis.conf:/config/redis.conf:ro" + command: ["redis-server", "/config/redis.conf"] + +volumes: + mystbin_pg_data: diff --git a/pyproject.toml b/pyproject.toml index c5eb883..ee4903a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [tool.ruff] line-length = 120 indent-width = 4 -exclude = ["venv"] +exclude = ["venv", ".venv"] [tool.ruff.lint] select = [ @@ -34,7 +34,7 @@ ignore = [ "ANN401", "UP031", "PTH123", - "E203", + "E203", "E501", ] @@ -56,9 +56,9 @@ skip-magic-trailing-comma = false line-ending = "auto" [tool.pyright] -exclude = ["venv"] +exclude = ["venv", ".venv"] useLibraryCodeForTypes = true typeCheckingMode = "strict" reportImportCycles = false reportPrivateUsage = false -pythonVersion = "3.11" \ No newline at end of file +pythonVersion = "3.11" diff --git a/redis.conf b/redis.conf new file mode 100644 index 0000000..fc14686 --- /dev/null +++ b/redis.conf @@ -0,0 +1,2 @@ +port 6379 +requirepass ChangeMeHere!!