Skip to content

Commit

Permalink
Rework context metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
KaQuMiQ committed Jan 3, 2025
1 parent c18f245 commit 3fd5972
Show file tree
Hide file tree
Showing 27 changed files with 1,553 additions and 752 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ TESTS_PATH := tests
-include .env

ifndef UV_VERSION
UV_VERSION := 0.5.12
UV_VERSION := 0.5.14
endif

.PHONY: uv_check venv sync update format lint test release
Expand Down
5 changes: 5 additions & 0 deletions examples/fastAPI/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*
!./pyproject.toml
!./config/unit.json
!./src
!./uv.lock
37 changes: 37 additions & 0 deletions examples/fastAPI/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
ARG PYTHON_TAG=3.12
ARG UNIT_TAG=1.32.1-python${PYTHON_TAG}

# SERVER #

FROM unit:${UNIT_TAG} AS server_builder

# copy only the parts needed for production
COPY ./src/integrations ./src/integrations
COPY ./src/solutions ./src/solutions
COPY ./src/features ./src/features
COPY ./src/server ./src/server

# install dependencies and packages
COPY --from=ghcr.io/astral-sh/uv:0.5.14 /uv /uvx /bin/

ENV UV_PROJECT_ENVIRONMENT="/usr/local/"

RUN --mount=type=bind,source=./uv.lock,target=./uv.lock --mount=type=bind,source=./pyproject.toml,target=./pyproject.toml uv sync --python python${PYTHON_TAG} --locked --no-editable --no-python-downloads --link-mode copy --compile-bytecode --only-group server

FROM server_builder AS server

# allow access to home directory for asyncpg library
RUN chgrp -R unit ${HOME} && chmod -R 050 ${HOME}

RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get -y autoremove \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# copy configuration
COPY ./config/unit.json /docker-entrypoint.d/config.json

CMD ["unitd", "--no-daemon", "--log", "/dev/stdout"]

# port 80 is already exposed by nginx unit image, can't change it...
70 changes: 43 additions & 27 deletions examples/fastAPI/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,62 @@ SOURCES_PATH := src
# load environment config from .env if able
-include .env

ifndef PYTHON_ALIAS
PYTHON_ALIAS := python
ifndef UV_VERSION
UV_VERSION := 0.5.14
endif

ifndef INSTALL_OPTIONS
INSTALL_OPTIONS := .[dev]
endif
.PHONY: uv_check venv sync update format lint run docker_run

ifndef UV_VERSION
UV_VERSION := 0.5.6
endif
# Check installed UV version and install if needed
uv_check:
@echo 'Checking uv version...'

.PHONY: venv sync lock update format lint test run
# Install if not present
@if ! command -v uv > /dev/null; then \
echo '...installing uv...'; \
curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh; \
if [ $$? -ne 0 ]; then \
echo "...installing uv failed!"; \
exit 1; \
fi; \
fi

# Check version and update if needed
@if command -v uv > /dev/null; then \
CURRENT_VERSION=$$(uv --version | head -n1 | cut -d" " -f2); \
if [ "$$(printf "%s\n%s" "$(UV_VERSION)" "$$CURRENT_VERSION" | sort -V | head -n1)" != "$(UV_VERSION)" ]; then \
echo '...updating uv...'; \
curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh; \
if [ $$? -ne 0 ]; then \
echo "...updating uv failed!"; \
exit 1; \
fi; \
else \
echo '...uv version is up-to-date!'; \
fi; \
fi

# Setup virtual environment for local development.
venv:
venv: uv_check
@echo '# Preparing development environment...'
@echo '...installing uv...'
@curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh
@echo '...cloning .env...'
@cp -n ./config/.env.example ./.env || :
@echo '...preparing git hooks...'
@cp -n ./config/pre-push ./.git/hooks/pre-push || :
@echo '...preparing venv...'
@$(PYTHON_ALIAS) -m venv .venv --prompt="VENV[DEV]" --clear --upgrade-deps
@. ./.venv/bin/activate && pip install --upgrade pip && uv pip install --editable $(INSTALL_OPTIONS) --constraint constraints
@uv sync --all-groups --all-extras --frozen --reinstall
@echo '...development environment ready! Activate venv using `. ./.venv/bin/activate`.'

# Sync environment with uv based on constraints
sync:
sync: uv_check
@echo '# Synchronizing dependencies...'
@$(if $(findstring $(UV_VERSION), $(shell uv --version | head -n1 | cut -d" " -f2)), , @echo '...updating uv...' && curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh)
@uv pip install --editable $(INSTALL_OPTIONS) --constraint constraints
@echo '...finished!'

# Generate a set of locked dependencies from pyproject.toml
lock:
@echo '# Locking dependencies...'
@uv pip compile pyproject.toml -o constraints --all-extras
@uv sync --all-groups --all-extras --frozen
@echo '...finished!'

# Update and lock dependencies from pyproject.toml
update:
@echo '# Updating dependencies...'
@$(if $(shell printf '%s\n%s\n' "$(UV_VERSION)" "$$(uv --version | head -n1 | cut -d' ' -f2)" | sort -V | head -n1 | grep -q "$(UV_VERSION)"), , @echo '...updating uv...' && curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh)
# @$(if $(findstring $(UV_VERSION), $(shell uv --version | head -n1 | cut -d" " -f2)), , @echo '...updating uv...' && curl -LsSf https://github.com/astral-sh/uv/releases/download/$(UV_VERSION)/uv-installer.sh | sh)
@uv --no-cache pip compile pyproject.toml -o constraints --all-extras --upgrade
@uv pip install --editable $(INSTALL_OPTIONS) --constraint constraints
@uv sync --all-groups --all-extras --upgrade
@echo '...finished!'

# Run formatter.
Expand All @@ -68,3 +80,7 @@ lint:
# Run the server
run:
@python -m server

# Run all services locally using docker-compose.
docker_run:
@docker compose up --force-recreate --build server
11 changes: 11 additions & 0 deletions examples/fastAPI/config/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
PYTHONOPTIMIZE=0
DEBUG_LOGGING=1
# Server
SERVER_PORT=8080
# Postgres
POSTGRES_DATABASE=postgres
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_SSLMODE=disable
50 changes: 50 additions & 0 deletions examples/fastAPI/config/unit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"listeners": {
"*:80": {
"pass": "routes/api"
}
},
"routes": {
"api": [
{
"match": {
"uri": "/health"
},
"action": {
"return": 204
}
},
{
"match": {
"uri": "/*"
},
"action": {
"pass": "applications/server"
}
}
]
},
"applications": {
"server": {
"type": "python",
"protocol": "asgi",
"path": "/",
"module": "server",
"callable": "app",
"processes": 1,
"environment": {
"PYTHONOPTIMIZE": "2",
"HOME": "."
}
}
},
"access_log": {
"path": "/dev/stdout",
"format": "$time_local [ACCESS] $request_line -> $status"
},
"settings": {
"http": {
"server_version": false
}
}
}
71 changes: 0 additions & 71 deletions examples/fastAPI/constraints

This file was deleted.

44 changes: 44 additions & 0 deletions examples/fastAPI/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
services:
server:
depends_on:
postgres:
condition: service_healthy
links:
- postgres
build:
context: .
dockerfile: Dockerfile
target: server
ports:
- ${SERVER_PORT:-8080}:80
restart: no
environment:
PYTHONOPTIMIZE: ${PYTHONOPTIMIZE:-2}
DEBUG_LOGGING: ${DEBUG_LOGGING}
POSTGRES_DATABASE: ${POSTGRES_DATABASE}
POSTGRES_HOST: postgres
POSTGRES_PORT: 5432
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_SSLMODE: ${POSTGRES_SSLMODE:-prefer}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}

postgres:
image: postgres:16.3-alpine3.20
restart: always
environment:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DATABASE}
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- ${POSTGRES_PORT}:5432
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DATABASE}"]
interval: 10s
start_period: 10s
timeout: 10s
retries: 3

volumes:
postgres_data:
10 changes: 7 additions & 3 deletions examples/fastAPI/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ maintainers = [
]
requires-python = ">=3.12"
dependencies = [
"haiway~=0.6.0",
"fastapi-slim~=0.115",
"haiway @ git+https://github.com/miquido/haiway@main",
"asyncpg~=0.30",
"httpx~=0.28.0",
]

[project.urls]
Homepage = "https://miquido.com"

[project.optional-dependencies]
[dependency-groups]
server = [
"haiway_fastapi",
"fastapi-slim~=0.115",
]
dev = [
"haiway_fastapi",
"uvicorn~=0.30",
"ruff~=0.8.0",
"pyright~=1.1",
Expand Down
Loading

0 comments on commit 3fd5972

Please sign in to comment.