Skip to content

Commit

Permalink
add docker
Browse files Browse the repository at this point in the history
  • Loading branch information
AbstractUmbra committed May 6, 2024
1 parent 71f6e8f commit af9e3bf
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 15 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.git/
.github/
types_/
config.template.toml
Dockerfile
LICENSE
migration.sql
pyproject.toml
README.md
2 changes: 2 additions & 0 deletions .env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
POSTGRES_USER=mystbin
POSTGRES_PASSWORD=mystbin
26 changes: 26 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Easily share code and text.
[Staging Website](https://staging.mystb.in)


### Running Locally
### Running without Docker
**Requirements:**
- Postgres

Expand All @@ -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.
20 changes: 10 additions & 10 deletions config.template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
name_limit = 25
42 changes: 42 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -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:
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tool.ruff]
line-length = 120
indent-width = 4
exclude = ["venv"]
exclude = ["venv", ".venv"]

[tool.ruff.lint]
select = [
Expand Down Expand Up @@ -34,7 +34,7 @@ ignore = [
"ANN401",
"UP031",
"PTH123",
"E203",
"E203",
"E501",
]

Expand All @@ -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"
pythonVersion = "3.11"
2 changes: 2 additions & 0 deletions redis.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
port 6379
requirepass ChangeMeHere!!

0 comments on commit af9e3bf

Please sign in to comment.