-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
105 lines (84 loc) · 3.31 KB
/
Copy pathMakefile
File metadata and controls
105 lines (84 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Makefile for FastAPI project using uv
# Variables
APP_MODULE := app/main.py
HOST := 0.0.0.0
PORT := 8000
DOCKER_SERVICE := web # Replace with your FastAPI service name
DOCKER_COMPOSE_FILE := docker-compose.yaml
PYTEST_ARGS := tests
TARGET := $(or $(TARGET),local) # force default to local
MSG ?= # optional message
# --- FastAPI commands ---
run-dev: ## Start FastAPI dev server with uv (reload, 1 worker, simple)
@echo "Starting FastAPI dev server with uv..."
uv run fastapi dev $(APP_MODULE) --host $(HOST) --port $(PORT)
run-dev-gunicorn: ## Start FastAPI dev server with Gunicorn + Uvicorn (1 worker, reload)
@echo "Starting FastAPI dev server with Gunicorn (1 worker, reload)..."
uv run gunicorn app.main:app \
-k uvicorn.workers.UvicornWorker \
--reload \
-w 1 \
--bind $(HOST):$(PORT)
run-prod: ## Start FastAPI in production with Gunicorn + Uvicorn workers (preload, multi-worker)
@echo "Starting FastAPI in production with Gunicorn..."
uv run gunicorn app.main:app \
-k uvicorn.workers.UvicornWorker \
--preload \
-w 4 \
--bind $(HOST):$(PORT)
# --- Docker Compose commands ---
up: ## Start Docker Compose services
@echo "Starting Docker Compose services..."
docker compose -f $(DOCKER_COMPOSE_FILE) up -d
down: ## Stop Docker Compose services
@echo "Stopping Docker Compose services..."
docker compose -f $(DOCKER_COMPOSE_FILE) down
build: ## Build Docker Compose services
@echo "Building Docker Compose services..."
docker compose -f $(DOCKER_COMPOSE_FILE) build
shell: ## Enter the FastAPI container shell
@echo "Entering $(DOCKER_SERVICE) container..."
docker compose exec $(DOCKER_SERVICE) sh
# --- Alembic ---
# Usage:
# make migrate -> upgrade head locally
# make migrate -t docker -> upgrade head in Docker container
# make migrate -m "message" -> autogenerate revision locally
# make migrate -t docker -m "message" -> revision in Docker
TARGET ?= local
migrate: ## Run Alembic migrations (local or docker)
ifeq ($(TARGET),docker)
$(if $(MSG), \
docker compose exec $(DOCKER_SERVICE) alembic revision --autogenerate -m "$(MSG)", \
docker compose exec $(DOCKER_SERVICE) alembic upgrade head)
else ifeq ($(TARGET),local)
$(if $(MSG), \
alembic revision --autogenerate -m "$(MSG)", \
alembic upgrade head)
else
$(error TARGET must be docker or local)
endif
# --- Tests ---
test: ## Run all tests
uv run pytest $(PYTEST_ARGS)
test-verbose: ## Run all tests in verbose mode
uv run pytest $(PYTEST_ARGS) -v
test-unit: ## Run unit tests only
uv run pytest tests -m unit
test-integration: ## Run integration tests only
uv run pytest tests -m integration
test-e2e: ## Run end-to-end tests only
uv run pytest tests -m e2e
# --- Pre-commit ---
precommit: ## Run pre-commit hooks on all files
@echo "Running pre-commit hooks on all files..."
uv run pre-commit run --all-files
# --- Clean ---
clean: ## Remove cache files
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type d -name "*.pytest_cache" -exec rm -rf {} +
# --- Help ---
help: ## Show available make targets
@echo "Available targets:"
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-18s\033[0m %s\n", $$1, $$2}'
.PHONY: run up down build shell migrate test test-verbose test-unit test-integration test-e2e clean precommit help