Skip to content

Commit

Permalink
Add a basic litestar app
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisLovering committed Aug 1, 2024
1 parent de3448a commit 525887d
Show file tree
Hide file tree
Showing 14 changed files with 1,575 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
**

# Except what we need
!app
!thallium
!requirements.txt
!LICENSE
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ ARG git_sha="development"
ENV GIT_SHA=$git_sha

# Install project dependencies
WORKDIR /app
WORKDIR /thallium
COPY requirements.txt ./
RUN pip install -r requirements.txt

# Copy the source code in last to optimize rebuilding the image
COPY . .

ENTRYPOINT ["python"]
CMD ["-m", "app"]
ENTRYPOINT ["uvicorn"]
CMD ["thallium.app:app", "--host", "0.0.0.0", "--port", "80"]
23 changes: 17 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,29 @@
.PHONEY: setup sync lock lint precommit
.PHONY: all install relock lock lockci lint lintdeps precommit test retest

setup: sync precommit lint
all: install precommit lint

sync:
install:
poetry install --sync

lock:
relock:
poetry lock
@poetry export --only main --output requirements.txt
poetry install --sync --no-root
pre-commit run --files pyproject.toml poetry.lock requirements.txt

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

lockci: relock lintdeps

lock: relock install lintdeps

lint:
poetry run pre-commit run --all-files

precommit:
poetry run pre-commit install

test:
pytest -n 4 --ff

retest:
pytest -n 4 --lf
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ Run `make` from the project root to both install this project's dependencies & i

## Other make targets
- `make lint` will run the pre-commit linting against all files in the repository
- `make lock` wil relock project dependencies and update the [`requirements.txt`](./requirements.txt) file with production dependencies
- `make lock` wil relock project dependencies, install them to your environment, and update the [`requirements.txt`](./requirements.txt) file with production dependencies
- `make test` / `make retest` will run the test suite. `retest` will only run the tests that failed on the last test run.
6 changes: 0 additions & 6 deletions app/__main__.py

This file was deleted.

7 changes: 5 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
services:
app:
thallium:
build: .
restart: unless-stopped
command: ["thallium.app:app", "--host", "0.0.0.0", "--port", "80", "--reload"]
volumes:
- ./app:/app/app:ro
- ./thallium:/thallium/thallium:ro
env_file:
- .env
ports:
- "127.0.0.1:8000:80"
971 changes: 970 additions & 1 deletion poetry.lock

Large diffs are not rendered by default.

16 changes: 15 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ version = "1.2.0"
description = "Distribute printful prizes to winners."
authors = ["Chris Lovering <[email protected]>"]
license = "MIT"
package-mode = false

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

litestar = { version = "^2.10.0", extras = ["standard"] }
pydantic = "^2.8.2"
pydantic-settings = "^2.4.0"

Expand All @@ -18,6 +20,11 @@ ruff = "^0.5.5"
[tool.poetry.group.dev.dependencies]
poetry-plugin-export = "*"

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

[build-system]
requires = ["poetry-core>=1.5.0"]
build-backend = "poetry.core.masonry.api"
Expand All @@ -43,7 +50,14 @@ ignore = [
]

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

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

[tool.pytest.ini_options]
# addopts = "--ignore=examples"
asyncio_mode = "auto"
543 changes: 543 additions & 0 deletions requirements.txt

Large diffs are not rendered by default.

Empty file added tests/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions tests/test_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from litestar.testing import TestClient

from thallium.app import app


def test_heartbeat() -> None:
with TestClient(app=app) as client:
assert client.get("/heartbeat").json() == {"detail": "I am alive!"}
2 changes: 1 addition & 1 deletion app/__init__.py → thallium/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from app.settings import SETTINGS
from thallium.settings import SETTINGS

# Console handler prints to terminal
console_handler = logging.StreamHandler()
Expand Down
10 changes: 10 additions & 0 deletions thallium/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from litestar import Litestar, get


@get("/heartbeat", sync_to_thread=False)
def heartbeat() -> dict:
"""Return a simple heartbeat."""
return {"detail": "I am alive!"}


app = Litestar([heartbeat])
File renamed without changes.

0 comments on commit 525887d

Please sign in to comment.