update skeleton
This commit is contained in:
@@ -4,7 +4,7 @@ CA_FILE ?=
|
|||||||
ACTION_SCRIPT ?=
|
ACTION_SCRIPT ?=
|
||||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||||
|
|
||||||
TEST_DATABASE_URL ?= $(shell sed -e 's|@postgres:|@localhost:|' -e 's|/messenger$$|/messenger_test|' secrets/database_url 2>/dev/null)
|
TEST_DATABASE_URL ?= $(shell test -f secrets/postgres_password && printf 'postgresql+psycopg://messenger:%s@localhost:5432/messenger_test' "$$(cat secrets/postgres_password)")
|
||||||
export TEST_DATABASE_URL
|
export TEST_DATABASE_URL
|
||||||
|
|
||||||
.PHONY: help check validate test load test-persistence \
|
.PHONY: help check validate test load test-persistence \
|
||||||
@@ -33,21 +33,21 @@ test-persistence:
|
|||||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
secrets:
|
secrets:
|
||||||
sh scripts/init-secrets.sh
|
@mkdir -p secrets
|
||||||
|
@test -f secrets/postgres_password || (head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/postgres_password && chmod 644 secrets/postgres_password && echo "создан secrets/postgres_password")
|
||||||
|
|
||||||
up:
|
up:
|
||||||
docker compose --env-file .env up -d --build
|
docker compose --env-file .env up -d --build
|
||||||
|
|
||||||
down:
|
down:
|
||||||
docker compose down # без -v: volumes с данными сохраняются
|
docker compose down
|
||||||
|
|
||||||
logs:
|
logs:
|
||||||
docker compose logs -f api
|
docker compose logs -f api
|
||||||
|
|
||||||
db-up:
|
db-up:
|
||||||
docker compose up -d --wait postgres
|
docker compose up -d --wait postgres
|
||||||
docker compose exec -T postgres sh -c \
|
docker compose exec -T postgres sh -c 'createdb -U "$$POSTGRES_USER" messenger_test 2>&1 | grep -v "already exists"; true'
|
||||||
"psql -U messenger -d postgres -tc \"SELECT 1 FROM pg_database WHERE datname='messenger_test'\" | grep -q 1 || createdb -U messenger messenger_test"
|
|
||||||
|
|
||||||
migrate:
|
migrate:
|
||||||
docker compose run --rm migrate
|
docker compose run --rm migrate
|
||||||
|
|||||||
+30
-6
@@ -4,6 +4,7 @@ from typing import Literal, Self
|
|||||||
|
|
||||||
from pydantic import Field, model_validator
|
from pydantic import Field, model_validator
|
||||||
from pydantic_settings import BaseSettings, SettingsConfigDict
|
from pydantic_settings import BaseSettings, SettingsConfigDict
|
||||||
|
from sqlalchemy import URL
|
||||||
|
|
||||||
|
|
||||||
class LogLevel(StrEnum):
|
class LogLevel(StrEnum):
|
||||||
@@ -27,7 +28,12 @@ class Settings(BaseSettings):
|
|||||||
session_store: Literal["postgres"] = "postgres"
|
session_store: Literal["postgres"] = "postgres"
|
||||||
|
|
||||||
database_url: str | None = None
|
database_url: str | None = None
|
||||||
database_url_file: Path | None = None
|
db_host: str | None = None
|
||||||
|
db_port: int = Field(default=5432, ge=1, le=65535)
|
||||||
|
db_name: str | None = None
|
||||||
|
db_user: str | None = None
|
||||||
|
db_password: str | None = None
|
||||||
|
db_password_file: Path | None = None
|
||||||
|
|
||||||
db_pool_size: int = Field(default=5, ge=1)
|
db_pool_size: int = Field(default=5, ge=1)
|
||||||
db_max_overflow: int = Field(default=5, ge=0)
|
db_max_overflow: int = Field(default=5, ge=0)
|
||||||
@@ -44,13 +50,31 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
@model_validator(mode="after")
|
@model_validator(mode="after")
|
||||||
def _require_database_source(self) -> Self:
|
def _require_database_source(self) -> Self:
|
||||||
if self.database_url is None and self.database_url_file is None:
|
if self.database_url is not None:
|
||||||
raise ValueError("Задайте DATABASE_URL или DATABASE_URL_FILE")
|
return self
|
||||||
|
has_password = self.db_password is not None or self.db_password_file is not None
|
||||||
|
if not (self.db_host and self.db_name and self.db_user and has_password):
|
||||||
|
raise ValueError(
|
||||||
|
"Задайте DATABASE_URL или DB_HOST, DB_NAME, DB_USER и "
|
||||||
|
"DB_PASSWORD_FILE (либо DB_PASSWORD)"
|
||||||
|
)
|
||||||
return self
|
return self
|
||||||
|
|
||||||
def get_database_url(self) -> str:
|
def get_database_url(self) -> str:
|
||||||
"""Возвращает строку подключения; секрет из файла читается на каждый вызов."""
|
|
||||||
if self.database_url is not None:
|
if self.database_url is not None:
|
||||||
return self.database_url
|
return self.database_url
|
||||||
assert self.database_url_file is not None
|
|
||||||
return self.database_url_file.read_text(encoding="utf-8").strip()
|
if self.db_password_file is not None:
|
||||||
|
password = self.db_password_file.read_text(encoding="utf-8").strip()
|
||||||
|
else:
|
||||||
|
password = self.db_password
|
||||||
|
|
||||||
|
url = URL.create(
|
||||||
|
"postgresql+psycopg",
|
||||||
|
username=self.db_user,
|
||||||
|
password=password,
|
||||||
|
host=self.db_host,
|
||||||
|
port=self.db_port,
|
||||||
|
database=self.db_name,
|
||||||
|
)
|
||||||
|
return url.render_as_string(hide_password=False)
|
||||||
|
|||||||
+5
-7
@@ -3,8 +3,8 @@ services:
|
|||||||
image: postgres:16.10-bookworm
|
image: postgres:16.10-bookworm
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
environment:
|
environment:
|
||||||
POSTGRES_USER: messenger
|
POSTGRES_USER: ${DB_USER}
|
||||||
POSTGRES_DB: messenger
|
POSTGRES_DB: ${DB_NAME}
|
||||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||||
secrets:
|
secrets:
|
||||||
- postgres_password
|
- postgres_password
|
||||||
@@ -13,7 +13,7 @@ services:
|
|||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:5432:5432"
|
- "127.0.0.1:5432:5432"
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
|
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||||
interval: 5s
|
interval: 5s
|
||||||
timeout: 3s
|
timeout: 3s
|
||||||
retries: 10
|
retries: 10
|
||||||
@@ -24,7 +24,7 @@ services:
|
|||||||
command: ["alembic", "upgrade", "head"]
|
command: ["alembic", "upgrade", "head"]
|
||||||
env_file: .env
|
env_file: .env
|
||||||
secrets:
|
secrets:
|
||||||
- database_url
|
- postgres_password
|
||||||
depends_on:
|
depends_on:
|
||||||
postgres:
|
postgres:
|
||||||
condition: service_healthy
|
condition: service_healthy
|
||||||
@@ -36,7 +36,7 @@ services:
|
|||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
env_file: .env
|
env_file: .env
|
||||||
secrets:
|
secrets:
|
||||||
- database_url
|
- postgres_password
|
||||||
ports:
|
ports:
|
||||||
- "127.0.0.1:8080:8080"
|
- "127.0.0.1:8080:8080"
|
||||||
depends_on:
|
depends_on:
|
||||||
@@ -48,8 +48,6 @@ services:
|
|||||||
secrets:
|
secrets:
|
||||||
postgres_password:
|
postgres_password:
|
||||||
file: ./secrets/postgres_password
|
file: ./secrets/postgres_password
|
||||||
database_url:
|
|
||||||
file: ./secrets/database_url
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
pgdata:
|
pgdata:
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
"""${message}
|
|
||||||
|
|
||||||
Revision ID: ${up_revision}
|
|
||||||
Revises: ${down_revision | comma,n}
|
|
||||||
Create Date: ${create_date}
|
|
||||||
"""
|
|
||||||
from typing import Sequence, Union
|
|
||||||
|
|
||||||
from alembic import op
|
|
||||||
import sqlalchemy as sa
|
|
||||||
${imports if imports else ""}
|
|
||||||
|
|
||||||
revision: str = ${repr(up_revision)}
|
|
||||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
|
||||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
|
||||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
|
||||||
|
|
||||||
|
|
||||||
def upgrade() -> None:
|
|
||||||
${upgrades if upgrades else "pass"}
|
|
||||||
|
|
||||||
|
|
||||||
def downgrade() -> None:
|
|
||||||
${downgrades if downgrades else "pass"}
|
|
||||||
+56
-32
@@ -1,5 +1,6 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Offline structural checks; full schema validation is a separate target."""
|
"""Offline structural checks; full schema validation is a separate target."""
|
||||||
|
|
||||||
import ast
|
import ast
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -7,48 +8,71 @@ import re
|
|||||||
|
|
||||||
ROOT = Path(__file__).resolve().parents[1]
|
ROOT = Path(__file__).resolve().parents[1]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
lab = json.loads((ROOT/'lab.json').read_text())['number']
|
lab = json.loads((ROOT / "lab.json").read_text())["number"]
|
||||||
spec = json.loads((ROOT/'contracts/openapi.json').read_text())
|
spec = json.loads((ROOT / "contracts/openapi.json").read_text())
|
||||||
assert spec['openapi']=='3.1.0'
|
assert spec["openapi"] == "3.1.0"
|
||||||
assert spec['info']['version']=='1.%d.0'%lab
|
assert spec["info"]["version"] == "1.%d.0" % lab
|
||||||
operation_ids = set()
|
operation_ids = set()
|
||||||
|
|
||||||
def walk(value):
|
def walk(value):
|
||||||
if isinstance(value,dict):
|
if isinstance(value, dict):
|
||||||
if '$ref' in value:
|
if "$ref" in value:
|
||||||
pointer = value['$ref']
|
pointer = value["$ref"]
|
||||||
assert pointer.startswith('#/'), 'unexpected external reference'
|
assert pointer.startswith("#/"), "unexpected external reference"
|
||||||
target = spec
|
target = spec
|
||||||
for part in pointer[2:].split('/'):
|
for part in pointer[2:].split("/"):
|
||||||
target = target[part.replace('~1','/').replace('~0','~')]
|
target = target[part.replace("~1", "/").replace("~0", "~")]
|
||||||
for v in value.values():
|
for v in value.values():
|
||||||
walk(v)
|
walk(v)
|
||||||
elif isinstance(value,list):
|
elif isinstance(value, list):
|
||||||
for v in value:
|
for v in value:
|
||||||
walk(v)
|
walk(v)
|
||||||
|
|
||||||
walk(spec)
|
walk(spec)
|
||||||
for route,methods in spec['paths'].items():
|
for route, methods in spec["paths"].items():
|
||||||
placeholders = set(re.findall(r'\{([^}]+)\}',route))
|
placeholders = set(re.findall(r"\{([^}]+)\}", route))
|
||||||
for method,op in methods.items():
|
for method, op in methods.items():
|
||||||
assert op['operationId'] not in operation_ids, 'duplicate operationId'
|
assert op["operationId"] not in operation_ids, "duplicate operationId"
|
||||||
operation_ids.add(op['operationId'])
|
operation_ids.add(op["operationId"])
|
||||||
params = {p['name'] for p in op.get('parameters',[]) if p['in']=='path' and p.get('required')}
|
params = {
|
||||||
assert placeholders==params, 'path parameter mismatch: '+route
|
p["name"]
|
||||||
for response in op['responses'].values():
|
for p in op.get("parameters", [])
|
||||||
assert 'X-Request-Id' in response['headers']
|
if p["in"] == "path" and p.get("required")
|
||||||
if lab>=3:
|
}
|
||||||
assert 'X-Instance-Id' in response['headers']
|
assert placeholders == params, "path parameter mismatch: " + route
|
||||||
assert ('/api/v1/auth/sessions' in spec['paths']) == (lab>=2)
|
for response in op["responses"].values():
|
||||||
assert ('/api/v1/attachments/{attachment_id}' in spec['paths']) == (lab>=4)
|
assert "X-Request-Id" in response["headers"]
|
||||||
for file in list((ROOT/'tests').glob('*.py'))+list((ROOT/'scripts').glob('*.py')):
|
if lab >= 3:
|
||||||
ast.parse(file.read_text(),filename=str(file))
|
assert "X-Instance-Id" in response["headers"]
|
||||||
for file in ROOT.rglob('*.json'):
|
assert ("/api/v1/auth/sessions" in spec["paths"]) == (lab >= 2)
|
||||||
if any(part in {'.git','.venv','node_modules','vendor','output'} for part in file.parts):
|
assert ("/api/v1/attachments/{attachment_id}" in spec["paths"]) == (lab >= 4)
|
||||||
|
for file in list((ROOT / "tests").glob("*.py")) + list(
|
||||||
|
(ROOT / "scripts").glob("*.py")
|
||||||
|
):
|
||||||
|
ast.parse(file.read_text(), filename=str(file))
|
||||||
|
for file in ROOT.rglob("*.json"):
|
||||||
|
if any(
|
||||||
|
part in {".git", ".venv", "node_modules", "vendor", "output"}
|
||||||
|
for part in file.parts
|
||||||
|
):
|
||||||
continue
|
continue
|
||||||
json.loads(file.read_text())
|
json.loads(file.read_text())
|
||||||
for required in ('README.md','COURSE.md','CONTRIBUTING.md','REPORT.md','contracts/README.md','tests/smoke.py','tests/README.md'):
|
for required in (
|
||||||
assert (ROOT/required).is_file(), 'missing '+required
|
"README.md",
|
||||||
print('PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.')
|
"COURSE.md",
|
||||||
|
"CONTRIBUTING.md",
|
||||||
|
"REPORT.md",
|
||||||
|
"contracts/README.md",
|
||||||
|
"tests/smoke.py",
|
||||||
|
"tests/README.md",
|
||||||
|
):
|
||||||
|
assert (ROOT / required).is_file(), "missing " + required
|
||||||
|
print(
|
||||||
|
"PASS: offline skeleton structure, local refs and Python syntax; application NOT tested."
|
||||||
|
)
|
||||||
|
|
||||||
if __name__=='__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|||||||
@@ -1,16 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
set -eu
|
|
||||||
cd "$(dirname "$0")/.."
|
|
||||||
mkdir -p secrets
|
|
||||||
|
|
||||||
if [ -f secrets/postgres_password ]; then
|
|
||||||
echo "secrets/ уже существует — оставляю как есть"
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
password="$(head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n')"
|
|
||||||
printf '%s' "$password" > secrets/postgres_password
|
|
||||||
printf 'postgresql+psycopg://messenger:%s@postgres:5432/messenger' "$password" > secrets/database_url
|
|
||||||
|
|
||||||
chmod 644 secrets/postgres_password secrets/database_url
|
|
||||||
echo "OK: secrets/postgres_password и secrets/database_url созданы"
|
|
||||||
@@ -1,23 +1,27 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first."""
|
"""Full OpenAPI and JSON Schema checks; install requirements-dev.txt first."""
|
||||||
|
|
||||||
import json
|
import json
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from openapi_spec_validator import validate
|
from openapi_spec_validator import validate
|
||||||
from jsonschema import Draft202012Validator
|
from jsonschema import Draft202012Validator
|
||||||
|
|
||||||
root = Path(__file__).resolve().parents[1]
|
root = Path(__file__).resolve().parents[1]
|
||||||
spec = json.loads((root/'contracts/openapi.json').read_text())
|
spec = json.loads((root / "contracts/openapi.json").read_text())
|
||||||
validate(spec)
|
validate(spec)
|
||||||
from referencing import Registry, Resource
|
from referencing import Registry, Resource
|
||||||
|
|
||||||
# The base URI resolves local #/components references inside examples.
|
# The base URI resolves local #/components references inside examples.
|
||||||
resource = dict(spec, **{'$schema': 'https://json-schema.org/draft/2020-12/schema'})
|
resource = dict(spec, **{"$schema": "https://json-schema.org/draft/2020-12/schema"})
|
||||||
registry = Registry().with_resource('urn:mtusi:openapi', Resource.from_contents(resource))
|
registry = Registry().with_resource(
|
||||||
for name, example in json.loads((root/'contracts/examples.json').read_text()).items():
|
"urn:mtusi:openapi", Resource.from_contents(resource)
|
||||||
schema = {'$ref': 'urn:mtusi:openapi#/components/schemas/' + name}
|
)
|
||||||
|
for name, example in json.loads((root / "contracts/examples.json").read_text()).items():
|
||||||
|
schema = {"$ref": "urn:mtusi:openapi#/components/schemas/" + name}
|
||||||
Draft202012Validator(schema, registry=registry).validate(example)
|
Draft202012Validator(schema, registry=registry).validate(example)
|
||||||
for path in (root/'contracts').glob('*.schema.json'):
|
for path in (root / "contracts").glob("*.schema.json"):
|
||||||
schema = json.loads(path.read_text())
|
schema = json.loads(path.read_text())
|
||||||
Draft202012Validator.check_schema(schema)
|
Draft202012Validator.check_schema(schema)
|
||||||
for example in schema.get('examples',[]):
|
for example in schema.get("examples", []):
|
||||||
Draft202012Validator(schema).validate(example)
|
Draft202012Validator(schema).validate(example)
|
||||||
print('PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.')
|
print("PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.")
|
||||||
|
|||||||
Reference in New Issue
Block a user