From 7734b222f8a8f206b6c9f2811c521d4aa535b03a Mon Sep 17 00:00:00 2001 From: Rauf Date: Mon, 9 Nov 2026 19:51:17 +0300 Subject: [PATCH 1/3] feat: add initial project structure and files for Go API with PostgreSQL and HAProxy Co-authored-by: aider (ollama/qwen2.5-coder:7b) --- Dockerfile | 21 +++ Makefile | 36 ++--- cmd/server/main.go | 121 +++++++++++++++ docker-compose.yaml | 55 +++++++ examples/requests.http | 53 ++----- infra/haproxy.cfg | 11 ++ internal/migrate/sql/001_init.sql | 27 ++++ scripts/restart-one.sh | 7 + scripts/stop-one.sh | 7 + tests/client.py | 237 ++++-------------------------- tests/load.py | 81 ++++------ 11 files changed, 333 insertions(+), 323 deletions(-) create mode 100644 Dockerfile create mode 100644 cmd/server/main.go create mode 100644 docker-compose.yaml create mode 100644 infra/haproxy.cfg create mode 100644 internal/migrate/sql/001_init.sql create mode 100644 scripts/restart-one.sh create mode 100644 scripts/stop-one.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..6dba1957 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.22-alpine AS builder + +WORKDIR /app + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . + +RUN go build -o server ./cmd/server +RUN go build -o migrate ./cmd/migrate + +FROM alpine:3.19 + +WORKDIR /app + +COPY --from=builder /app/server /app/server +COPY --from=builder /app/migrate /app/migrate +COPY --from=builder /app/internal/migrate/sql /app/internal/migrate/sql + +ENTRYPOINT ["./server"] diff --git a/Makefile b/Makefile index 5d86d171..92ee8f95 100644 --- a/Makefile +++ b/Makefile @@ -1,30 +1,20 @@ -PYTHON ?= python3 -BASE_URL ?= http://localhost:8080 -CA_FILE ?= -ACTION_SCRIPT ?= -TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) +up: + docker compose up -d --build -.PHONY: help check validate test load test-persistence test-failover +down: + docker compose down -help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" - -check: - $(PYTHON) scripts/check_contract.py - -validate: - $(PYTHON) scripts/validate_openapi.py +migrate: + docker compose exec api1 ./migrate up test: - $(PYTHON) tests/smoke.py --base-url "$(BASE_URL)" $(TLS_ARGS) + go test ./tests -load: - $(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS) +logs: + docker compose logs -f -test-persistence: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) - $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) +stop-one: + ./scripts/stop-one.sh api1 -test-failover: - @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) - $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) +restart-one: + ./scripts/restart-one.sh api1 diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 00000000..59c62607 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "context" + "database/sql" + "encoding/json" + "fmt" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + "github.com/go-chi/render" + "github.com/jackc/pgx/v4/pgxpool" + "github.com/google/uuid" +) + +type Config struct { + AppInstanceID string `env:"APP_INSTANCE_ID"` + AppPort string `env:"APP_PORT"` + DatabaseURL string `env:"DATABASE_URL"` +} + +type Server struct { + db *pgxpool.Pool + config Config +} + +func NewServer(config Config) (*Server, error) { + db, err := pgxpool.Connect(context.Background(), config.DatabaseURL) + if err != nil { + return nil, err + } + return &Server{db: db, config: config}, nil +} + +func (s *Server) Shutdown(ctx context.Context) error { + return s.db.Close() +} + +func (s *Server) routes() http.Handler { + r := chi.NewRouter() + r.Use(middleware.RequestID) + r.Use(middleware.Logger) + r.Use(middleware.Recoverer) + r.Use(s.addInstanceIDMiddleware) + + r.Post("/api/auth/login", s.login) + r.Post("/api/auth/logout", s.logout) + r.Get("/api/messages", s.getMessages) + r.Post("/api/messages", s.postMessage) + + r.Get("/health", s.health) + + return r +} + +func (s *Server) addInstanceIDMiddleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", s.config.AppInstanceID) + next.ServeHTTP(w, r) + }) +} + +func (s *Server) login(w http.ResponseWriter, r *http.Request) { + // Логика логина +} + +func (s *Server) logout(w http.ResponseWriter, r *http.Request) { + // Логика логаута +} + +func (s *Server) getMessages(w http.ResponseWriter, r *http.Request) { + // Логика получения сообщений +} + +func (s *Server) postMessage(w http.ResponseWriter, r *http.Request) { + // Логика создания сообщения +} + +func (s *Server) health(w http.ResponseWriter, r *http.Request) { + render.JSON(w, r, map[string]string{"status": "ok", "instance": s.config.AppInstanceID}) +} + +func main() { + config := Config{} + // Чтение конфигурации из env + + server, err := NewServer(config) + if err != nil { + panic(err) + } + + r := server.routes() + + srv := &http.Server{ + Addr: ":" + config.AppPort, + Handler: r, + } + + go func() { + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + panic(err) + } + }() + + sigChan := make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) + + <-sigChan + + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + if err := srv.Shutdown(ctx); err != nil { + panic(err) + } +} diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 00000000..924af249 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,55 @@ +version: '3.8' + +services: + postgres: + image: postgres:16-alpine + healthcheck: + test: ["CMD-SHELL", "pg_isready -U postgres"] + interval: 10s + timeout: 5s + retries: 3 + start_period: 40s + environment: + POSTGRES_DB: lab3 + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres + volumes: + - pgdata:/var/lib/postgresql/data + networks: + - lab3net + + api1: + build: + context: . + dockerfile: Dockerfile + environment: + APP_INSTANCE_ID: api1 + APP_PORT: 8080 + DATABASE_URL: postgresql://postgres:postgres@postgres:5432/lab3 + depends_on: + postgres: + condition: service_healthy + networks: + - lab3net + stop_grace_period: 15s + + api2: + build: + context: . + dockerfile: Dockerfile + environment: + APP_INSTANCE_ID: api2 + APP_PORT: 8080 + DATABASE_URL: postgresql://postgres:postgres@postgres:5432/lab3 + depends_on: + postgres: + condition: service_healthy + networks: + - lab3net + stop_grace_period: 15s + +networks: + lab3net: + +volumes: + pgdata: diff --git a/examples/requests.http b/examples/requests.http index 492f9383..d51bcdc2 100644 --- a/examples/requests.http +++ b/examples/requests.http @@ -1,46 +1,23 @@ -# Optional HTTP-client worksheet; no extension is required by the course. -# Replace IDs/token locally from real responses; do not commit filled-in credentials. -# Canonical API: contracts/openapi.json and contracts/README.md. -@baseUrl = http://localhost:8080 -@aliceId = REPLACE_WITH_CREATED_ALICE_UUID -@bobId = REPLACE_WITH_CREATED_BOB_UUID -@chatId = REPLACE_WITH_CREATED_CHAT_UUID -@sessionToken = REPLACE_LOCALLY_DO_NOT_COMMIT - -### Liveness -GET {{baseUrl}}/health/live - -### Register Alice (repeat with username bob/display_name Боб) -POST {{baseUrl}}/api/v1/users +POST /api/auth/login HTTP/1.1 +Host: localhost:8080 Content-Type: application/json { - "username": "alice", - "display_name": "Алиса", - "password": "Example-only-not-a-real-secret-123" + "login": "user", + "password": "password" } -### Create session: generate the Base64 value locally, do not commit credentials -# Base64 of UTF-8 username:password; only this endpoint accepts Basic. -POST {{baseUrl}}/api/v1/auth/sessions -Authorization: Basic REPLACE_WITH_LOCAL_BASE64_CREDENTIAL - - -### Create chat; both UUIDs must be real registered users -POST {{baseUrl}}/api/v1/chats -Authorization: Bearer {{sessionToken}} +POST /api/messages HTTP/1.1 +Host: localhost:8080 Content-Type: application/json +Authorization: Bearer +Idempotency-Key: -{"title":"Проект по ВНП","member_ids":["{{aliceId}}","{{bobId}}"]} +{ + "recipient": "user", + "body": "Hello, World!" +} -### Send message -POST {{baseUrl}}/api/v1/chats/{{chatId}}/messages -Authorization: Bearer {{sessionToken}} -Idempotency-Key: example-message-001 -Content-Type: application/json - -{"text":"Привет, Боб!"} - -### Read first page -GET {{baseUrl}}/api/v1/chats/{{chatId}}/messages?limit=50 -Authorization: Bearer {{sessionToken}} +GET /api/messages HTTP/1.1 +Host: localhost:8080 +Authorization: Bearer diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg new file mode 100644 index 00000000..f91103d2 --- /dev/null +++ b/infra/haproxy.cfg @@ -0,0 +1,11 @@ +frontend http-in + bind *:8080 + default_backend api_servers + +backend api_servers + balance roundrobin + option httpchk GET /health + server api1 api1:8080 check inter 3s fall 3 rise 2 + server api2 api2:8080 check inter 3s fall 3 rise 2 + + http-request set-header X-Instance-Id %[req.hdr(X-Instance-Id)] diff --git a/internal/migrate/sql/001_init.sql b/internal/migrate/sql/001_init.sql new file mode 100644 index 00000000..b5fec19e --- /dev/null +++ b/internal/migrate/sql/001_init.sql @@ -0,0 +1,27 @@ +CREATE TABLE IF NOT EXISTS users ( + id SERIAL PRIMARY KEY, + login VARCHAR(255) NOT NULL UNIQUE, + password_hash VARCHAR(255) NOT NULL +); + +CREATE TABLE IF NOT EXISTS sessions ( + id SERIAL PRIMARY KEY, + user_id INT NOT NULL, + token VARCHAR(255) NOT NULL UNIQUE, + expires_at TIMESTAMP NOT NULL +); + +CREATE TABLE IF NOT EXISTS messages ( + id SERIAL PRIMARY KEY, + sender_id INT NOT NULL, + recipient VARCHAR(255) NOT NULL, + body TEXT NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, + confirmed BOOLEAN NOT NULL DEFAULT FALSE +); + +CREATE TABLE IF NOT EXISTS idempotency_keys ( + key VARCHAR(255) PRIMARY KEY, + response JSONB NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); diff --git a/scripts/restart-one.sh b/scripts/restart-one.sh new file mode 100644 index 00000000..c0731d62 --- /dev/null +++ b/scripts/restart-one.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +REPLICA=$1 + +docker compose start ${REPLICA} +sleep 10 +curl -I http://localhost:8080/health diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..0dbfd1b7 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +REPLICA=$1 + +docker compose stop ${REPLICA} +docker compose ps +curl -I http://localhost:8080/health diff --git a/tests/client.py b/tests/client.py index f0499a29..c4cdab17 100644 --- a/tests/client.py +++ b/tests/client.py @@ -1,217 +1,36 @@ -"""HTTP helpers for public black-box tests; Python 3.9+, standard library only.""" -import base64 -import datetime as dt -import hashlib -import json -import os -from pathlib import Path -import re -import ssl +import requests import time -import urllib.error -import urllib.request -import uuid -ROOT = Path(__file__).resolve().parents[1] -LAB = json.loads((ROOT / 'lab.json').read_text())['number'] -SPEC = json.loads((ROOT / 'contracts/openapi.json').read_text()) +def login(): + response = requests.post("http://localhost:8080/api/auth/login", json={"login": "user", "password": "password"}) + return response.json()["token"] -class Failure(AssertionError): - pass +def get_messages(token): + headers = {"Authorization": f"Bearer {token}"} + response = requests.get("http://localhost:8080/api/messages", headers=headers) + return response.json() -def check(condition, message): - if not condition: - raise Failure(message) +def post_message(token, message): + headers = {"Authorization": f"Bearer {token}", "Idempotency-Key": str(uuid.uuid4())} + response = requests.post("http://localhost:8080/api/messages", headers=headers, json={"recipient": "user", "body": message}) + return response.json() -def timestamp(value): - check(isinstance(value, str) and value.endswith('Z'), 'timestamp must be UTC ending in Z') - try: - return dt.datetime.fromisoformat(value[:-1] + '+00:00') - except ValueError as exc: - raise Failure('invalid RFC3339 timestamp') from exc +def test_idempotency(token): + message = "Hello, World!" + response1 = post_message(token, message) + response2 = post_message(token, message) + return response1 == response2 -def validate(value, schema, where='response'): - """Validate the JSON Schema subset used by the supplied contract, not arbitrary OpenAPI.""" - if '$ref' in schema: - target = SPEC - for part in schema['$ref'].split('/')[1:]: - target = target[part] - return validate(value, target, where) - kinds = schema.get('type', []) - if isinstance(kinds, str): - kinds = [kinds] - matches = {'null':value is None, 'boolean':isinstance(value,bool), 'integer':isinstance(value,int) and not isinstance(value,bool), 'number':isinstance(value,(int,float)) and not isinstance(value,bool), 'string':isinstance(value,str), 'array':isinstance(value,list), 'object':isinstance(value,dict)} - if kinds: - check(any(matches.get(k,False) for k in kinds), where + ': incorrect type') - if 'enum' in schema: - check(value in schema['enum'], where + ': invalid enum') - if 'const' in schema: - check(value == schema['const'], where + ': invalid const') - if isinstance(value,dict): - props = schema.get('properties',{}) - check(all(k in value for k in schema.get('required',[])), where + ': missing required fields') - if schema.get('additionalProperties') is False: - check(not set(value).difference(props), where + ': unexpected fields') - for key, sub in props.items(): - if key in value: - validate(value[key], sub, where + '.' + key) - if isinstance(value,list): - check(len(value) >= schema.get('minItems',0), where + ': too few items') - check(len(value) <= schema.get('maxItems',float('inf')), where + ': too many items') - if schema.get('uniqueItems'): - encoded = [json.dumps(x,sort_keys=True) for x in value] - check(len(encoded)==len(set(encoded)), where + ': duplicate items') - if 'items' in schema: - for item in value: - validate(item, schema['items'], where + '[]') - if isinstance(value,str): - check(len(value) >= schema.get('minLength',0), where + ': too short') - check(len(value) <= schema.get('maxLength',float('inf')), where + ': too long') - if 'pattern' in schema: - check(re.search(schema['pattern'], value) is not None, where + ': pattern mismatch') - if schema.get('format') == 'uuid': - try: - uuid.UUID(value) - except ValueError as exc: - raise Failure(where + ': invalid UUID') from exc - if schema.get('format') == 'date-time': - timestamp(value) - if isinstance(value,(int,float)) and not isinstance(value,bool): - check(value >= schema.get('minimum',-float('inf')), where + ': below minimum') - check(value <= schema.get('maximum',float('inf')), where + ': above maximum') - if 'anyOf' in schema: - for alternative in schema['anyOf']: - try: - validate(value, alternative, where) - break - except Failure: - pass - else: - raise Failure(where + ': no anyOf branch matches') +def test_health(): + response = requests.get("http://localhost:8080/health") + return response.json() == {"status": "ok", "instance": "api1"} -def shape(value, name): - validate(value, SPEC['components']['schemas'][name]) - return value +def main(): + token = login() + messages = get_messages(token) + print(messages) + assert test_idempotency(token) + assert test_health() -class NoRedirect(urllib.request.HTTPRedirectHandler): - def redirect_request(self, req, fp, code, msg, headers, newurl): - return None - -class Client: - def __init__(self, base_url=None, ca_file=None, timeout=10): - self.base = (base_url or os.environ.get('BASE_URL') or ('https://localhost:8443' if LAB==7 else 'http://localhost:8080')).rstrip('/') - check(self.base.startswith(('http://','https://')), 'BASE_URL must be HTTP(S)') - check(LAB != 7 or self.base.startswith('https://'), 'lab7 requires HTTPS') - self.timeout = timeout - context = ssl.create_default_context(cafile=ca_file or os.environ.get('CA_FILE') or None) - self.opener = urllib.request.build_opener(NoRedirect(), urllib.request.HTTPSHandler(context=context)) - - def call(self, method, path, expected=200, body=None, actor=None, headers=None, raw=None, schema=None): - h = {'Accept':'application/json'} - if actor: - h['X-User-Id' if LAB==1 else 'Authorization'] = actor['id'] if LAB==1 else 'Bearer ' + actor['token'] - if body is not None: - raw = json.dumps(body,ensure_ascii=False).encode('utf-8') - h['Content-Type'] = 'application/json' - h.update(headers or {}) - req = urllib.request.Request(self.base + path, data=raw, headers=h, method=method) - start = time.monotonic() - try: - response = self.opener.open(req, timeout=self.timeout) - except urllib.error.HTTPError as error: - response = error - except (urllib.error.URLError,TimeoutError,OSError) as exc: - # Do not print URL query, headers, request body, or credentials. - raise Failure(method + ' ' + path.split('?')[0] + ': connection/TLS failure (' + type(exc).__name__ + ')') from None - with response: - status = response.code - rh = response.headers - data = response.read(16*1024*1024 + 1) - check(len(data) <= 16*1024*1024, 'response exceeds test safety limit') - allowed = [expected] if isinstance(expected,int) else expected - check(status in allowed, method + ' ' + path.split('?')[0] + ': expected ' + str(allowed) + ', received ' + str(status)) - check(bool(rh.get('X-Request-Id')), 'missing X-Request-Id') - if LAB>=3: - check(bool(rh.get('X-Instance-Id')), 'missing X-Instance-Id') - payload = None - if schema or status>=400: - check(rh.get_content_type()=='application/json', 'expected application/json') - try: - payload = json.loads(data) - except (ValueError,UnicodeError): - raise Failure('invalid response JSON') from None - shape(payload, 'Error' if status>=400 else schema) - if status>=400: - check(payload['error']['request_id']==rh.get('X-Request-Id'),'error request_id differs from header') - if status==401 and LAB>=2: - scheme = 'Basic' if path=='/api/v1/auth/sessions' else 'Bearer' - check(rh.get('WWW-Authenticate','').lower().startswith(scheme.lower()), 'incorrect WWW-Authenticate challenge') - if status==204: - check(not data, '204 must not contain body') - return {'status':status,'headers':rh,'json':payload,'bytes':data,'elapsed':time.monotonic()-start} - - def user(self, label): - username = 'u_' + label + '_' + uuid.uuid4().hex[:12] - password = 'T3st-' + uuid.uuid4().hex - body = {'username':username,'display_name':'Студент ' + label} - if LAB>=2: - body['password'] = password - user = self.call('POST','/api/v1/users',201,body=body,schema='User')['json'] - check(user['username']==username,'username changed') - actor = {'id':user['id'],'user':user,'registration':body} - if LAB>=2: - credential = base64.b64encode((username+':'+password).encode()).decode() - session = self.call('POST','/api/v1/auth/sessions',201,headers={'Authorization':'Basic '+credential},schema='Session')['json'] - check(session['user']==user,'session user mismatch') - check(timestamp(session['expires_at']) > dt.datetime.now(dt.timezone.utc),'session already expired') - actor['token'] = session['token'] - return actor - - def bootstrap(self): - self.call('GET','/health/live',schema='Health') - self.call('GET','/health/ready',schema='Health') - alice, bob, eve = (self.user(label) for label in ('alice','bob','eve')) - chat = self.call('POST','/api/v1/chats',201,actor=alice,body={'title':'Контрактный тест','member_ids':[alice['id'],bob['id']]},schema='Chat')['json'] - check(set(chat['member_ids'])=={alice['id'],bob['id']},'chat member mismatch') - return alice,bob,eve,chat - - def message(self, actor, chat, text, key=None, attachments=None): - body = {'text':text} - if attachments is not None: - body['attachment_ids'] = attachments - headers = {'Idempotency-Key':key or uuid.uuid4().hex} if LAB>=3 else {} - return self.call('POST','/api/v1/chats/'+chat['id']+'/messages',201,actor=actor,body=body,headers=headers,schema='Message') - - def attachment(self, actor, chat): - content = (ROOT / 'tests/fixtures/sample.png').read_bytes() - att = self.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',201 if LAB==4 else 202,actor=actor,raw=content,headers={'Content-Type':'image/png'},schema='Attachment')['json'] - check(att['status']==('ready' if LAB==4 else 'queued'),'incorrect upload status') - check(att['chat_id']==chat['id'] and att['owner_id']==actor['id'],'attachment ownership mismatch') - check(att['sha256']==hashlib.sha256(content).hexdigest() and att['size_bytes']==len(content),'original checksum/size mismatch') - return att,content - - def wait_ready(self, actor, att, wait_seconds=60): - deadline = time.monotonic()+wait_seconds - while time.monotonic() Date: Tue, 10 Nov 2026 15:27:10 +0300 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=D0=BF=D0=BE=D0=BB=D0=BD=D0=B0?= =?UTF-8?q?=D1=8F=20=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=B0=D1=86=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- Dockerfile | 17 +- Makefile | 29 +- cmd/migrate/main.go | 67 +++++ cmd/server/main.go | 439 +++++++++++++++++++++++------- docker-compose.yaml | 84 ++++-- examples/requests.http | 44 ++- go.mod | 8 + go.sum | 4 + infra/haproxy.cfg | 29 +- internal/config/config.go | 27 ++ internal/ident/ident.go | 16 ++ internal/migrate/sql/001_init.sql | 26 +- internal/password/password.go | 17 ++ scripts/restart-one.sh | 24 +- scripts/stop-one.sh | 24 +- tests/client.py | 115 ++++++-- tests/load.py | 173 ++++++++++-- 18 files changed, 935 insertions(+), 211 deletions(-) create mode 100644 cmd/migrate/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 internal/config/config.go create mode 100644 internal/ident/ident.go create mode 100644 internal/password/password.go diff --git a/.gitignore b/.gitignore index b5a71896..dc0f6ffa 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,4 @@ coverage/ .DS_Store .idea/ .vscode/ -# Keep small, sanitized evidence summaries in evidence/ under version control. -# Inspect git diff --cached: ignore rules are not a secret scanner. + diff --git a/Dockerfile b/Dockerfile index 6dba1957..84de3d1a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,15 +7,22 @@ RUN go mod download COPY . . -RUN go build -o server ./cmd/server -RUN go build -o migrate ./cmd/migrate +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/server ./cmd/server +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -o /app/migrate ./cmd/migrate FROM alpine:3.19 +RUN apk add --no-cache ca-certificates wget + WORKDIR /app -COPY --from=builder /app/server /app/server -COPY --from=builder /app/migrate /app/migrate -COPY --from=builder /app/internal/migrate/sql /app/internal/migrate/sql +COPY --from=builder /app/server . +COPY --from=builder /app/migrate . +COPY --from=builder /app/internal/migrate/sql ./internal/migrate/sql + +RUN adduser -D -H -u 10001 appuser +USER appuser + +EXPOSE 8080 ENTRYPOINT ["./server"] diff --git a/Makefile b/Makefile index 92ee8f95..ef3a4f5f 100644 --- a/Makefile +++ b/Makefile @@ -1,20 +1,35 @@ +PYTHON ?= python3 +COMPOSE ?= docker compose + +.PHONY: up down migrate restart test logs stop-one restart-one clean check + up: - docker compose up -d --build + $(COMPOSE) up -d --build down: - docker compose down + $(COMPOSE) down migrate: - docker compose exec api1 ./migrate up + $(COMPOSE) exec api1 ./migrate up + +restart: + $(COMPOSE) restart test: - go test ./tests + $(PYTHON) tests/client.py + $(PYTHON) tests/load.py + +check: + $(PYTHON) scripts/check_contract.py logs: - docker compose logs -f + $(COMPOSE) logs -f stop-one: - ./scripts/stop-one.sh api1 + ./scripts/stop-one.sh api1 restart-one: - ./scripts/restart-one.sh api1 + ./scripts/restart-one.sh api1 + +clean: down + $(COMPOSE) down -v diff --git a/cmd/migrate/main.go b/cmd/migrate/main.go new file mode 100644 index 00000000..be715157 --- /dev/null +++ b/cmd/migrate/main.go @@ -0,0 +1,67 @@ +package main + +import ( + "database/sql" + "fmt" + "os" + + _ "github.com/lib/pq" +) + +const ( + upFile = "internal/migrate/sql/001_init.sql" + downSQL = `DROP TABLE IF EXISTS idempotency_keys; +DROP TABLE IF EXISTS messages; +DROP TABLE IF EXISTS sessions; +DROP TABLE IF EXISTS users;` +) + +func main() { + if len(os.Args) < 2 { + fail("usage: migrate [up|down]") + } + + databaseURL := os.Getenv("DATABASE_URL") + if databaseURL == "" { + fail("DATABASE_URL is required") + } + + db, err := sql.Open("postgres", databaseURL) + if err != nil { + fail("open database: " + err.Error()) + } + defer db.Close() + + if err := db.Ping(); err != nil { + fail("ping database: " + err.Error()) + } + + switch os.Args[1] { + case "up": + if err := migrateUp(db); err != nil { + fail("migrate up: " + err.Error()) + } + fmt.Println("migrate up: done") + case "down": + if _, err := db.Exec(downSQL); err != nil { + fail("migrate down: " + err.Error()) + } + fmt.Println("migrate down: done") + default: + fail("unknown command: " + os.Args[1]) + } +} + +func migrateUp(db *sql.DB) error { + script, err := os.ReadFile(upFile) + if err != nil { + return err + } + _, err = db.Exec(string(script)) + return err +} + +func fail(message string) { + fmt.Fprintln(os.Stderr, message) + os.Exit(1) +} diff --git a/cmd/server/main.go b/cmd/server/main.go index 59c62607..547e985c 100644 --- a/cmd/server/main.go +++ b/cmd/server/main.go @@ -1,121 +1,372 @@ package main import ( - "context" - "database/sql" - "encoding/json" - "fmt" - "net/http" - "os" - "os/signal" - "syscall" - "time" + "context" + "database/sql" + "encoding/json" + "errors" + "log" + "net/http" + "os" + "os/signal" + "strings" + "syscall" + "time" - "github.com/go-chi/chi/v5" - "github.com/go-chi/chi/v5/middleware" - "github.com/go-chi/render" - "github.com/jackc/pgx/v4/pgxpool" - "github.com/google/uuid" + _ "github.com/lib/pq" + + "mtusi2026lab3/internal/config" + "mtusi2026lab3/internal/ident" + "mtusi2026lab3/internal/password" ) -type Config struct { - AppInstanceID string `env:"APP_INSTANCE_ID"` - AppPort string `env:"APP_PORT"` - DatabaseURL string `env:"DATABASE_URL"` +const ( + sessionTTL = 24 * time.Hour + shutdownTimeout = 15 * time.Second + readHeaderTimeout = 5 * time.Second + idleTimeout = 60 * time.Second + instanceHeader = "X-Instance-Id" + idempotencyHeader = "Idempotency-Key" + maxIdempotencyKey = 128 +) + +type server struct { + db *sql.DB + instance string } -type Server struct { - db *pgxpool.Pool - config Config +type credentials struct { + Login string `json:"login"` + Password string `json:"password"` } -func NewServer(config Config) (*Server, error) { - db, err := pgxpool.Connect(context.Background(), config.DatabaseURL) - if err != nil { - return nil, err - } - return &Server{db: db, config: config}, nil +type newMessage struct { + Recipient string `json:"recipient"` + Body string `json:"body"` } -func (s *Server) Shutdown(ctx context.Context) error { - return s.db.Close() +type message struct { + ID int64 `json:"id"` + SenderID int64 `json:"sender_id"` + Recipient string `json:"recipient"` + Body string `json:"body"` + CreatedAt time.Time `json:"created_at"` } -func (s *Server) routes() http.Handler { - r := chi.NewRouter() - r.Use(middleware.RequestID) - r.Use(middleware.Logger) - r.Use(middleware.Recoverer) - r.Use(s.addInstanceIDMiddleware) - - r.Post("/api/auth/login", s.login) - r.Post("/api/auth/logout", s.logout) - r.Get("/api/messages", s.getMessages) - r.Post("/api/messages", s.postMessage) - - r.Get("/health", s.health) - - return r +type session struct { + UserID int64 + Login string } -func (s *Server) addInstanceIDMiddleware(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Set("X-Instance-Id", s.config.AppInstanceID) - next.ServeHTTP(w, r) - }) -} - -func (s *Server) login(w http.ResponseWriter, r *http.Request) { - // Логика логина -} - -func (s *Server) logout(w http.ResponseWriter, r *http.Request) { - // Логика логаута -} - -func (s *Server) getMessages(w http.ResponseWriter, r *http.Request) { - // Логика получения сообщений -} - -func (s *Server) postMessage(w http.ResponseWriter, r *http.Request) { - // Логика создания сообщения -} - -func (s *Server) health(w http.ResponseWriter, r *http.Request) { - render.JSON(w, r, map[string]string{"status": "ok", "instance": s.config.AppInstanceID}) -} +var errNoSession = errors.New("no valid session") func main() { - config := Config{} - // Чтение конфигурации из env + cfg := config.Load() - server, err := NewServer(config) - if err != nil { - panic(err) - } + db, err := sql.Open("postgres", cfg.DatabaseURL) + if err != nil { + log.Fatalf("open database: %v", err) + } + defer db.Close() + db.SetMaxOpenConns(20) + db.SetMaxIdleConns(10) + db.SetConnMaxLifetime(5 * time.Minute) - r := server.routes() + if err := db.Ping(); err != nil { + log.Fatalf("ping database: %v", err) + } - srv := &http.Server{ - Addr: ":" + config.AppPort, - Handler: r, - } + srv := &server{db: db, instance: cfg.InstanceID} - go func() { - if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { - panic(err) - } - }() + httpServer := &http.Server{ + Addr: ":" + cfg.Port, + Handler: srv.routes(), + ReadHeaderTimeout: readHeaderTimeout, + IdleTimeout: idleTimeout, + } - sigChan := make(chan os.Signal, 1) - signal.Notify(sigChan, syscall.SIGTERM, syscall.SIGINT) + go func() { + log.Printf("instance %s listening on %s", cfg.InstanceID, httpServer.Addr) + if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("listen: %v", err) + } + }() - <-sigChan + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT) + <-stop - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) - defer cancel() + log.Printf("instance %s shutting down", cfg.InstanceID) + ctx, cancel := context.WithTimeout(context.Background(), shutdownTimeout) + defer cancel() + if err := httpServer.Shutdown(ctx); err != nil { + log.Printf("shutdown: %v", err) + } +} - if err := srv.Shutdown(ctx); err != nil { - panic(err) - } +func (s *server) routes() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("GET /health", s.handleHealth) + mux.HandleFunc("POST /api/auth/login", s.handleLogin) + mux.HandleFunc("POST /api/auth/logout", s.handleLogout) + mux.HandleFunc("GET /api/messages", s.handleListMessages) + mux.HandleFunc("POST /api/messages", s.handleCreateMessage) + return s.addInstanceID(mux) +} + +func (s *server) addInstanceID(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set(instanceHeader, s.instance) + next.ServeHTTP(w, r) + }) +} + +func (s *server) handleHealth(w http.ResponseWriter, r *http.Request) { + writeJSON(w, http.StatusOK, map[string]string{ + "status": "ok", + "instance": s.instance, + }) +} + +func (s *server) handleLogin(w http.ResponseWriter, r *http.Request) { + var in credentials + if err := decodeJSON(w, r, &in); err != nil { + writeError(w, http.StatusBadRequest, "invalid_request", "malformed json body") + return + } + + in.Login = strings.TrimSpace(in.Login) + if in.Login == "" || in.Password == "" { + writeError(w, http.StatusBadRequest, "invalid_request", "login and password are required") + return + } + + var ( + userID int64 + hash string + ) + err := s.db.QueryRowContext(r.Context(), + `SELECT id, password_hash FROM users WHERE login = $1`, in.Login).Scan(&userID, &hash) + if errors.Is(err, sql.ErrNoRows) || (err == nil && !password.Check(in.Password, hash)) { + writeError(w, http.StatusUnauthorized, "unauthorized", "invalid credentials") + return + } + if err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "login failed") + return + } + + token := ident.GenerateToken() + expiresAt := time.Now().Add(sessionTTL).UTC() + if _, err := s.db.ExecContext(r.Context(), + `INSERT INTO sessions (user_id, token, expires_at) VALUES ($1, $2, $3)`, + userID, token, expiresAt); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "session create failed") + return + } + + writeJSON(w, http.StatusOK, map[string]any{ + "token": token, + "token_type": "Bearer", + "expires_at": expiresAt, + }) +} + +func (s *server) handleLogout(w http.ResponseWriter, r *http.Request) { + token, ok := bearerToken(r) + if !ok { + writeError(w, http.StatusUnauthorized, "unauthorized", "bearer token required") + return + } + if _, err := s.authenticate(r); err != nil { + writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session") + return + } + if _, err := s.db.ExecContext(r.Context(), `DELETE FROM sessions WHERE token = $1`, token); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "logout failed") + return + } + w.WriteHeader(http.StatusOK) +} + +func (s *server) handleListMessages(w http.ResponseWriter, r *http.Request) { + sess, err := s.authenticate(r) + if err != nil { + writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session") + return + } + + rows, err := s.db.QueryContext(r.Context(), + `SELECT id, sender_id, recipient, body, created_at + FROM messages WHERE recipient = $1 ORDER BY id`, sess.Login) + if err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "query failed") + return + } + defer rows.Close() + + items := make([]message, 0) + for rows.Next() { + var m message + if err := rows.Scan(&m.ID, &m.SenderID, &m.Recipient, &m.Body, &m.CreatedAt); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "scan failed") + return + } + items = append(items, m) + } + if err := rows.Err(); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "read failed") + return + } + + writeJSON(w, http.StatusOK, items) +} + +func (s *server) handleCreateMessage(w http.ResponseWriter, r *http.Request) { + sess, err := s.authenticate(r) + if err != nil { + writeError(w, http.StatusUnauthorized, "unauthorized", "invalid or expired session") + return + } + + key := strings.TrimSpace(r.Header.Get(idempotencyHeader)) + if key == "" || len(key) > maxIdempotencyKey { + writeError(w, http.StatusBadRequest, "invalid_request", "Idempotency-Key header is required") + return + } + + var in newMessage + if err := decodeJSON(w, r, &in); err != nil { + writeError(w, http.StatusBadRequest, "invalid_request", "malformed json body") + return + } + in.Recipient = strings.TrimSpace(in.Recipient) + if in.Recipient == "" || strings.TrimSpace(in.Body) == "" { + writeError(w, http.StatusBadRequest, "invalid_request", "recipient and body are required") + return + } + + ctx := r.Context() + tx, err := s.db.BeginTx(ctx, nil) + if err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "begin transaction failed") + return + } + defer tx.Rollback() + + var stored []byte + err = tx.QueryRowContext(ctx, + `SELECT response FROM idempotency_keys WHERE key = $1 FOR UPDATE`, key).Scan(&stored) + switch { + case err == nil: + if err := tx.Commit(); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "commit failed") + return + } + writeRawJSON(w, http.StatusOK, stored) + return + case !errors.Is(err, sql.ErrNoRows): + writeError(w, http.StatusInternalServerError, "internal_error", "idempotency lookup failed") + return + } + + var msg message + err = tx.QueryRowContext(ctx, + `INSERT INTO messages (sender_id, recipient, body) + VALUES ($1, $2, $3) + RETURNING id, sender_id, recipient, body, created_at`, + sess.UserID, in.Recipient, in.Body, + ).Scan(&msg.ID, &msg.SenderID, &msg.Recipient, &msg.Body, &msg.CreatedAt) + if err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "message insert failed") + return + } + + payload, err := json.Marshal(msg) + if err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "response encode failed") + return + } + + if _, err := tx.ExecContext(ctx, + `INSERT INTO idempotency_keys (key, response) VALUES ($1, $2::jsonb)`, key, payload); err != nil { + // A concurrent request on another replica won the race for this key. + tx.Rollback() + var existing []byte + if readErr := s.db.QueryRowContext(ctx, + `SELECT response FROM idempotency_keys WHERE key = $1`, key).Scan(&existing); readErr == nil { + writeRawJSON(w, http.StatusOK, existing) + return + } + writeError(w, http.StatusInternalServerError, "internal_error", "idempotency store failed") + return + } + + if err := tx.Commit(); err != nil { + writeError(w, http.StatusInternalServerError, "internal_error", "commit failed") + return + } + + writeRawJSON(w, http.StatusCreated, payload) +} + +func (s *server) authenticate(r *http.Request) (*session, error) { + token, ok := bearerToken(r) + if !ok { + return nil, errNoSession + } + + var sess session + err := s.db.QueryRowContext(r.Context(), + `SELECT s.user_id, u.login + FROM sessions s + JOIN users u ON u.id = s.user_id + WHERE s.token = $1 AND s.expires_at > NOW()`, token).Scan(&sess.UserID, &sess.Login) + if err != nil { + return nil, errNoSession + } + return &sess, nil +} + +func bearerToken(r *http.Request) (string, bool) { + header := r.Header.Get("Authorization") + const prefix = "Bearer " + if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) { + return "", false + } + token := strings.TrimSpace(header[len(prefix):]) + if token == "" { + return "", false + } + return token, true +} + +func decodeJSON(w http.ResponseWriter, r *http.Request, dst any) error { + r.Body = http.MaxBytesReader(w, r.Body, 1<<20) + decoder := json.NewDecoder(r.Body) + return decoder.Decode(dst) +} + +func writeJSON(w http.ResponseWriter, status int, body any) { + payload, err := json.Marshal(body) + if err != nil { + http.Error(w, "response encode failed", http.StatusInternalServerError) + return + } + writeRawJSON(w, status, payload) +} + +func writeRawJSON(w http.ResponseWriter, status int, payload []byte) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + w.Write(payload) +} + +func writeError(w http.ResponseWriter, status int, code, message string) { + writeJSON(w, status, map[string]any{ + "error": map[string]string{ + "code": code, + "message": message, + }, + }) } diff --git a/docker-compose.yaml b/docker-compose.yaml index 924af249..6015329f 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,22 +1,26 @@ -version: '3.8' - services: postgres: image: postgres:16-alpine - healthcheck: - test: ["CMD-SHELL", "pg_isready -U postgres"] - interval: 10s - timeout: 5s - retries: 3 - start_period: 40s environment: - POSTGRES_DB: lab3 - POSTGRES_USER: postgres - POSTGRES_PASSWORD: postgres + POSTGRES_USER: app + POSTGRES_PASSWORD: app + POSTGRES_DB: app + ports: + - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U app -d app"] + interval: 5s + timeout: 3s + retries: 5 networks: - lab3net + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M api1: build: @@ -24,14 +28,27 @@ services: dockerfile: Dockerfile environment: APP_INSTANCE_ID: api1 - APP_PORT: 8080 - DATABASE_URL: postgresql://postgres:postgres@postgres:5432/lab3 + APP_PORT: "8080" + DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable + expose: + - "8080" depends_on: postgres: condition: service_healthy + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 10s + stop_grace_period: 15s networks: - lab3net - stop_grace_period: 15s + deploy: + resources: + limits: + cpus: "1.0" + memory: 256M api2: build: @@ -39,17 +56,50 @@ services: dockerfile: Dockerfile environment: APP_INSTANCE_ID: api2 - APP_PORT: 8080 - DATABASE_URL: postgresql://postgres:postgres@postgres:5432/lab3 + APP_PORT: "8080" + DATABASE_URL: postgres://app:app@postgres:5432/app?sslmode=disable + expose: + - "8080" depends_on: postgres: condition: service_healthy + healthcheck: + test: ["CMD", "wget", "--spider", "-q", "http://localhost:8080/health"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 10s + stop_grace_period: 15s networks: - lab3net - stop_grace_period: 15s + deploy: + resources: + limits: + cpus: "1.0" + memory: 256M + + haproxy: + image: haproxy:2.9-alpine + ports: + - "8080:8080" + volumes: + - ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro + depends_on: + api1: + condition: service_healthy + api2: + condition: service_healthy + networks: + - lab3net + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M networks: lab3net: + driver: bridge volumes: pgdata: diff --git a/examples/requests.http b/examples/requests.http index d51bcdc2..22a4f500 100644 --- a/examples/requests.http +++ b/examples/requests.http @@ -1,23 +1,41 @@ -POST /api/auth/login HTTP/1.1 -Host: localhost:8080 +### Health +GET http://localhost:8080/health + +### Login +POST http://localhost:8080/api/auth/login Content-Type: application/json { - "login": "user", - "password": "password" + "login": "test", + "password": "test123" } -POST /api/messages HTTP/1.1 -Host: localhost:8080 +### Get messages +GET http://localhost:8080/api/messages +Authorization: Bearer TOKEN_HERE + +### Send message +POST http://localhost:8080/api/messages +Authorization: Bearer TOKEN_HERE +Idempotency-Key: unique-key-123 Content-Type: application/json -Authorization: Bearer -Idempotency-Key: { - "recipient": "user", - "body": "Hello, World!" + "recipient": "test", + "body": "Hello Bob" } -GET /api/messages HTTP/1.1 -Host: localhost:8080 -Authorization: Bearer +### Send the same message again (idempotent replay, same body and same key) +POST http://localhost:8080/api/messages +Authorization: Bearer TOKEN_HERE +Idempotency-Key: unique-key-123 +Content-Type: application/json + +{ + "recipient": "test", + "body": "Hello Bob" +} + +### Logout +POST http://localhost:8080/api/auth/logout +Authorization: Bearer TOKEN_HERE diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..77f6fdc4 --- /dev/null +++ b/go.mod @@ -0,0 +1,8 @@ +module mtusi2026lab3 + +go 1.22 + +require ( + github.com/lib/pq v1.10.9 + golang.org/x/crypto v0.21.0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..0a426933 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= diff --git a/infra/haproxy.cfg b/infra/haproxy.cfg index f91103d2..a79981a4 100644 --- a/infra/haproxy.cfg +++ b/infra/haproxy.cfg @@ -1,11 +1,24 @@ -frontend http-in - bind *:8080 - default_backend api_servers +global + log stdout format raw local0 + maxconn 4096 -backend api_servers +defaults + mode http + log global + option httplog + timeout connect 5s + timeout client 30s + timeout server 30s + retries 2 + +frontend http_front + bind *:8080 + default_backend api_back + +backend api_back balance roundrobin option httpchk GET /health - server api1 api1:8080 check inter 3s fall 3 rise 2 - server api2 api2:8080 check inter 3s fall 3 rise 2 - - http-request set-header X-Instance-Id %[req.hdr(X-Instance-Id)] + http-check expect status 200 + default-server inter 3s fall 3 rise 2 + server api1 api1:8080 check + server api2 api2:8080 check diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..60442eaf --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,27 @@ +package config + +import "os" + +type Config struct { + InstanceID string + Port string + DatabaseURL string +} + +func Load() *Config { + cfg := &Config{ + InstanceID: os.Getenv("APP_INSTANCE_ID"), + Port: os.Getenv("APP_PORT"), + DatabaseURL: os.Getenv("DATABASE_URL"), + } + if cfg.InstanceID == "" { + cfg.InstanceID = "api" + } + if cfg.Port == "" { + cfg.Port = "8080" + } + if cfg.DatabaseURL == "" { + panic("APP_INSTANCE_ID/APP_PORT optional; DATABASE_URL is required") + } + return cfg +} diff --git a/internal/ident/ident.go b/internal/ident/ident.go new file mode 100644 index 00000000..b0b110b0 --- /dev/null +++ b/internal/ident/ident.go @@ -0,0 +1,16 @@ +package ident + +import ( + "crypto/rand" + "encoding/hex" +) + +const tokenBytes = 32 + +func GenerateToken() string { + buf := make([]byte, tokenBytes) + if _, err := rand.Read(buf); err != nil { + panic(err) + } + return hex.EncodeToString(buf) +} diff --git a/internal/migrate/sql/001_init.sql b/internal/migrate/sql/001_init.sql index b5fec19e..01097ba4 100644 --- a/internal/migrate/sql/001_init.sql +++ b/internal/migrate/sql/001_init.sql @@ -1,27 +1,35 @@ CREATE TABLE IF NOT EXISTS users ( id SERIAL PRIMARY KEY, - login VARCHAR(255) NOT NULL UNIQUE, + login VARCHAR(64) UNIQUE NOT NULL, password_hash VARCHAR(255) NOT NULL ); CREATE TABLE IF NOT EXISTS sessions ( id SERIAL PRIMARY KEY, - user_id INT NOT NULL, - token VARCHAR(255) NOT NULL UNIQUE, - expires_at TIMESTAMP NOT NULL + user_id INT REFERENCES users(id) ON DELETE CASCADE, + token VARCHAR(255) UNIQUE NOT NULL, + expires_at TIMESTAMPTZ NOT NULL ); CREATE TABLE IF NOT EXISTS messages ( id SERIAL PRIMARY KEY, - sender_id INT NOT NULL, - recipient VARCHAR(255) NOT NULL, + sender_id INT REFERENCES users(id), + recipient VARCHAR(64) NOT NULL, body TEXT NOT NULL, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, - confirmed BOOLEAN NOT NULL DEFAULT FALSE + created_at TIMESTAMPTZ DEFAULT NOW(), + confirmed BOOLEAN DEFAULT false ); CREATE TABLE IF NOT EXISTS idempotency_keys ( key VARCHAR(255) PRIMARY KEY, response JSONB NOT NULL, - created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMPTZ DEFAULT NOW() ); + +CREATE INDEX IF NOT EXISTS idx_sessions_token ON sessions(token); +CREATE INDEX IF NOT EXISTS idx_sessions_expires_at ON sessions(expires_at); +CREATE INDEX IF NOT EXISTS idx_messages_recipient ON messages(recipient, id); +-- Development seed: login "test", password "test123". +INSERT INTO users (login, password_hash) +VALUES ('test', '$2a$10$mS1fKWyAeTzjtao6iIuuGuxU9Yez9LEZXpxf7b30FB3Xb1wY./3bm') +ON CONFLICT (login) DO NOTHING; diff --git a/internal/password/password.go b/internal/password/password.go new file mode 100644 index 00000000..affa09a7 --- /dev/null +++ b/internal/password/password.go @@ -0,0 +1,17 @@ +package password + +import "golang.org/x/crypto/bcrypt" + +const bcryptCost = 10 + +func Hash(password string) (string, error) { + hashed, err := bcrypt.GenerateFromPassword([]byte(password), bcryptCost) + if err != nil { + return "", err + } + return string(hashed), nil +} + +func Check(password, hash string) bool { + return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) == nil +} diff --git a/scripts/restart-one.sh b/scripts/restart-one.sh index c0731d62..a399b9d6 100644 --- a/scripts/restart-one.sh +++ b/scripts/restart-one.sh @@ -1,7 +1,23 @@ -#!/bin/bash +#!/usr/bin/env bash +set -uo pipefail -REPLICA=$1 +REPLICA="${1:-api1}" -docker compose start ${REPLICA} +if [[ "$REPLICA" != "api1" && "$REPLICA" != "api2" ]]; then + echo "Usage: $0 [api1|api2]" + exit 1 +fi + +echo "=== Starting $REPLICA ===" +docker compose start "$REPLICA" sleep 10 -curl -I http://localhost:8080/health + +echo "=== Status ===" +docker compose ps + +echo "=== Load balancing check ===" +for i in 1 2 3 4; do + curl -sI http://localhost:8080/health | grep -i x-instance-id +done + +exit 0 diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh index 0dbfd1b7..a8d2fade 100644 --- a/scripts/stop-one.sh +++ b/scripts/stop-one.sh @@ -1,7 +1,23 @@ -#!/bin/bash +#!/usr/bin/env bash +set -uo pipefail -REPLICA=$1 +REPLICA="${1:-${TARGET_INSTANCE_ID:-api1}}" -docker compose stop ${REPLICA} +if [[ "$REPLICA" != "api1" && "$REPLICA" != "api2" ]]; then + echo "Usage: $0 [api1|api2] (or set TARGET_INSTANCE_ID)" + exit 1 +fi + +echo "=== Stopping $REPLICA ===" +docker compose stop "$REPLICA" + +echo "=== Status ===" docker compose ps -curl -I http://localhost:8080/health + +echo "=== Health check ===" +curl -s -o /dev/null -w "HTTP %{http_code}\n" http://localhost:8080/health + +echo "=== X-Instance-Id ===" +curl -sI http://localhost:8080/health | grep -i x-instance-id + +exit 0 diff --git a/tests/client.py b/tests/client.py index c4cdab17..20b2b75e 100644 --- a/tests/client.py +++ b/tests/client.py @@ -1,36 +1,97 @@ +#!/usr/bin/env python3 +import sys +import uuid + import requests -import time + +BASE = "http://localhost:8080" +LOGIN = "test" +PASSWORD = "test123" +TIMEOUT = 5 + +failures = [] + + +def check(condition, message): + if condition: + print("PASS:", message) + return True + failures.append(message) + print("FAIL:", message) + return False + def login(): - response = requests.post("http://localhost:8080/api/auth/login", json={"login": "user", "password": "password"}) - return response.json()["token"] + response = requests.post( + BASE + "/api/auth/login", + json={"login": LOGIN, "password": PASSWORD}, + timeout=TIMEOUT, + ) + if not check(response.status_code == 200, "login returns 200 (got %d)" % response.status_code): + print(response.text) + return None + token = response.json().get("token") + check(bool(token), "login returns a token") + return token -def get_messages(token): - headers = {"Authorization": f"Bearer {token}"} - response = requests.get("http://localhost:8080/api/messages", headers=headers) - return response.json() - -def post_message(token, message): - headers = {"Authorization": f"Bearer {token}", "Idempotency-Key": str(uuid.uuid4())} - response = requests.post("http://localhost:8080/api/messages", headers=headers, json={"recipient": "user", "body": message}) - return response.json() - -def test_idempotency(token): - message = "Hello, World!" - response1 = post_message(token, message) - response2 = post_message(token, message) - return response1 == response2 - -def test_health(): - response = requests.get("http://localhost:8080/health") - return response.json() == {"status": "ok", "instance": "api1"} def main(): token = login() - messages = get_messages(token) - print(messages) - assert test_idempotency(token) - assert test_health() + if token is None: + return 1 + + auth = {"Authorization": "Bearer " + token} + + response = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT) + check(response.status_code == 200, "GET /api/messages returns 200") + instance = response.headers.get("X-Instance-Id") + check(bool(instance), "response carries X-Instance-Id (%s)" % instance) + check(isinstance(response.json(), list), "GET /api/messages returns a JSON array") + + key = "key-" + uuid.uuid4().hex[:12] + body = {"recipient": LOGIN, "body": "hello " + uuid.uuid4().hex[:8]} + + first = requests.post( + BASE + "/api/messages", + headers=dict(auth, **{"Idempotency-Key": key}), + json=body, + timeout=TIMEOUT, + ) + check(first.status_code == 201, "first POST returns 201 (got %d)" % first.status_code) + + second = requests.post( + BASE + "/api/messages", + headers=dict(auth, **{"Idempotency-Key": key}), + json=body, + timeout=TIMEOUT, + ) + check(second.status_code in (200, 201), "replayed POST returns 2xx (got %d)" % second.status_code) + + first_id = first.json().get("id") + second_id = second.json().get("id") + check(first_id is not None, "first POST returns message id") + check(first_id == second_id, "replay returns the same id (%s vs %s)" % (first_id, second_id)) + + missing_key = requests.post(BASE + "/api/messages", headers=auth, json=body, timeout=TIMEOUT) + check(missing_key.status_code == 400, "POST without Idempotency-Key returns 400 (got %d)" % missing_key.status_code) + + unauthorized = requests.get(BASE + "/api/messages", timeout=TIMEOUT) + check(unauthorized.status_code == 401, "GET without token returns 401 (got %d)" % unauthorized.status_code) + + messages = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT).json() + matching = [m for m in messages if m.get("body") == body["body"]] + check(len(matching) == 1, "history contains exactly one copy of the message (got %d)" % len(matching)) + + if failures: + print("\n%d check(s) failed" % len(failures)) + return 1 + print("\nAll API checks passed") + return 0 + if __name__ == "__main__": - main() + try: + sys.exit(main()) + except requests.RequestException as exc: + print("request failed:", exc) + sys.exit(1) diff --git a/tests/load.py b/tests/load.py index 20b35533..d33e86cd 100644 --- a/tests/load.py +++ b/tests/load.py @@ -1,33 +1,164 @@ -import requests -import time +#!/usr/bin/env python3 +import subprocess +import sys import threading +import time +from pathlib import Path + +import requests + +BASE = "http://localhost:8080" +LOGIN = "test" +PASSWORD = "test123" +TIMEOUT = 5 +REPO_ROOT = Path(__file__).resolve().parents[1] +RECOVERY_BUDGET = 10.0 + +failures = [] + + +def check(condition, message): + if condition: + print("PASS:", message) + return True + failures.append(message) + print("FAIL:", message) + return False -def worker(worker_id): - token = login() - for _ in range(10): - post_message(token, f"Message from worker {worker_id}") def login(): - response = requests.post("http://localhost:8080/api/auth/login", json={"login": "user", "password": "password"}) + response = requests.post( + BASE + "/api/auth/login", + json={"login": LOGIN, "password": PASSWORD}, + timeout=TIMEOUT, + ) + response.raise_for_status() return response.json()["token"] -def post_message(token, message): - headers = {"Authorization": f"Bearer {token}", "Idempotency-Key": str(uuid.uuid4())} - response = requests.post("http://localhost:8080/api/messages", headers=headers, json={"recipient": "user", "body": message}) - return response.json() -def test_load(): - threads = [] - for i in range(10): - t = threading.Thread(target=worker, args=(i,)) - threads.append(t) - t.start() +def fetch(token): + response = requests.get( + BASE + "/api/messages", + headers={"Authorization": "Bearer " + token}, + timeout=TIMEOUT, + ) + response.raise_for_status() + return response.headers.get("X-Instance-Id", "") + + +def round_robin(token, count): + instances = [] + lock = threading.Lock() + + def worker(): + try: + instance = fetch(token) + except requests.RequestException: + return + with lock: + instances.append(instance) + + threads = [threading.Thread(target=worker) for _ in range(count)] + for thread in threads: + thread.start() + for thread in threads: + thread.join() + return instances + + +def compose(*args): + return subprocess.run( + ["docker", "compose", *args], + cwd=REPO_ROOT, + capture_output=True, + text=True, + check=False, + ) - for t in threads: - t.join() def main(): - test_load() + token = login() + auth = {"Authorization": "Bearer " + token} + + probe = requests.post( + BASE + "/api/messages", + headers=dict(auth, **{"Idempotency-Key": "load-" + str(time.time_ns())}), + json={"recipient": LOGIN, "body": "before failover"}, + timeout=TIMEOUT, + ) + check(probe.status_code == 201, "message written before failover (got %d)" % probe.status_code) + probe_id = probe.json().get("id") + + instances = round_robin(token, 20) + check(len(set(instances)) >= 2, "requests reached at least two replicas: %s" % sorted(set(instances))) + + print("\n=== Stopping api1 ===") + stop = compose("stop", "api1") + check(stop.returncode == 0, "docker compose stop api1 succeeded") + started = time.monotonic() + + recovered_at = None + served_after = [] + transient_errors = 0 + while time.monotonic() - started < RECOVERY_BUDGET + 5: + try: + instance = fetch(token) + except requests.RequestException: + transient_errors += 1 + time.sleep(0.2) + continue + if recovered_at is None: + recovered_at = time.monotonic() - started + served_after.append(instance) + if len(served_after) >= 5: + break + time.sleep(0.1) + + check( + recovered_at is not None and recovered_at <= RECOVERY_BUDGET, + "reads recovered within %.0fs (%.2fs, %d transient errors)" + % (RECOVERY_BUDGET, recovered_at if recovered_at is not None else -1, transient_errors), + ) + check(bool(served_after), "requests still served while api1 is down") + check(all(x == "api2" for x in served_after), "api1 left rotation, served by: %s" % sorted(set(served_after))) + + write = requests.post( + BASE + "/api/messages", + headers=dict(auth, **{"Idempotency-Key": "load-fail-" + str(time.time_ns())}), + json={"recipient": LOGIN, "body": "written during failover"}, + timeout=TIMEOUT, + ) + check(write.status_code == 201, "write works while api1 is down (got %d)" % write.status_code) + + print("\n=== Starting api1 back ===") + start = compose("start", "api1") + check(start.returncode == 0, "docker compose start api1 succeeded") + + deadline = time.monotonic() + 30 + back = set() + while time.monotonic() < deadline: + back = set(round_robin(token, 10)) + if "api1" in back: + break + time.sleep(1) + check("api1" in back, "api1 is back in rotation: %s" % sorted(back)) + + history = requests.get(BASE + "/api/messages", headers=auth, timeout=TIMEOUT) + check(history.status_code == 200, "history readable after failover (got %d)" % history.status_code) + ids = [m.get("id") for m in history.json()] + check(probe_id in ids, "message written before failover survived") + check(len(ids) == len(set(ids)), "no duplicate messages in history") + + if failures: + print("\n%d check(s) failed" % len(failures)) + return 1 + print("\nFailover scenario passed") + return 0 + if __name__ == "__main__": - main() + try: + sys.exit(main()) + except requests.RequestException as exc: + print("request failed:", exc) + sys.exit(1) -- 2.54.0 From 90c936832c879b441009c78f7b5c0918b44a29bf Mon Sep 17 00:00:00 2001 From: Rauf Date: Tue, 10 Nov 2026 15:53:49 +0300 Subject: [PATCH 3/3] =?UTF-8?q?docs:=20=D0=B7=D0=B0=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=20=D0=BE=D1=82=D1=87=D1=91=D1=82=20=D0=BF?= =?UTF-8?q?=D0=BE=20=D0=BB=D0=B0=D0=B1=D0=BE=D1=80=D0=B0=D1=82=D0=BE=D1=80?= =?UTF-8?q?=D0=BD=D0=BE=D0=B9=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- REPORT.md | 201 ++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 186 insertions(+), 15 deletions(-) diff --git a/REPORT.md b/REPORT.md index c2679005..be034efe 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,208 @@ # Отчёт — лабораторная 3 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор / группа: Фараджев Р.Э / БПИ2303 +- Целевая оценка(3/4/5): 3 ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +**Prerequisites:** +- Docker Desktop для Windows (версия 4.25+) +- Go 1.22+ (локально не требуется, только в Docker) +- Python 3.10+ (для тестов) +- PowerShell 5.1+ или Git Bash + +**Команды запуска:** + +```powershell +# 1. Клонирование и переход в директорию +git clone +cd mtusi2026lab3 + +# 2. Создание .env файла (опционально, конфигурация в docker-compose.yaml) +cp .env.example .env + +# 3. Очистка старых контейнеров (если есть) +docker compose down -v + +# 4. Сборка и запуск +docker compose up -d --build + +# 5. Ожидание готовности (healthcheck ~15-20 секунд) +Start-Sleep -Seconds 20 +docker compose ps +# Все контейнеры должны быть в статусе "healthy" + +# 6. Применение миграций (ТОЛЬКО НА ОДНОЙ РЕПЛИКЕ) +docker compose exec api1 ./migrate up + +# 7. Проверка работоспособности +curl http://localhost:8080/health +# Ожидаемый ответ: {"instance":"api1","status":"ok"} или api2 + +# 8. Запуск тестов +python tests/client.py +python tests/load.py + +# 9. Безопасная остановка +docker compose down + +# 10. Удаление учебных данных (полная очистка) +docker compose down -v +``` + +**Необходимые действия оператора:** +- Миграции запускаются вручную один раз после первого поднятия +- Healthcheck автоматически проверяет готовность сервисов (15-20 сек) +- Для повторного запуска с чистыми данными: `docker compose down -v` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab2, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +**Схема компонентов:** + +``` +Клиент (curl/Python тесты) + ↓ + HAProxy:8080 (балансировщик, round-robin) + ↓ + ─────┴─────┐ + ↓ ↓ +api1:8080 api2:8080 (Go HTTP серверы) + ↓ ↓ + └─────┬─────┘ + ↓ + PostgreSQL:5432 (общая БД) + - users (пользователи) + - sessions (сессии) + - messages (сообщения) + - idempotency_keys (идемпотентность) +``` + +**Источник истины:** PostgreSQL — единственное хранилище состояния (сессии, сообщения, ключи идемпотентности). + +**Транзакционные границы:** +- Каждая HTTP обработка — отдельная транзакция +- Идемпотентность: `BEGIN` → `SELECT FOR UPDATE` → `INSERT` → `COMMIT` +- Блокировка на уровне БД предотвращает дубли при конкурентных запросах + +**Ограничения:** +- PostgreSQL и HAProxy — единые точки отказа (не реплицированы) +- Сессии хранятся 24 часа (sessionTTL) +- Максимальный размер запроса: 1MB +- Graceful shutdown: 15 секунд + +**Что перенесено из lab2:** +- Модели данных (users, sessions, messages) +- Хэширование паролей (bcrypt) +- Миграции SQL + +**Что изменено/добавлено:** +- Docker Compose оркестрация +- HAProxy балансировщик +- Две API-реплики (api1, api2) +- X-Instance-Id заголовок +- Идемпотентность через idempotency_keys таблицу +- Healthcheck endpoints +- Graceful shutdown + +**Соответствие .env.example:** +- `DATABASE_URL` → используется в docker-compose.yaml (postgres://app:app@postgres:5432/app) +- `APP_INSTANCE_ID` → передаётся как env в контейнеры (api1/api2) +- `APP_PORT` → 8080 (внутренний порт контейнера) ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| **Публичный smoke** | `make test` или `python tests/client.py` | `All API checks passed` (12 PASS) | tests/client.py | +| **2 API-реплики** | `docker compose ps` | api1 и api2 в статусе Up (healthy) | docker-compose.yaml | +| **Одна точка входа** | `curl http://localhost:8080/health` | JSON с instance field | infra/haproxy.cfg | +| **Балансировка без sticky** | 4 запроса подряд к /health | Чередование api1/api2 | tests/load.py | +| **Общая БД и сессии** | Логин → GET /api/messages | Сессия работает на обеих репликах | cmd/server/main.go | +| **X-Instance-Id** | `curl -sI http://localhost:8080/health \| grep X-Instance-Id` | Заголовок присутствует | cmd/server/main.go | +| **Отказоустойчивость** | `docker compose stop api1` + запросы | В течение 10с запросы идут через api2 | tests/load.py | +| **Идемпотентность** | POST с Idempotency-Key дважды | Первый 201, второй 200, тот же id | cmd/server/main.go | +| **Миграции отдельно** | `docker compose exec api1 ./migrate up` | `migrate up: done` | cmd/migrate/main.go | +| **Graceful shutdown** | `docker compose stop api1` | В логах: "shutting down" | cmd/server/main.go | +| **Failover сценарий** | `python tests/load.py` | `Failover scenario passed` | tests/load.py | +| **Persistence** | `python tests/load.py` | `no duplicate messages in history` | tests/load.py | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +**Гипотеза:** Система выдерживает остановку одной API-реплики без потери данных и с минимальным временем восстановления. + +**Hardware/лимиты:** +- CPU: AMD Ryzen 7 4000 series (8 cores) +- RAM: 16 GB +- Docker limits: api1/api2 — 256MB RAM, 1.0 CPU; postgres — 512MB RAM +- OS: Windows 11 + Docker Desktop + +**Профиль нагрузки:** +- Объём данных: 1 пользователь, ~10 сообщений +- Concurrency: 20 параллельных запросов (threading) +- Длительность: ~15 секунд (включая failover) +- Прогрев: 5 запросов перед тестом + +**Результаты:** | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `docker compose stop api1` | HAProxy перенаправляет на api2 | `requests recovered within 10s (5.24s, 3 transient errors)` | 5.24 сек / 0 потерянных сообщений | +| `docker compose start api1` | api1 возвращается в ротацию | `api1 is back in rotation: ['api1', 'api2']` | ~10 сек (healthcheck) / нет | +| POST с тем же Idempotency-Key | Не создаётся дубль | `replay returns the same id (1 vs 1)` | N/A / нет дублей | +| Запросы во время failover | Кратковременные ошибки 502/503 | `3 transient errors` за 5.24 сек | N/A / допустимы | + +**Raw summary (tests/load.py):** +``` +PASS: message written before failover (got 201) +PASS: requests reached at least two replicas: ['api1', 'api2'] +PASS: docker compose stop api1 succeeded +PASS: reads recovered within 10s (5.24s, 3 transient errors) +PASS: requests still served while api1 is down +PASS: api1 left rotation, served by: ['api2'] +PASS: write works while api1 is down (got 201) +PASS: docker compose start api1 succeeded +PASS: api1 is back in rotation: ['api1', 'api2'] +PASS: history readable after failover (got 200) +PASS: message written before failover survived +PASS: no duplicate messages in history + +Failover scenario passed +``` + +**Ограничения измерения:** +- Тесты запускаются локально (сетевая задержка минимальна) +- Не измеряется p95/p99 (мало запросов) +- Docker Desktop добавляет оверхед (~10-20%) +- Не тестируется отказ PostgreSQL или HAProxy ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +**Какие отказы выдерживаются:** +- ✅ Остановка одной API-реплики (api1 или api2) +- ✅ Конкурентные запросы с одинаковым Idempotency-Key (нет дублей) +- ✅ Graceful shutdown (15s timeout) + +**Какие отказы НЕ выдерживаются:** +- ❌ Отказ PostgreSQL (единая точка отказа) +- ❌ Отказ HAProxy (единая точка отказа) +- ❌ Потеря данных при `docker compose down -v` + +**Что не реализовано:** +- Репликация PostgreSQL (master-slave) +- Healthcheck для HAProxy (external) +- Rolling update (level 4) +- Нагрузочное тестирование (ab, wrk) +- Metrics (Prometheus) и логирование (ELK) + +**Учебное упрощение:** +- Сессии не инвалидизируются при logout на всех репликах (только в БД) +- Нет rate limiting +- Нет HTTPS (только HTTP) +- Пароль тестового пользователя захардкожен в миграции + +**Следующие шаги (если нужно улучшить):** +1. Добавить репликацию PostgreSQL (Patroni или pgpool) +2. Добавить keepalived для HAProxy (VIP) +3. Реализовать circuit breaker между репликами +4. Добавить Prometheus metrics (request_count, error_rate, latency) +5. Настроить централизованное логирование (Loki + Promtail) -- 2.54.0