Skip to content

Commit

Permalink
feat: now using docker for dev with dedicated makefile (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
TeKrop committed Mar 29, 2024
1 parent 2d87cda commit 4f1ffd4
Show file tree
Hide file tree
Showing 75 changed files with 756 additions and 662 deletions.
3 changes: 1 addition & 2 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Container settings
APP_VOLUME_PATH=/my/custom/path/for/app
APP_VOLUME_PATH=
APP_PORT=80

# Application settings
Expand All @@ -14,7 +14,6 @@ REDIS_HOST=redis
REDIS_PORT=6379

# Cache configuration
USE_API_CACHE_IN_APP=false
EXPIRED_CACHE_REFRESH_LIMIT=3600
HEROES_PATH_CACHE_TIMEOUT=86400
HERO_PATH_CACHE_TIMEOUT=86400
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
strategy:
matrix:
python-version: ["3.12"]
poetry-version: [1.6.1]
poetry-version: [1.8.2]

steps:
- name: Build project
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ __pycache__/

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
Expand Down
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ default_language_version:
python: python3.12
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.1.3
rev: v0.3.4
hooks:
- id: ruff
name: (ruff) Linting and fixing code
args: [--fix, --exit-non-zero-on-fix]
- id: ruff-format
name: (ruff) Formatting code
- repo: https://github.com/sourcery-ai/sourcery
rev: v1.11.0
rev: v1.16.0
hooks:
- id: sourcery
name: (sourcery) Checking code quality
Expand Down
21 changes: 14 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
FROM python:3.12-alpine
FROM python:3.12-alpine AS main

WORKDIR /code

# Environment variables
ENV PYTHONFAULTHANDLER=1 \
Expand All @@ -7,16 +9,15 @@ ENV PYTHONFAULTHANDLER=1 \
PIP_NO_CACHE_DIR=off \
PIP_DISABLE_PIP_VERSION_CHECK=on \
PIP_DEFAULT_TIMEOUT=100 \
POETRY_VERSION=1.6.1
POETRY_VERSION=1.8.2

# Install required system packages and install poetry
RUN apk add build-base && \
apk add libffi-dev && \
pip install poetry==1.6.1
pip install poetry==1.8.2

# Copy only requirements (caching in Docker layer)
WORKDIR /code
COPY pyproject.toml scripts/app-start.sh /code/
COPY pyproject.toml /code/

# Install dependencies
RUN poetry config virtualenvs.create false && \
Expand All @@ -26,5 +27,11 @@ RUN poetry config virtualenvs.create false && \
COPY ./app /code/app
COPY ./static /code/static

# Configure the command
CMD ["sh", "/code/app-start.sh"]
# Copy crontabs file and make it executable
COPY ./build/overfast-crontab /etc/crontabs/root
RUN chmod +x /etc/crontabs/root

# For dev image, copy the tests and install necessary dependencies
FROM main as dev
RUN poetry install --only dev --no-interaction --no-ansi
COPY ./tests /code/tests
132 changes: 76 additions & 56 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,74 +1,94 @@
# Default target
.DEFAULT_GOAL := help

# Help message
# Colors
YELLOW := \033[1;33m
GREEN := \033[1;32m
CYAN := \033[1;36m
RESET := \033[0m

# Aliases
DOCKER_RUN := docker compose run \
--volume ${PWD}/app:/code/app \
--volume ${PWD}/tests:/code/tests \
--volume ${PWD}/htmlcov:/code/htmlcov \
--volume ${PWD}/logs:/code/logs \
--publish 8000:8000 \
--rm \
app

help:
@echo "============================================================================="
@echo " OverFast API Makefile Help"
@echo "============================================================================="
@echo "Available targets:"
@echo ""
@echo " make install"
@echo " Install dependencies."
@echo ""
@echo " make run"
@echo " Run OverFastAPI application."
@echo ""
@echo " make test [TARGET=path/to/tests] [COVERAGE=true]"
@echo " Run tests with optional coverage calculation and target specification."
@echo "Usage: make <command>"
@echo ""
@echo " make lint"
@echo " Run linter."
@echo ""
@echo " make format"
@echo " Run formatter."
@echo ""
@echo " make docker-up"
@echo " Build and run the project using Docker Compose."
@echo ""
@echo " make clean"
@echo " Remove generated files."
@echo "============================================================================="
@echo "${CYAN}Commands:${RESET}"
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "build" "Build project images (dev mode)."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "start" "Run OverFastAPI application (dev mode)."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "lint" "Run linter."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "format" "Run formatter."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "shell" "Access an interactive shell inside the app container"
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "exec" "Execute a given COMMAND inside the app container"
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "test" "Run tests. PYTEST_ARGS can be specified."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "up" "Build & run OverFastAPI application (production mode)."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "down" "Stop the app and remove containers."
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "clean" "Clean up Docker environment"
@printf " ${GREEN}%-10s${RESET} : ${YELLOW}%s${RESET}\n" "help" "Show this help message"

# Install dependencies
install:
@echo "Installing dependencies..."
poetry lock --no-update
poetry install
# Build project images
build:
@echo "Building OverFastAPI (dev mode)..."
BUILD_TARGET="dev" docker compose build

# Run OverFastAPI application
run:
@echo "Launching OverFastAPI..."
uvicorn app.main:app --reload

# Run tests with coverage (optional)
test:
ifdef COVERAGE
@echo "Running tests with coverage..."
python -m pytest --cov app --cov-report html -n auto $(TARGET)
else
@echo "Running tests..."
python -m pytest -n auto $(TARGET)
endif
# Run OverFastAPI application (dev mode)
start:
@echo "Launching OverFastAPI (dev mode)..."
$(DOCKER_RUN) uvicorn app.main:app --reload --host 0.0.0.0

# Run linter
lint:
@echo "Running linter..."
ruff check --fix --exit-non-zero-on-fix
$(DOCKER_RUN) ruff check --fix --exit-non-zero-on-fix

# Run formatter
format:
@echo "Running formatter..."
ruff format
$(DOCKER_RUN) ruff format

# Open a shell on the app container
shell:
@echo "Running shell on app container..."
$(DOCKER_RUN) /bin/sh

# Execute a command on the app container
exec:
@echo "Running shell on app container..."
$(DOCKER_RUN) $(COMMAND)

# Run tests (no coverage calculation if a target is specified)
test: build
ifdef PYTEST_ARGS
@echo "Running tests on $(PYTEST_ARGS)..."
$(DOCKER_RUN) python -m pytest $(PYTEST_ARGS)
else
@echo "Running all tests with coverage..."
$(DOCKER_RUN) python -m pytest --cov app --cov-report html -n auto
endif

# Run OverFastAPI application (production mode)
up: down
@echo "Building OverFastAPI (production mode)..."
docker compose build
@echo "Launching OverFastAPI (production mode)..."
docker compose up -d

# Build and run the project using Docker Compose
docker-up:
@echo "Building and running the project using Docker Compose..."
docker compose up --build -d
# Remove containers
down:
@echo "Stopping OverFastAPI and cleaning containers..."
docker compose down -v --remove-orphans

# Clean up generated files
clean:
@echo "Cleaning up..."
rm -rf __pycache__ .pytest_cache htmlcov
# Clean Docker environment
clean: down
@echo "Cleaning Docker environment..."
docker image prune -af
docker network prune -f

.PHONY: help install run test lint format docker-up clean
.PHONY: help build start lint format shell exec test up down clean
Loading

0 comments on commit 4f1ffd4

Please sign in to comment.