Skip to content

Commit

Permalink
Merge pull request #95 from stuartmaxwell:maintenance
Browse files Browse the repository at this point in the history
Maintenance
  • Loading branch information
stuartmaxwell authored Oct 1, 2024
2 parents 6ee7839 + cbaa7cc commit 7688682
Show file tree
Hide file tree
Showing 12 changed files with 578 additions and 105 deletions.
53 changes: 53 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# Docker
.dockerignore
docker-compose*
Dockerfile*

# Git
.git/
.github/
.gitignore

# Virtual Environment
.venv/

# IDEs and editors
.idea
.vscode
.editorconfig

# Tool configuration
.mypy_cache/
.pytest_cache/
.ruff_cache/
.pre-commit-config.yaml
.prettierignore
# pyproject.toml
pytest.ini
.dmypy.json
dmypy.json

# Caddy config
Caddyfile*

# Project files not required
docs/
staticfiles/
env.template
requirements.in
README.md
media/
.env

# Macos yuck
.DS_Store

# Other files that sometimes creep in
**/*.sqlite3
**/*.csv
**/*.log
4 changes: 0 additions & 4 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,3 @@ jobs:
context: .
push: true
tags: ${{ env.REGISTRY }}/${{ github.repository }}:latest

# - name: Send POST request to Portainer webhook
# run: |
# curl --fail --retry 3 --max-time 10 -X POST "${{ secrets.PORTAINER_WEBHOOK }}" || exit 1
8 changes: 4 additions & 4 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ default_language_version:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
rev: v4.6.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -13,13 +13,13 @@ repos:
- id: check-case-conflict
- id: check-merge-conflict
- repo: https://github.com/adamchainz/django-upgrade
rev: "1.15.0" # replace with latest tag on GitHub
rev: "1.21.0" # replace with latest tag on GitHub
hooks:
- id: django-upgrade
args: [--target-version, "4.2"] # Replace with Django version
- repo: https://github.com/astral-sh/ruff-pre-commit
# Ruff version.
rev: v0.1.0
rev: v0.6.8
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand All @@ -36,7 +36,7 @@ repos:
- --tabwidth=2
exclude: markdown_editor.html
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.3
rev: v4.0.0-alpha.8
hooks:
- id: prettier
types_or:
Expand Down
29 changes: 18 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,35 @@ FROM python:3.12-slim-bookworm

RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y build-essential curl libpq-dev --no-install-recommends \
&& apt-get install -y build-essential libpq-dev --no-install-recommends \
&& rm -rf /var/lib/apt/lists/* /usr/share/doc /usr/share/man \
&& apt-get clean

# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE 1
# install uv
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv

# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED 1

# Install pip requirements
RUN python -m pip install --upgrade pip
COPY requirements.txt /app/requirements.txt
RUN python -m pip install -r /app/requirements.txt
# - Silence uv complaining about not being able to use hard links,
# - tell uv to byte-compile packages for faster application startups,
# - prevent uv from accidentally downloading isolated Python builds,
# - pick a Python,
# - and finally declare `/venv` as the target for `uv sync`.
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PYTHON=python3.12
# UV_PROJECT_ENVIRONMENT=/venv
# UV_CACHE_DIR=/cache

# Set up the environment
COPY pyproject.toml /app/
COPY uv.lock /app/
WORKDIR /app
RUN uv sync --locked --no-dev
COPY . /app
RUN mkdir /app/staticfiles

# Switching to a non-root user
RUN useradd appuser && chown -R appuser /app
USER appuser

# Expose port 8000 for Gunicorn
EXPOSE 8000
8 changes: 8 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
app:
build: .
entrypoint: /app/entrypoint.sh
command: uv run --no-cache manage.py runserver 0.0.0.0:8000
user: "1000"
ports:
- "8000:8000"
2 changes: 0 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3.8"

services:
app:
image: ghcr.io/stuartmaxwell/stuartm.nz:latest
Expand Down
20 changes: 15 additions & 5 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
#!/bin/sh
#!/bin/bash

if [ "$DB" = "postgresql" ]
# Exit immediately if any command fails
set -e

if [ "$DB_ENGINE" = "django.db.backends.postgresql" ]
then
echo "Waiting for postgresql..."

while ! nc -z $DB_HOST $DB_PORT; do
sleep 0.1
done

echo "PostgreSQL started"
echo "PostgreSQL ready!"
fi

python manage.py migrate --no-input
python manage.py collectstatic --no-input
# Apply database migrations
echo "Applying database migrations"
uv run --no-cache manage.py migrate --noinput

# Collect static files
echo "Collecting static files"
uv run --no-cache manage.py collectstatic --noinput

# Start the Django server with Gunicorn
echo "Starting server"
exec "$@"
85 changes: 85 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# Set the default recipe to list all available commands
default:
@just --list

# Set the Python version
python_version := "3.12"

# Set the uv run command
uv := "uv run --python 3.12 --extra test"

#Set the uv command to run a tool
uv-tool := "uv tool run"

# Run the Django development server
run:
@just sync
{{uv}} manage.py runserver

# Make migrations
makemigrations:
{{uv}} manage.py makemigrations

# Apply migrations
migrate:
{{uv}} manage.py migrate

# Create a superuser
createsuperuser:
{{uv}} manage.py createsuperuser

# Collect static files
collectstatic:
{{uv}} manage.py collectstatic

# Run Django shell
shell:
{{uv}} manage.py shell

# Check for any problems in your project
check:
{{uv}} manage.py check

# Run pytest
test:
{{uv}} pytest

# Run Ruff linking
lint:
{{uv-tool}} ruff check

# Run Ruff formatting
format:
{{uv-tool}} ruff format

# Sync the package
sync:
uv sync --python {{python_version}} --all-extras

# Sync and upgrade the package
sync-up:
uv sync --python {{python_version}} --all-extras --upgrade

# Lock the package version
lock:
uv lock

# Upgrade pre-commit hooks
pc-up:
{{uv-tool}} pre-commit autoupdate

# Run pre-commit hooks
pc-run:
{{uv-tool}} pre-commit run --all-files

# Run Docker compose up on the development environment
dc-up-dev:
docker-compose --file docker-compose-dev.yml up -d --build

# Run Docker compose logs on the development environment
dc-logs-dev:
docker-compose --file docker-compose-dev.yml logs -f

# Run a terminal on the development environment
dc-exec-dev:
docker compose --file docker-compose-dev.yml exec app /bin/bash
22 changes: 22 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
[project]
name = "stuartm-nz"
version = "0.1.0"
description = "stuartm.nz"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"django-environ>=0.11.2",
"django~=5.1",
"djpress~=0.8",
"psycopg>=3.2.2",
"psycopg-binary>=3.2.2",
"pymdown-extensions>=10.11",
"whitenoise>=6.7.0",
"gunicorn>=23.0.0",
"rich>=13.8.1",
"sentry-sdk>=2.14.0",
"django-debug-toolbar>=4.4.6",
"pytest>=8.3.3",
"pytest-django>=4.9.0",
]

[tool.curlylint.rules]
image_alt = true

Expand Down
12 changes: 0 additions & 12 deletions requirements.in

This file was deleted.

67 changes: 0 additions & 67 deletions requirements.txt

This file was deleted.

Loading

0 comments on commit 7688682

Please sign in to comment.