Skip to content

Commit

Permalink
Move to fastapi & mono repo layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Aug 16, 2024
1 parent 525887d commit 71d1ea5
Show file tree
Hide file tree
Showing 17 changed files with 1,116 additions and 1,598 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
description: Checks the validity of the pyproject.toml file.
entry: poetry check
language: system
files: pyproject.toml
files: thallium-backend/pyproject.toml
pass_filenames: false
require_serial: true

Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ install:

relock:
poetry lock
@poetry export --only main --output requirements.txt
@poetry export --only main --output thallium-backend/requirements.txt

lintdeps:
@pre-commit run --files pyproject.toml poetry.lock requirements.txt
@pre-commit run --files pyproject.toml poetry.lock thallium-backend/requirements.txt

lockci: relock lintdeps

Expand Down
28 changes: 23 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
services:
postgres:
restart: unless-stopped
image: postgres:16-alpine
environment:
POSTGRES_DB: thallium
POSTGRES_PASSWORD: thallium
POSTGRES_USER: thallium
healthcheck:
test: ["CMD-SHELL", "pg_isready -U thallium"]
interval: 2s
timeout: 1s
retries: 5
ports:
- "15432:5432"

thallium:
build: .
build: thallium-backend
restart: unless-stopped
command: ["thallium.app:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
volumes:
- ./thallium:/thallium/thallium:ro
command: ["src.app:fastapi_app", "--host", "0.0.0.0", "--port", "80", "--reload"]
# volumes:
# - ./thallium:/thallium/thallium:ro
env_file:
- .env
ports:
- "127.0.0.1:8000:80"
- "8000:80"
depends_on:
postgres:
condition: service_healthy
1,504 changes: 601 additions & 903 deletions poetry.lock

Large diffs are not rendered by default.

18 changes: 11 additions & 7 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
[tool.poetry]
name = "thallium"
version = "1.2.0"
description = "Distribute printful prizes to winners."
description = "Distribute printful prizes to users with vouchers"
authors = ["Chris Lovering <[email protected]>"]
license = "MIT"
package-mode = false

[tool.poetry.dependencies]
python = "3.12.*"
python = "^3.12.0"

litestar = { version = "^2.10.0", extras = ["standard"] }
alembic = {version = "^1.13.2", extras = ["tz"]}
fastapi = "^0.112.0"
httpx = "^0.23.3"
sqlalchemy = {version = "^2.0.32", extras = ["asyncio"]}
psycopg = {version = "^3.2.1", extras = ["binary"]}
pydantic = "^2.8.2"
pydantic-settings = "^2.4.0"
uvicorn = "^0.30.6"

[tool.poetry.group.linting.dependencies]
pre-commit = "^3.8.0"
Expand All @@ -21,7 +26,7 @@ ruff = "^0.5.5"
poetry-plugin-export = "*"

[tool.poetry.group.test.dependencies]
pytest ="^8.3.2"
pytest = "^8.3.2"
pytest-asyncio = "^0.23.8"
pytest-xdist = "^3.6.1"

Expand All @@ -34,7 +39,6 @@ target-version = "py312"
extend-exclude = [".cache"]
line-length = 120
unsafe-fixes = true
preview = true
output-format = "concise"

[tool.ruff.lint]
Expand All @@ -50,13 +54,13 @@ ignore = [
]

[tool.ruff.lint.isort]
known-first-party = ["tests", "thallium"]
known-first-party = ["tests", "src"]
order-by-type = false
case-sensitive = true
combine-as-imports = true

[tool.ruff.lint.per-file-ignores]
"tests/**" = ["D103", "S101"]
"*/tests/**" = ["D103", "S101"]

[tool.pytest.ini_options]
# addopts = "--ignore=examples"
Expand Down
648 changes: 0 additions & 648 deletions requirements.txt

This file was deleted.

8 changes: 0 additions & 8 deletions tests/test_app.py

This file was deleted.

2 changes: 1 addition & 1 deletion .dockerignore → thallium-backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
**

# Except what we need
!thallium
!src
!requirements.txt
!LICENSE
2 changes: 1 addition & 1 deletion Dockerfile → thallium-backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ RUN pip install -r requirements.txt
COPY . .

ENTRYPOINT ["uvicorn"]
CMD ["thallium.app:app", "--host", "0.0.0.0", "--port", "80"]
CMD ["src.app:fastapi_app", "--host", "0.0.0.0", "--port", "80"]
430 changes: 430 additions & 0 deletions thallium-backend/requirements.txt

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions thallium/__init__.py → thallium-backend/src/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import logging

from thallium.settings import SETTINGS
from src.settings import CONFIG

# Console handler prints to terminal
console_handler = logging.StreamHandler()
level = logging.DEBUG if SETTINGS.debug else logging.INFO
level = logging.DEBUG if CONFIG.debug else logging.INFO
console_handler.setLevel(level)

# Remove old loggers, if any
Expand All @@ -17,6 +17,6 @@
logging.basicConfig(
format="%(asctime)s - %(name)s %(levelname)s: %(message)s",
datefmt="%D %H:%M:%S",
level=logging.DEBUG if SETTINGS.debug else logging.INFO,
level=logging.DEBUG if CONFIG.debug else logging.INFO,
handlers=[console_handler],
)
25 changes: 25 additions & 0 deletions thallium-backend/src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import logging

from fastapi import FastAPI, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse

from src.settings import CONFIG

log = logging.getLogger(__name__)


fastapi_app = FastAPI(debug=CONFIG.debug)


@fastapi_app.get("/heartbeat")
def health_check() -> JSONResponse:
"""Return basic response, for use as a health check."""
return JSONResponse({"detail": "I am alive!"})


@fastapi_app.exception_handler(RequestValidationError)
def pydantic_validation_error(request: Request, error: RequestValidationError) -> JSONResponse:
"""Raise a warning for pydantic validation errors, before returning."""
log.warning("Error from %s: %s", request.url, error)
return JSONResponse({"error": str(error)}, status_code=422)
9 changes: 9 additions & 0 deletions thallium-backend/src/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from pydantic_settings import BaseSettings


class _CONFIG(BaseSettings, env_file=".env", env_file_encoding="utf-8"):
debug: bool = False
git_sha: str = "development"


CONFIG = _CONFIG()
File renamed without changes.
9 changes: 9 additions & 0 deletions thallium-backend/tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from fastapi.testclient import TestClient

from src.app import fastapi_app


def test_heartbeat() -> None:
"""Ensure the heartbeat works."""
with TestClient(app=fastapi_app) as client:
assert client.get("/heartbeat").json() == {"detail": "I am alive!"}
10 changes: 0 additions & 10 deletions thallium/app.py

This file was deleted.

9 changes: 0 additions & 9 deletions thallium/settings.py

This file was deleted.

0 comments on commit 71d1ea5

Please sign in to comment.