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()