feat: полная реализация

This commit is contained in:
2026-11-10 15:27:10 +03:00
parent 7734b222f8
commit 5cf99b5384
18 changed files with 935 additions and 211 deletions
+1 -2
View File
@@ -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.
+12 -5
View File
@@ -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"]
+22 -7
View File
@@ -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
+67
View File
@@ -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)
}
+345 -94
View File
@@ -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,
},
})
}
+67 -17
View File
@@ -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:
+31 -13
View File
@@ -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 <token>
Idempotency-Key: <idempotency-key>
{
"recipient": "user",
"body": "Hello, World!"
"recipient": "test",
"body": "Hello Bob"
}
GET /api/messages HTTP/1.1
Host: localhost:8080
Authorization: Bearer <token>
### 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
+8
View File
@@ -0,0 +1,8 @@
module mtusi2026lab3
go 1.22
require (
github.com/lib/pq v1.10.9
golang.org/x/crypto v0.21.0
)
+4
View File
@@ -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=
+21 -8
View File
@@ -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
+27
View File
@@ -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
}
+16
View File
@@ -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)
}
+17 -9
View File
@@ -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;
+17
View File
@@ -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
}
+20 -4
View File
@@ -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
+20 -4
View File
@@ -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
+88 -27
View File
@@ -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)
+152 -21
View File
@@ -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)