Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,42 @@ jobs:
uses: actions/checkout@v4

- name: Setup Python
uses: actions/setup-python@v4
id: setup-python
uses: actions/setup-python@v5
with:
python-version: 3.11
cache: 'pip'
python-version: '3.12'

- name: Load cached Poetry installation
id: cached-poetry
uses: actions/cache@v3
with:
path: ~/.local
key: poetry-0

- name: Install Poetry
if: steps.cached-poetry.outputs.cache-hit != 'true'
uses: snok/install-poetry@v1
with:
version: '1.8.2'
virtualenvs-create: true
virtualenvs-in-project: true

- name: Load cached venv
id: cached-poetry-dependencies
uses: actions/cache@v3
with:
path: .venv
key: venv-${{ runner.os }}-${{ steps.setup-python.outputs.python-version }}-${{ hashFiles('**/poetry.lock') }}

- name: Install dependencies
run: |
pip install -r requirements-cpu.txt
pip install -r requirements-dev.txt
if: steps.cached-poetry-dependencies.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

- name: Lint
run: |
python -m ruff check .
run: make lint-check

- name: Test and coverage
run: |
python -m pytest --cov=. --cov-report term --cov-report xml:coverage.xml
run: make coverage

- name: Code Coverage Report
uses: irongut/CodeCoverageSummary@v1.3.0
Expand Down
4 changes: 2 additions & 2 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
**

# ... except for dependencies
!Pipfile
!Pipfile.lock
!pyproject.toml
!poetry.lock

# ... and the modules
!app/
Expand Down
15 changes: 0 additions & 15 deletions backend/.flake8

This file was deleted.

1 change: 1 addition & 0 deletions backend/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"**/__pycache__": true,
"**/.mypy_cache": true,
"**/.pytest_cache": true,
"**/.ruff_cache": true,
"**/.venv": true,
},
}
58 changes: 29 additions & 29 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,60 +1,60 @@
FROM python:3.11-slim as builder
FROM python:3.12-slim AS builder

# Setup environment
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
# Set environment variables
ENV POETRY_VERSION="1.8.2"
ENV POETRY_HOME="/opt/poetry"
ENV POETRY_CACHE_DIR="/cache/poetry"
ENV POETRY_VIRTUALENVS_IN_PROJECT=true

# Tell pipenv to create venv in the current directory
ENV PIPENV_VENV_IN_PROJECT=1
# Create cache directory
RUN mkdir -p /cache/poetry

# Install dependencies
RUN apt-get update && apt-get install -y --no-install-recommends gcc
# RUN apt-get update && apt-get install -y --no-install-recommends gcc

# Install pipenv
RUN pip install pipenv
# Install Poetry
RUN pip install poetry==${POETRY_VERSION}

# Copy Pipfile and Pipfile.lock
RUN mkdir -v -p /build
ADD Pipfile Pipfile.lock /build/

# Set working directory
# Set the working directory
WORKDIR /build

# Install packages specified in Pipfile.lock
RUN --mount=type=cache,target=/root/.cache pipenv install --deploy
# Copy the project files
ADD pyproject.toml poetry.lock ./

# Validate the project
RUN poetry check

FROM python:3.11-slim as runner
# Install project dependencies
RUN --mount=type=cache,target=/cache/poetry poetry install --only main --no-root

# Second stage
FROM python:3.12-slim as runner

# Set correct locale
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8

# This prevents Python from writing out pyc files
ENV PYTHONDONTWRITEBYTECODE 1

# This prevents Python from buffering stdout and stderr
ENV PYTHONUNBUFFERED 1

# Enable Python tracebacks on segfaults
ENV PYTHONFAULTHANDLER 1

# Copy venv from builder
RUN mkdir -v -p /app
COPY --from=builder /build/.venv/ /app/.venv
# Copy virtual environment
COPY --from=builder /build/.venv /app/.venv

# Copy source code
# Copy project files
COPY . /app

# Set working directory
WORKDIR /app

# Create user
RUN useradd --system --no-create-home appuser
# Create a non-root user
RUN addgroup --system --gid 1001 appuser
RUN adduser --system --uid 1001 appuser
USER appuser

# Finalize
EXPOSE 5000
USER appuser
ENTRYPOINT ["./.venv/bin/python", "-m", "uvicorn"]
CMD ["app.main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "5000"]
60 changes: 0 additions & 60 deletions backend/GCP.Dockerfile

This file was deleted.

34 changes: 19 additions & 15 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,43 @@
default: dev

install:
pipenv install --dev
poetry self add poetry-plugin-dotenv poetry-plugin-export
poetry config warnings.export false
poetry install

dev:
pipenv run uvicorn app.main:app --reload --port 5000
poetry run uvicorn app.main:app --reload --port 5000

prod:
pipenv run uvicorn app.main:app --host 0.0.0.0 --port 5000
poetry run uvicorn app.main:app --host 0.0.0.0 --port 5000

test:
pipenv run pytest
poetry run pytest

coverage:
pipenv run pytest --cov=. --cov-report term --cov-report xml:coverage.xml

audit:
pipenv check --audit-and-monitor --save-json audit.json
poetry run pytest --cov=. --cov-report term --cov-report xml:coverage.xml

requirements:
pipenv requirements > requirements-cpu.txt
pipenv requirements --dev-only > requirements-dev.txt
poetry lock
poetry export -f requirements.txt --output requirements-cpu.txt --without dev
poetry export -f requirements.txt --output requirements-dev.txt --only dev

lint-check:
poetry run ruff check .
poetry run ruff format .

lint:
ruff check .
ruff format .
poetry run ruff check --fix .
poetry run ruff format .

clean:
rm -rf .ruff_cache
rm -rf .pytest_cache
rm -rf .mypy_cache
rm -rf .coverage
rm -rf coverage.xml
rm -rf audit.json

.PHONY: default install dev prod test coverage audit requirements lint clean
.PHONY: default install dev prod test coverage requirements lint clean

.EXPORT_ALL_VARIABLES:
PIPENV_VERBOSITY = -1
POETRY_VIRTUALENVS_IN_PROJECT = true
33 changes: 0 additions & 33 deletions backend/Pipfile

This file was deleted.

Loading