forked from mtusi2026/mtusi2026lab3
Lab 2 baseline for Lab 3 #2
@@ -17,4 +17,5 @@ backups
|
||||
__pycache__
|
||||
node_modules
|
||||
evidence
|
||||
/messenger
|
||||
.DS_Store
|
||||
|
||||
@@ -22,6 +22,7 @@ dist/
|
||||
build/
|
||||
target/
|
||||
bin/
|
||||
/messenger
|
||||
obj/
|
||||
coverage/
|
||||
*.log
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
FROM golang:1.26.5-alpine3.24 AS build
|
||||
|
||||
WORKDIR /src
|
||||
COPY . .
|
||||
RUN go build ./cmd/messenger
|
||||
|
||||
FROM build AS test
|
||||
|
||||
CMD ["go", "test", "./internal/postgres", "./internal/session"]
|
||||
|
||||
FROM build AS experiment
|
||||
|
||||
CMD ["go", "run", "./cmd/lab2experiment"]
|
||||
|
||||
FROM swaggerapi/swagger-ui:v5.31.2 AS swagger
|
||||
|
||||
COPY contracts/openapi.json /spec/openapi.json
|
||||
ENV SWAGGER_JSON=/spec/openapi.json
|
||||
|
||||
FROM prom/prometheus:v3.13.3 AS prometheus
|
||||
|
||||
COPY monitoring/prometheus.yml /etc/prometheus/prometheus.yml
|
||||
|
||||
FROM grafana/grafana:13.1.6 AS grafana
|
||||
|
||||
COPY monitoring/grafana/provisioning /etc/grafana/provisioning
|
||||
COPY monitoring/grafana/dashboards /etc/grafana/dashboards
|
||||
|
||||
FROM alpine:3.24.2
|
||||
|
||||
RUN addgroup -S app && adduser -S -G app -u 10001 app
|
||||
COPY --from=build --chown=app:app /src/messenger /app/messenger
|
||||
|
||||
USER app
|
||||
WORKDIR /app
|
||||
HEALTHCHECK --interval=10s --timeout=2s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1
|
||||
CMD ["/app/messenger"]
|
||||
@@ -0,0 +1,165 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type apiClient struct {
|
||||
baseURL string
|
||||
client *http.Client
|
||||
token string
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println("PASS: PostgreSQL and Valkey outage recovery")
|
||||
}
|
||||
|
||||
func run() error {
|
||||
baseURL := flag.String("base-url", "http://localhost:8080", "API base URL")
|
||||
flag.Parse()
|
||||
client := &apiClient{baseURL: *baseURL, client: &http.Client{Timeout: 10 * time.Second}}
|
||||
if err := client.registerAndLogin(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expect(http.MethodGet, "/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := verifyOutage(client, "valkey"); err != nil {
|
||||
return err
|
||||
}
|
||||
return verifyOutage(client, "postgres")
|
||||
}
|
||||
|
||||
func (c *apiClient) registerAndLogin() error {
|
||||
username := "dep_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
||||
password := "T3st-" + uuid.NewString()
|
||||
body := map[string]string{"username": username, "display_name": "Dependency check", "password": password}
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/users", bytes.NewReader(payload))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
if err := closeResponse(response, http.StatusCreated); err != nil {
|
||||
return fmt.Errorf("register: %w", err)
|
||||
}
|
||||
|
||||
request, err = http.NewRequest(http.MethodPost, c.baseURL+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
request.SetBasicAuth(username, password)
|
||||
response, err = c.client.Do(request)
|
||||
if err != nil {
|
||||
return fmt.Errorf("login: %w", err)
|
||||
}
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
return fmt.Errorf("login status = %d", response.StatusCode)
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.NewDecoder(io.LimitReader(response.Body, 1<<20)).Decode(&session); err != nil {
|
||||
return fmt.Errorf("decode session: %w", err)
|
||||
}
|
||||
if session.Token == "" {
|
||||
return fmt.Errorf("login returned an empty token")
|
||||
}
|
||||
c.token = session.Token
|
||||
return nil
|
||||
}
|
||||
|
||||
func verifyOutage(client *apiClient, service string) (returnErr error) {
|
||||
if err := compose("stop", service); err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() {
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); returnErr == nil && err != nil {
|
||||
returnErr = err
|
||||
}
|
||||
}()
|
||||
if err := client.expectEventually("/health/ready", http.StatusServiceUnavailable, false); err != nil {
|
||||
return fmt.Errorf("%s readiness outage: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusServiceUnavailable, true); err != nil {
|
||||
return fmt.Errorf("%s business outage: %w", service, err)
|
||||
}
|
||||
if err := compose("up", "-d", "--wait", "--wait-timeout", "90", service); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := client.expectEventually("/health/ready", http.StatusOK, false); err != nil {
|
||||
return fmt.Errorf("%s readiness recovery: %w", service, err)
|
||||
}
|
||||
if err := client.expectEventually("/api/v1/users/me", http.StatusOK, true); err != nil {
|
||||
return fmt.Errorf("%s session recovery: %w", service, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func compose(arguments ...string) error {
|
||||
command := exec.Command("docker", append([]string{"compose"}, arguments...)...)
|
||||
if output, err := command.CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("docker compose %v: %w: %s", arguments, err, bytes.TrimSpace(output))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *apiClient) expectEventually(path string, status int, authenticated bool) error {
|
||||
deadline := time.Now().Add(20 * time.Second)
|
||||
var lastErr error
|
||||
for time.Now().Before(deadline) {
|
||||
lastErr = c.expect(http.MethodGet, path, status, authenticated)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
time.Sleep(200 * time.Millisecond)
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func (c *apiClient) expect(method, path string, status int, authenticated bool) error {
|
||||
request, err := http.NewRequest(method, c.baseURL+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if authenticated {
|
||||
request.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
response, err := c.client.Do(request)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return closeResponse(response, status)
|
||||
}
|
||||
|
||||
func closeResponse(response *http.Response, status int) error {
|
||||
defer func() { _ = response.Body.Close() }()
|
||||
_, _ = io.Copy(io.Discard, io.LimitReader(response.Body, 1<<20))
|
||||
if response.StatusCode != status {
|
||||
return fmt.Errorf("status = %d, want %d", response.StatusCode, status)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/config"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/logger"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/messenger"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/password"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/session"
|
||||
)
|
||||
|
||||
func main() {
|
||||
appLogger := logger.New()
|
||||
if err := run(appLogger); err != nil {
|
||||
appLogger.Error("server stopped", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run(logger *slog.Logger) error {
|
||||
settings, err := config.Load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pool, err := database.Open(context.Background(), database.Config{
|
||||
URL: settings.DatabaseURL, MinConnections: settings.DBMinConns, MaxConnections: settings.DBMaxConns,
|
||||
MaxConnectionAge: settings.DBMaxConnectionAge, MaxConnectionIdle: settings.DBMaxConnectionIdle,
|
||||
HealthCheckPeriod: settings.DBHealthCheckPeriod, ConnectTimeout: settings.DependencyTimeout,
|
||||
MigrationTimeout: settings.MigrationTimeout,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
sessions, err := session.New(settings.CacheURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = sessions.Close() }()
|
||||
passwords, err := password.New(password.Config{
|
||||
MemoryKiB: settings.PasswordMemoryKiB, Iterations: settings.PasswordIterations,
|
||||
Parallelism: settings.PasswordParallelism, SaltLength: settings.PasswordSaltLength,
|
||||
KeyLength: settings.PasswordKeyLength,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
app := messenger.NewServer(
|
||||
logger,
|
||||
postgres.New(pool),
|
||||
sessions,
|
||||
passwords,
|
||||
settings.SessionTTL,
|
||||
settings.RequestTimeout,
|
||||
settings.SwaggerPort,
|
||||
)
|
||||
server := &http.Server{
|
||||
Addr: settings.Address,
|
||||
Handler: app.Handler(),
|
||||
ReadHeaderTimeout: settings.HTTPReadHeaderTimeout,
|
||||
ReadTimeout: settings.HTTPReadTimeout,
|
||||
WriteTimeout: settings.HTTPWriteTimeout,
|
||||
IdleTimeout: settings.HTTPIdleTimeout,
|
||||
}
|
||||
listener, err := net.Listen("tcp", server.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
serveResult := make(chan error, 1)
|
||||
go func() {
|
||||
serveResult <- server.Serve(listener)
|
||||
}()
|
||||
logger.Info("server started", "address", server.Addr)
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
select {
|
||||
case err := <-serveResult:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
logger.Info("shutdown requested")
|
||||
app.SetReady(false)
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), settings.HTTPShutdownTimeout)
|
||||
defer cancel()
|
||||
if err := server.Shutdown(shutdownCtx); err != nil {
|
||||
_ = server.Close()
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17.6-alpine3.22
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
cpus: ${POSTGRES_CPUS}
|
||||
mem_limit: ${POSTGRES_MEMORY}
|
||||
restart: unless-stopped
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:8.1.4-alpine
|
||||
command: ["valkey-server", "--appendonly", "yes", "--appendfsync", "everysec", "--requirepass", "${VALKEY_PASSWORD}"]
|
||||
environment:
|
||||
VALKEY_PASSWORD: ${VALKEY_PASSWORD}
|
||||
volumes:
|
||||
- valkey_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "valkey-cli --no-auth-warning -a \"$$VALKEY_PASSWORD\" ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
cpus: ${VALKEY_CPUS}
|
||||
mem_limit: ${VALKEY_MEMORY}
|
||||
restart: unless-stopped
|
||||
|
||||
api:
|
||||
build: .
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab2}
|
||||
env_file: .env
|
||||
ports:
|
||||
- "${API_BIND_ADDRESS}:${APP_PORT}"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
cpus: ${API_CPUS}
|
||||
mem_limit: ${API_MEMORY}
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
|
||||
swagger:
|
||||
build:
|
||||
context: .
|
||||
target: swagger
|
||||
ports:
|
||||
- "${SWAGGER_BIND_ADDRESS}:8080"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
tests:
|
||||
profiles: ["tests"]
|
||||
build:
|
||||
context: .
|
||||
target: test
|
||||
environment:
|
||||
TEST_DATABASE_URL: ${DATABASE_URL}
|
||||
TEST_CACHE_URL: ${CACHE_URL}
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
|
||||
experiment:
|
||||
profiles: ["research"]
|
||||
build:
|
||||
context: .
|
||||
target: experiment
|
||||
env_file: .env
|
||||
command: ["go", "run", "./cmd/lab2experiment", "-output=/output/lab2-track2.json"]
|
||||
volumes:
|
||||
- ./evidence:/output
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
|
||||
postgres-exporter:
|
||||
image: quay.io/prometheuscommunity/postgres-exporter:v0.20.1
|
||||
environment:
|
||||
DATA_SOURCE_URI: postgres:5432/${POSTGRES_DB}?sslmode=disable
|
||||
DATA_SOURCE_USER: ${POSTGRES_USER}
|
||||
DATA_SOURCE_PASS: ${POSTGRES_PASSWORD}
|
||||
PG_EXPORTER_COLLECTION_TIMEOUT: 5s
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
valkey-exporter:
|
||||
image: oliver006/redis_exporter:v1.89.0-alpine
|
||||
environment:
|
||||
REDIS_ADDR: redis://valkey:6379
|
||||
REDIS_USER: default
|
||||
REDIS_PASSWORD: ${VALKEY_PASSWORD}
|
||||
depends_on:
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
prometheus:
|
||||
build:
|
||||
context: .
|
||||
target: prometheus
|
||||
command:
|
||||
- --config.file=/etc/prometheus/prometheus.yml
|
||||
- --storage.tsdb.path=/prometheus
|
||||
- --storage.tsdb.retention.time=7d
|
||||
volumes:
|
||||
- prometheus_data:/prometheus
|
||||
ports:
|
||||
- "${PROMETHEUS_BIND_ADDRESS}:9090"
|
||||
depends_on:
|
||||
- api
|
||||
- postgres-exporter
|
||||
- valkey-exporter
|
||||
restart: unless-stopped
|
||||
|
||||
grafana:
|
||||
build:
|
||||
context: .
|
||||
target: grafana
|
||||
environment:
|
||||
GF_SECURITY_ADMIN_USER: admin
|
||||
GF_SECURITY_ADMIN_PASSWORD: ${GRAFANA_ADMIN_PASSWORD}
|
||||
GF_USERS_ALLOW_SIGN_UP: "false"
|
||||
volumes:
|
||||
- grafana_data:/var/lib/grafana
|
||||
ports:
|
||||
- "${GRAFANA_BIND_ADDRESS}:3000"
|
||||
depends_on:
|
||||
- prometheus
|
||||
restart: unless-stopped
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
valkey_data:
|
||||
prometheus_data:
|
||||
grafana_data:
|
||||
@@ -0,0 +1,39 @@
|
||||
module gitea.privcloud.tech/dizi/mtusi2026lab2
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/prometheus/client_golang v1.23.2
|
||||
github.com/spf13/viper v1.21.0
|
||||
github.com/valkey-io/valkey-go v1.0.78
|
||||
golang.org/x/crypto v0.57.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/beorn7/perks v1.0.1 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/prometheus/client_model v0.6.2 // indirect
|
||||
github.com/prometheus/common v0.66.1 // indirect
|
||||
github.com/prometheus/procfs v0.16.1 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
google.golang.org/protobuf v1.36.8 // indirect
|
||||
)
|
||||
@@ -0,0 +1,96 @@
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
|
||||
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc=
|
||||
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/onsi/gomega v1.42.1 h1:iN1rCUX+44NZ1Dc97MPoeFYbFR0vh8zxoxMFwKdyZ6I=
|
||||
github.com/onsi/gomega v1.42.1/go.mod h1:REff/hsDsodHoKlWsP2mAPhu1+5/6hVYNf9rIEBpeSg=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/prometheus/client_golang v1.23.2 h1:Je96obch5RDVy3FDMndoUsjAhG5Edi49h0RJWRi/o0o=
|
||||
github.com/prometheus/client_golang v1.23.2/go.mod h1:Tb1a6LWHB3/SPIzCoaDXI4I8UHKeFTEQ1YCr+0Gyqmg=
|
||||
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
|
||||
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
|
||||
github.com/prometheus/common v0.66.1 h1:h5E0h5/Y8niHc5DlaLlWLArTQI7tMrsfQjHV+d9ZoGs=
|
||||
github.com/prometheus/common v0.66.1/go.mod h1:gcaUsgf3KfRSwHY4dIMXLPV0K/Wg1oZ8+SbZk/HH/dA=
|
||||
github.com/prometheus/procfs v0.16.1 h1:hZ15bTNuirocR6u0JZ6BAHHmwS1p8B4P6MRqxtzMyRg=
|
||||
github.com/prometheus/procfs v0.16.1/go.mod h1:teAbpZRB1iIAJYREa1LsoWUXykVXA1KlTmWl8x/U+Is=
|
||||
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/valkey-io/valkey-go v1.0.78 h1:1Ce8hRaaNXpKfqmqZQRhnbVh1eW8MGR873pdXVvE4y8=
|
||||
github.com/valkey-io/valkey-go v1.0.78/go.mod h1:gvC/r2m3eW4Hbj0YnjogTzNtFdPSM/D+NCqen+5OABM=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/net v0.58.0 h1:ynWG7rqYi4ccpTEuPZ2QGWHktVEM9DMCj9yzDE0Q7To=
|
||||
golang.org/x/net v0.58.0/go.mod h1:YwCddHnFlT7eLQqVprV19OnhLGtc5xOKgE0RyqgfWAU=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
google.golang.org/protobuf v1.36.8 h1:xHScyCOEuuwZEc6UtSOvPbAT4zRh0xcNRYekJwfqyMc=
|
||||
google.golang.org/protobuf v1.36.8/go.mod h1:fuxRtAxBytpl4zzqUh6/eyUujkJdNiuEkXntxiD/uRU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,209 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Address string
|
||||
DatabaseURL string
|
||||
CacheURL string
|
||||
SessionTTL time.Duration
|
||||
DBMaxConns int32
|
||||
DBMinConns int32
|
||||
DBMaxConnectionAge time.Duration
|
||||
DBMaxConnectionIdle time.Duration
|
||||
DBHealthCheckPeriod time.Duration
|
||||
DependencyTimeout time.Duration
|
||||
MigrationTimeout time.Duration
|
||||
RequestTimeout time.Duration
|
||||
HTTPReadHeaderTimeout time.Duration
|
||||
HTTPReadTimeout time.Duration
|
||||
HTTPWriteTimeout time.Duration
|
||||
HTTPIdleTimeout time.Duration
|
||||
HTTPShutdownTimeout time.Duration
|
||||
PasswordMemoryKiB uint32
|
||||
PasswordIterations uint32
|
||||
PasswordParallelism uint8
|
||||
PasswordSaltLength uint32
|
||||
PasswordKeyLength uint32
|
||||
SwaggerPort string
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
values := viper.New()
|
||||
values.AutomaticEnv()
|
||||
|
||||
host, err := requiredString(values, "APP_HOST")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
port, err := requiredPort(values, "APP_PORT")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
databaseURL, err := requiredString(values, "DATABASE_URL")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
cacheURL, err := requiredString(values, "CACHE_URL")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
store, err := requiredString(values, "SESSION_STORE")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if store != "valkey" {
|
||||
return Config{}, errors.New("SESSION_STORE must be valkey")
|
||||
}
|
||||
|
||||
sessionTTL, err := seconds(values, "SESSION_TTL_SECONDS", 1, 86400*30)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConns, err := integer(values, "DB_MAX_CONNS", 1, 100)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
minConns, err := integer(values, "DB_MIN_CONNS", 0, maxConns)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConnectionAge, err := seconds(values, "DB_MAX_CONNECTION_AGE_SECONDS", 1, 86400)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
maxConnectionIdle, err := seconds(values, "DB_MAX_CONNECTION_IDLE_SECONDS", 1, 86400)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
healthCheckPeriod, err := seconds(values, "DB_HEALTH_CHECK_SECONDS", 1, 3600)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
dependencyTimeout, err := seconds(values, "DEPENDENCY_TIMEOUT_SECONDS", 1, 60)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
migrationTimeout, err := seconds(values, "MIGRATION_TIMEOUT_SECONDS", 1, 600)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
requestTimeout, err := seconds(values, "REQUEST_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
readHeaderTimeout, err := seconds(values, "HTTP_READ_HEADER_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
readTimeout, err := seconds(values, "HTTP_READ_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
writeTimeout, err := seconds(values, "HTTP_WRITE_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
idleTimeout, err := seconds(values, "HTTP_IDLE_TIMEOUT_SECONDS", 1, 3600)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
shutdownTimeout, err := seconds(values, "HTTP_SHUTDOWN_TIMEOUT_SECONDS", 1, 300)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
passwordMemory, err := integer(values, "PASSWORD_MEMORY_KIB", 8*1024, 1024*1024)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
passwordIterations, err := integer(values, "PASSWORD_ITERATIONS", 1, 20)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
passwordParallelism, err := integer(values, "PASSWORD_PARALLELISM", 1, 255)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
passwordSaltLength, err := integer(values, "PASSWORD_SALT_LENGTH", 8, 1024)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
passwordKeyLength, err := integer(values, "PASSWORD_KEY_LENGTH", 16, 1024)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
swaggerPort, err := requiredPort(values, "SWAGGER_PORT")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
return Config{
|
||||
Address: net.JoinHostPort(host, port), DatabaseURL: databaseURL, CacheURL: cacheURL,
|
||||
SessionTTL: sessionTTL, DBMaxConns: int32(maxConns), DBMinConns: int32(minConns),
|
||||
DBMaxConnectionAge: maxConnectionAge, DBMaxConnectionIdle: maxConnectionIdle,
|
||||
DBHealthCheckPeriod: healthCheckPeriod, DependencyTimeout: dependencyTimeout,
|
||||
MigrationTimeout: migrationTimeout, RequestTimeout: requestTimeout,
|
||||
HTTPReadHeaderTimeout: readHeaderTimeout, HTTPReadTimeout: readTimeout,
|
||||
HTTPWriteTimeout: writeTimeout, HTTPIdleTimeout: idleTimeout,
|
||||
HTTPShutdownTimeout: shutdownTimeout, PasswordMemoryKiB: uint32(passwordMemory),
|
||||
PasswordIterations: uint32(passwordIterations), PasswordParallelism: uint8(passwordParallelism),
|
||||
PasswordSaltLength: uint32(passwordSaltLength), PasswordKeyLength: uint32(passwordKeyLength),
|
||||
SwaggerPort: swaggerPort,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func requiredString(values *viper.Viper, name string) (string, error) {
|
||||
value := strings.TrimSpace(values.GetString(name))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s is required", name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func requiredPort(values *viper.Viper, name string) (string, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := integerValue(value, 1, 65535); err != nil {
|
||||
return "", fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func seconds(values *viper.Viper, name string, minimum, maximum int) (time.Duration, error) {
|
||||
value, err := integer(values, name, minimum, maximum)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return time.Duration(value) * time.Second, nil
|
||||
}
|
||||
|
||||
func integer(values *viper.Viper, name string, minimum, maximum int) (int, error) {
|
||||
value, err := requiredString(values, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
parsed, err := integerValue(value, minimum, maximum)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s: %w", name, err)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
|
||||
func integerValue(value string, minimum, maximum int) (int, error) {
|
||||
parsed, err := strconv.Atoi(value)
|
||||
if err != nil || parsed < minimum || parsed > maximum {
|
||||
return 0, fmt.Errorf("must be between %d and %d", minimum, maximum)
|
||||
}
|
||||
return parsed, nil
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/migrations"
|
||||
)
|
||||
|
||||
var (
|
||||
errURLRequired = errors.New("database URL is required")
|
||||
errMinConnections = errors.New("minimum database connections must not be negative")
|
||||
errMaxConnections = errors.New("maximum database connections must be positive")
|
||||
errConnectionRange = errors.New("minimum database connections exceed maximum")
|
||||
errMaxConnectionAge = errors.New("maximum database connection age must be positive")
|
||||
errMaxConnectionIdle = errors.New("maximum database connection idle time must be positive")
|
||||
errHealthCheckPeriod = errors.New("database health check period must be positive")
|
||||
errConnectionTimeout = errors.New("database connection timeout must be positive")
|
||||
errMigrationTimeout = errors.New("database migration timeout must be positive")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
URL string
|
||||
MinConnections int32
|
||||
MaxConnections int32
|
||||
MaxConnectionAge time.Duration
|
||||
MaxConnectionIdle time.Duration
|
||||
HealthCheckPeriod time.Duration
|
||||
ConnectTimeout time.Duration
|
||||
MigrationTimeout time.Duration
|
||||
}
|
||||
|
||||
func Open(ctx context.Context, config Config) (*pgxpool.Pool, error) {
|
||||
if err := validateConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
poolConfig, err := pgxpool.ParseConfig(config.URL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse database configuration: %w", err)
|
||||
}
|
||||
poolConfig.MinConns = config.MinConnections
|
||||
poolConfig.MaxConns = config.MaxConnections
|
||||
poolConfig.MaxConnLifetime = config.MaxConnectionAge
|
||||
poolConfig.MaxConnIdleTime = config.MaxConnectionIdle
|
||||
poolConfig.HealthCheckPeriod = config.HealthCheckPeriod
|
||||
|
||||
connectCtx, cancel := context.WithTimeout(ctx, config.ConnectTimeout)
|
||||
defer cancel()
|
||||
pool, err := pgxpool.NewWithConfig(connectCtx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create database pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(connectCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
migrationCtx, cancel := context.WithTimeout(ctx, config.MigrationTimeout)
|
||||
defer cancel()
|
||||
if err := applyMigrations(migrationCtx, pool, migrations.FS); err != nil {
|
||||
pool.Close()
|
||||
return nil, err
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func validateConfig(config Config) error {
|
||||
switch {
|
||||
case config.URL == "":
|
||||
return errURLRequired
|
||||
case config.MinConnections < 0:
|
||||
return errMinConnections
|
||||
case config.MaxConnections < 1:
|
||||
return errMaxConnections
|
||||
case config.MinConnections > config.MaxConnections:
|
||||
return errConnectionRange
|
||||
case config.MaxConnectionAge <= 0:
|
||||
return errMaxConnectionAge
|
||||
case config.MaxConnectionIdle <= 0:
|
||||
return errMaxConnectionIdle
|
||||
case config.HealthCheckPeriod <= 0:
|
||||
return errHealthCheckPeriod
|
||||
case config.ConnectTimeout <= 0:
|
||||
return errConnectionTimeout
|
||||
case config.MigrationTimeout <= 0:
|
||||
return errMigrationTimeout
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
const (
|
||||
migrationLockID int64 = 0x4d54555349324c42
|
||||
migrationUnlockTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
var migrationNamePattern = regexp.MustCompile(`^(\d+)_([a-z0-9_]+)\.sql$`)
|
||||
|
||||
type migration struct {
|
||||
version int64
|
||||
name string
|
||||
checksum string
|
||||
sql string
|
||||
}
|
||||
|
||||
func applyMigrations(ctx context.Context, pool *pgxpool.Pool, migrationFS fs.FS) (returnErr error) {
|
||||
migrations, err := readMigrations(migrationFS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
connection, err := pool.Acquire(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("acquire connection for migrations: %w", err)
|
||||
}
|
||||
defer connection.Release()
|
||||
if _, err := connection.Exec(ctx, "SELECT pg_advisory_lock($1)", migrationLockID); err != nil {
|
||||
return fmt.Errorf("lock migrations: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
unlockCtx, cancel := context.WithTimeout(context.Background(), migrationUnlockTimeout)
|
||||
defer cancel()
|
||||
if _, err := connection.Exec(unlockCtx, "SELECT pg_advisory_unlock($1)", migrationLockID); err != nil {
|
||||
_ = connection.Conn().Close(unlockCtx)
|
||||
if returnErr == nil {
|
||||
returnErr = fmt.Errorf("unlock migrations: %w", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
if _, err := connection.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version BIGINT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
checksum TEXT NOT NULL,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp()
|
||||
)`); err != nil {
|
||||
return fmt.Errorf("create migration history: %w", err)
|
||||
}
|
||||
for _, migration := range migrations {
|
||||
if err := applyMigration(ctx, connection, migration); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func readMigrations(migrationFS fs.FS) ([]migration, error) {
|
||||
entries, err := fs.ReadDir(migrationFS, ".")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read embedded migrations: %w", err)
|
||||
}
|
||||
migrations := make([]migration, 0, len(entries))
|
||||
versions := make(map[int64]string)
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() || filepath.Ext(entry.Name()) != ".sql" {
|
||||
continue
|
||||
}
|
||||
matches := migrationNamePattern.FindStringSubmatch(entry.Name())
|
||||
if matches == nil {
|
||||
return nil, fmt.Errorf("invalid migration filename %q", entry.Name())
|
||||
}
|
||||
version, err := strconv.ParseInt(matches[1], 10, 64)
|
||||
if err != nil || version < 1 {
|
||||
return nil, fmt.Errorf("invalid migration version in %q", entry.Name())
|
||||
}
|
||||
if previous, exists := versions[version]; exists {
|
||||
return nil, fmt.Errorf("migration version %d is used by %q and %q", version, previous, entry.Name())
|
||||
}
|
||||
data, err := fs.ReadFile(migrationFS, entry.Name())
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read migration %q: %w", entry.Name(), err)
|
||||
}
|
||||
hash := sha256.Sum256(data)
|
||||
versions[version] = entry.Name()
|
||||
migrations = append(migrations, migration{
|
||||
version: version,
|
||||
name: entry.Name(),
|
||||
checksum: hex.EncodeToString(hash[:]),
|
||||
sql: string(data),
|
||||
})
|
||||
}
|
||||
if len(migrations) == 0 {
|
||||
return nil, errors.New("no database migrations found")
|
||||
}
|
||||
sort.Slice(migrations, func(i, j int) bool {
|
||||
return migrations[i].version < migrations[j].version
|
||||
})
|
||||
return migrations, nil
|
||||
}
|
||||
|
||||
func applyMigration(ctx context.Context, connection *pgxpool.Conn, migration migration) error {
|
||||
var checksum string
|
||||
err := connection.QueryRow(ctx, "SELECT checksum FROM schema_migrations WHERE version = $1", migration.version).Scan(&checksum)
|
||||
switch {
|
||||
case err == nil && checksum != migration.checksum:
|
||||
return fmt.Errorf("migration %q checksum does not match applied version", migration.name)
|
||||
case err == nil:
|
||||
return nil
|
||||
case !errors.Is(err, pgx.ErrNoRows):
|
||||
return fmt.Errorf("read migration %q state: %w", migration.name, err)
|
||||
}
|
||||
|
||||
transaction, err := connection.Begin(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("begin migration %q: %w", migration.name, err)
|
||||
}
|
||||
defer func() {
|
||||
_ = transaction.Rollback(context.WithoutCancel(ctx))
|
||||
}()
|
||||
if _, err := transaction.Exec(ctx, migration.sql, pgx.QueryExecModeSimpleProtocol); err != nil {
|
||||
return fmt.Errorf("apply migration %q: %w", migration.name, err)
|
||||
}
|
||||
if _, err := transaction.Exec(ctx, `
|
||||
INSERT INTO schema_migrations (version, name, checksum)
|
||||
VALUES ($1, $2, $3)`, migration.version, migration.name, migration.checksum); err != nil {
|
||||
return fmt.Errorf("record migration %q: %w", migration.name, err)
|
||||
}
|
||||
if err := transaction.Commit(ctx); err != nil {
|
||||
return fmt.Errorf("commit migration %q: %w", migration.name, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New() *slog.Logger {
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/session"
|
||||
)
|
||||
|
||||
func (s *Server) createSession(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodPost {
|
||||
return methodNotAllowed(w, http.MethodPost)
|
||||
}
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1))
|
||||
if err != nil || len(body) != 0 {
|
||||
return invalidRequest()
|
||||
}
|
||||
username, passwordValue, ok := singleBasicAuth(r)
|
||||
if !ok {
|
||||
return unauthorized()
|
||||
}
|
||||
credentials, err := s.messengerStore.UserByUsername(r.Context(), username)
|
||||
if errors.Is(err, postgres.ErrNotFound) {
|
||||
if _, verifyErr := s.passwords.Verify(passwordValue, s.passwords.DummyHash()); verifyErr != nil {
|
||||
return internalError()
|
||||
}
|
||||
return unauthorized()
|
||||
}
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
verified, err := s.passwords.Verify(passwordValue, credentials.PasswordHash)
|
||||
if err != nil {
|
||||
return internalError()
|
||||
}
|
||||
if !verified {
|
||||
return unauthorized()
|
||||
}
|
||||
token, expiresAt, err := s.sessions.Create(r.Context(), credentials.ID, s.sessionTTL)
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}{token, "Bearer", model.FormatTime(expiresAt), credentials.User})
|
||||
return nil
|
||||
}
|
||||
|
||||
func singleBasicAuth(r *http.Request) (string, string, bool) {
|
||||
if len(r.Header.Values("Authorization")) != 1 {
|
||||
return "", "", false
|
||||
}
|
||||
username, passwordValue, ok := r.BasicAuth()
|
||||
return username, passwordValue, ok && username != ""
|
||||
}
|
||||
|
||||
func (s *Server) revokeSession(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodDelete {
|
||||
return methodNotAllowed(w, http.MethodDelete)
|
||||
}
|
||||
_, token, authErr := s.authenticate(r)
|
||||
if authErr != nil {
|
||||
return authErr
|
||||
}
|
||||
if err := s.sessions.Delete(r.Context(), token); errors.Is(err, session.ErrNotFound) {
|
||||
return unauthorized()
|
||||
} else if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) authenticate(r *http.Request) (User, string, *apiError) {
|
||||
values := r.Header.Values("Authorization")
|
||||
if len(values) != 1 {
|
||||
return User{}, "", unauthorized()
|
||||
}
|
||||
scheme, token, found := strings.Cut(values[0], " ")
|
||||
if !found || !strings.EqualFold(scheme, "Bearer") || token == "" || strings.ContainsAny(token, " \t\r\n") {
|
||||
return User{}, "", unauthorized()
|
||||
}
|
||||
userID, err := s.sessions.UserID(r.Context(), token)
|
||||
if errors.Is(err, session.ErrNotFound) || errors.Is(err, session.ErrInvalidToken) {
|
||||
return User{}, "", unauthorized()
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, "", unavailable()
|
||||
}
|
||||
user, err := s.messengerStore.UserByID(r.Context(), userID)
|
||||
if errors.Is(err, postgres.ErrNotFound) {
|
||||
_ = s.sessions.Delete(r.Context(), token)
|
||||
return User{}, "", unauthorized()
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, "", unavailable()
|
||||
}
|
||||
return user, token, nil
|
||||
}
|
||||
|
||||
func authenticationChallenge(r *http.Request) string {
|
||||
if r.URL.Path == "/api/v1/auth/sessions" {
|
||||
return `Basic realm="messenger", charset="UTF-8"`
|
||||
}
|
||||
return `Bearer realm="messenger"`
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
errorCodeInvalidRequest = "invalid_request"
|
||||
errorCodeUnauthorized = "unauthorized"
|
||||
errorCodeNotFound = "not_found"
|
||||
errorCodeInternal = "internal_error"
|
||||
errorCodeUnsupportedMediaType = "unsupported_media_type"
|
||||
errorCodePayloadTooLarge = "payload_too_large"
|
||||
errorCodeUnavailable = "unavailable"
|
||||
errorCodeConflict = "conflict"
|
||||
)
|
||||
|
||||
type apiError struct {
|
||||
status int
|
||||
code string
|
||||
message string
|
||||
}
|
||||
|
||||
func methodNotAllowed(w http.ResponseWriter, allowed string) *apiError {
|
||||
w.Header().Set("Allow", allowed)
|
||||
return &apiError{http.StatusMethodNotAllowed, errorCodeInvalidRequest, "method not allowed"}
|
||||
}
|
||||
|
||||
func invalidRequest() *apiError {
|
||||
return &apiError{http.StatusBadRequest, errorCodeInvalidRequest, "invalid request"}
|
||||
}
|
||||
|
||||
func unauthorized() *apiError {
|
||||
return &apiError{http.StatusUnauthorized, errorCodeUnauthorized, "authentication required"}
|
||||
}
|
||||
|
||||
func notFound() *apiError {
|
||||
return &apiError{http.StatusNotFound, errorCodeNotFound, "resource not found"}
|
||||
}
|
||||
|
||||
func internalError() *apiError {
|
||||
return &apiError{http.StatusInternalServerError, errorCodeInternal, "internal server error"}
|
||||
}
|
||||
|
||||
func unavailable() *apiError {
|
||||
return &apiError{http.StatusServiceUnavailable, errorCodeUnavailable, "service unavailable"}
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, requestID string, err *apiError) {
|
||||
writeJSON(w, err.status, map[string]any{"error": map[string]string{
|
||||
"code": err.code, "message": err.message, "request_id": requestID,
|
||||
}})
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(value)
|
||||
}
|
||||
@@ -0,0 +1,261 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func (s *Server) live(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet)
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) readiness(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet)
|
||||
}
|
||||
if !s.ready.Load() {
|
||||
return unavailable()
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 2*time.Second)
|
||||
defer cancel()
|
||||
if err := s.messengerStore.Ping(ctx); err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
if err := s.sessions.Ping(ctx); err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) users(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodPost {
|
||||
return methodNotAllowed(w, http.MethodPost)
|
||||
}
|
||||
var input struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
if err := readJSON(w, r, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
if !usernamePattern.MatchString(input.Username) || !validText(input.DisplayName, 100) || !validPassword(input.Password) {
|
||||
return invalidRequest()
|
||||
}
|
||||
passwordHash, err := s.passwords.Hash(input.Password)
|
||||
if err != nil {
|
||||
return internalError()
|
||||
}
|
||||
user, err := s.messengerStore.CreateUser(r.Context(), input.Username, input.DisplayName, passwordHash)
|
||||
if errors.Is(err, postgres.ErrConflict) {
|
||||
return &apiError{http.StatusConflict, errorCodeConflict, "username already exists"}
|
||||
}
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, user)
|
||||
return nil
|
||||
}
|
||||
|
||||
func validPassword(value string) bool {
|
||||
length := utf8.RuneCountInString(value)
|
||||
return utf8.ValidString(value) && strings.TrimSpace(value) != "" && length >= 12 && length <= 128
|
||||
}
|
||||
|
||||
func (s *Server) me(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet)
|
||||
}
|
||||
user, _, err := s.authenticate(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
writeJSON(w, http.StatusOK, user)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) chats(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodPost && r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet+", "+http.MethodPost)
|
||||
}
|
||||
user, _, authErr := s.authenticate(r)
|
||||
if authErr != nil {
|
||||
return authErr
|
||||
}
|
||||
if r.Method == http.MethodGet {
|
||||
return s.listChats(w, r, user)
|
||||
}
|
||||
var input struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
if err := readJSON(w, r, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validText(input.Title, 100) || len(input.MemberIDs) < 1 || len(input.MemberIDs) > 100 {
|
||||
return invalidRequest()
|
||||
}
|
||||
members := make(map[string]struct{}, len(input.MemberIDs))
|
||||
for i, memberID := range input.MemberIDs {
|
||||
canonical, valid := canonicalID(memberID)
|
||||
if !valid {
|
||||
return invalidRequest()
|
||||
}
|
||||
if _, duplicate := members[canonical]; duplicate {
|
||||
return invalidRequest()
|
||||
}
|
||||
members[canonical] = struct{}{}
|
||||
input.MemberIDs[i] = canonical
|
||||
}
|
||||
if _, included := members[user.ID]; !included {
|
||||
return invalidRequest()
|
||||
}
|
||||
chat, err := s.messengerStore.CreateChat(r.Context(), input.Title, input.MemberIDs, user.ID)
|
||||
if errors.Is(err, postgres.ErrInvalidMembers) {
|
||||
return invalidRequest()
|
||||
}
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, chat)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) listChats(w http.ResponseWriter, r *http.Request, user User) *apiError {
|
||||
limit, cursor, err := pageParams(r, "chat", user.ID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, hasNext, listErr := s.messengerStore.ListChats(r.Context(), user.ID, cursorPosition(cursor), limit)
|
||||
if errors.Is(listErr, postgres.ErrInvalidCursor) {
|
||||
return invalidRequest()
|
||||
}
|
||||
if listErr != nil {
|
||||
return unavailable()
|
||||
}
|
||||
if items == nil {
|
||||
items = []Chat{}
|
||||
}
|
||||
var next *string
|
||||
if hasNext {
|
||||
last := items[len(items)-1]
|
||||
value := encodeCursor("chat", user.ID, last.CreatedAt, last.ID)
|
||||
next = &value
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Items []Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}{items, next})
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) chat(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet)
|
||||
}
|
||||
user, _, authErr := s.authenticate(r)
|
||||
if authErr != nil {
|
||||
return authErr
|
||||
}
|
||||
chatID, valid := canonicalID(strings.Split(r.URL.Path, "/")[4])
|
||||
if !valid {
|
||||
return invalidRequest()
|
||||
}
|
||||
chat, err := s.messengerStore.ChatForUser(r.Context(), chatID, user.ID)
|
||||
if errors.Is(err, postgres.ErrNotFound) {
|
||||
return notFound()
|
||||
}
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusOK, chat)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) messages(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
if r.Method != http.MethodPost && r.Method != http.MethodGet {
|
||||
return methodNotAllowed(w, http.MethodGet+", "+http.MethodPost)
|
||||
}
|
||||
user, _, authErr := s.authenticate(r)
|
||||
if authErr != nil {
|
||||
return authErr
|
||||
}
|
||||
chatID, valid := canonicalID(strings.Split(r.URL.Path, "/")[4])
|
||||
if !valid {
|
||||
return invalidRequest()
|
||||
}
|
||||
if r.Method == http.MethodGet {
|
||||
return s.listMessages(w, r, user, chatID)
|
||||
}
|
||||
var input struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
if err := readJSON(w, r, &input); err != nil {
|
||||
return err
|
||||
}
|
||||
if !validText(input.Text, 4000) {
|
||||
return invalidRequest()
|
||||
}
|
||||
message, err := s.messengerStore.CreateMessage(r.Context(), chatID, user.ID, input.Text)
|
||||
if errors.Is(err, postgres.ErrNotFound) {
|
||||
return notFound()
|
||||
}
|
||||
if err != nil {
|
||||
return unavailable()
|
||||
}
|
||||
writeJSON(w, http.StatusCreated, message)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Server) listMessages(w http.ResponseWriter, r *http.Request, user User, chatID string) *apiError {
|
||||
limit, cursor, err := pageParams(r, "message", chatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, hasNext, listErr := s.messengerStore.ListMessages(r.Context(), chatID, user.ID, cursorPosition(cursor), limit)
|
||||
if errors.Is(listErr, postgres.ErrNotFound) {
|
||||
return notFound()
|
||||
}
|
||||
if errors.Is(listErr, postgres.ErrInvalidCursor) {
|
||||
return invalidRequest()
|
||||
}
|
||||
if listErr != nil {
|
||||
return unavailable()
|
||||
}
|
||||
if items == nil {
|
||||
items = []Message{}
|
||||
}
|
||||
var next *string
|
||||
if hasNext {
|
||||
last := items[len(items)-1]
|
||||
value := encodeCursor("message", chatID, last.CreatedAt, last.ID)
|
||||
next = &value
|
||||
}
|
||||
writeJSON(w, http.StatusOK, struct {
|
||||
Items []Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}{items, next})
|
||||
return nil
|
||||
}
|
||||
|
||||
func cursorPosition(cursor *pageCursor) *model.Position {
|
||||
if cursor == nil {
|
||||
return nil
|
||||
}
|
||||
return &model.Position{CreatedAt: cursor.Time, ID: cursor.ID}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
type MessengerStore interface {
|
||||
Ping(context.Context) error
|
||||
CreateUser(context.Context, string, string, string) (model.User, error)
|
||||
UserByID(context.Context, string) (model.User, error)
|
||||
UserByUsername(context.Context, string) (model.UserCredentials, error)
|
||||
CreateChat(context.Context, string, []string, string) (model.Chat, error)
|
||||
ChatForUser(context.Context, string, string) (model.Chat, error)
|
||||
ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error)
|
||||
CreateMessage(context.Context, string, string, string) (model.Message, error)
|
||||
ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error)
|
||||
}
|
||||
|
||||
type SessionStore interface {
|
||||
Ping(context.Context) error
|
||||
Create(context.Context, string, time.Duration) (string, time.Time, error)
|
||||
UserID(context.Context, string) (string, error)
|
||||
Delete(context.Context, string) error
|
||||
}
|
||||
|
||||
type PasswordService interface {
|
||||
Hash(string) (string, error)
|
||||
Verify(string, string) (bool, error)
|
||||
DummyHash() string
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/collectors"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
type metrics struct {
|
||||
requests *prometheus.CounterVec
|
||||
duration *prometheus.HistogramVec
|
||||
inFlight prometheus.Gauge
|
||||
handler http.Handler
|
||||
}
|
||||
|
||||
func newMetrics() *metrics {
|
||||
registry := prometheus.NewRegistry()
|
||||
result := &metrics{
|
||||
requests: prometheus.NewCounterVec(prometheus.CounterOpts{
|
||||
Namespace: "messenger",
|
||||
Subsystem: "http",
|
||||
Name: "requests_total",
|
||||
Help: "Completed HTTP requests.",
|
||||
}, []string{"method", "route", "status_class"}),
|
||||
duration: prometheus.NewHistogramVec(prometheus.HistogramOpts{
|
||||
Namespace: "messenger",
|
||||
Subsystem: "http",
|
||||
Name: "request_duration_seconds",
|
||||
Help: "HTTP request duration in seconds.",
|
||||
Buckets: prometheus.DefBuckets,
|
||||
}, []string{"method", "route"}),
|
||||
inFlight: prometheus.NewGauge(prometheus.GaugeOpts{
|
||||
Namespace: "messenger",
|
||||
Subsystem: "http",
|
||||
Name: "requests_in_flight",
|
||||
Help: "HTTP requests currently being served.",
|
||||
}),
|
||||
}
|
||||
registry.MustRegister(
|
||||
result.requests,
|
||||
result.duration,
|
||||
result.inFlight,
|
||||
collectors.NewGoCollector(),
|
||||
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
|
||||
)
|
||||
result.handler = promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
|
||||
return result
|
||||
}
|
||||
|
||||
func (m *metrics) observe(r *http.Request, status int, started time.Time) {
|
||||
route := routeTemplate(r.URL.Path)
|
||||
m.requests.WithLabelValues(r.Method, route, strconv.Itoa(status/100)+"xx").Inc()
|
||||
m.duration.WithLabelValues(r.Method, route).Observe(time.Since(started).Seconds())
|
||||
}
|
||||
|
||||
func routeTemplate(path string) string {
|
||||
switch path {
|
||||
case "/health/live", "/health/ready", "/metrics", "/api/v1/users", "/api/v1/users/me",
|
||||
"/api/v1/auth/sessions", "/api/v1/auth/sessions/current", "/api/v1/chats":
|
||||
return path
|
||||
}
|
||||
parts := strings.Split(strings.Trim(path, "/"), "/")
|
||||
if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" {
|
||||
return "/api/v1/chats/{chat_id}"
|
||||
}
|
||||
if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" {
|
||||
return "/api/v1/chats/{chat_id}/messages"
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User = model.User
|
||||
type Chat = model.Chat
|
||||
type Message = model.Message
|
||||
|
||||
func newID() (string, error) {
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id.String(), nil
|
||||
}
|
||||
|
||||
func canonicalID(value string) (string, bool) {
|
||||
if len(value) != 36 {
|
||||
return "", false
|
||||
}
|
||||
id, err := uuid.Parse(value)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return id.String(), true
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const maxRequestBytes = 1 << 20
|
||||
|
||||
func readJSON(w http.ResponseWriter, r *http.Request, target any) *apiError {
|
||||
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if err != nil || mediaType != "application/json" || (params["charset"] != "" && !strings.EqualFold(params["charset"], "utf-8")) {
|
||||
return &apiError{http.StatusUnsupportedMediaType, errorCodeUnsupportedMediaType, "expected application/json"}
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxRequestBytes)
|
||||
data, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
return &apiError{http.StatusRequestEntityTooLarge, errorCodePayloadTooLarge, "request body too large"}
|
||||
}
|
||||
return invalidRequest()
|
||||
}
|
||||
if !utf8.Valid(data) {
|
||||
return invalidRequest()
|
||||
}
|
||||
var object map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &object); err != nil || object == nil {
|
||||
return invalidRequest()
|
||||
}
|
||||
normalized, err := json.Marshal(object)
|
||||
if err != nil {
|
||||
return invalidRequest()
|
||||
}
|
||||
decoder := json.NewDecoder(bytes.NewReader(normalized))
|
||||
decoder.DisallowUnknownFields()
|
||||
if err := decoder.Decode(target); err != nil {
|
||||
return invalidRequest()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validText(value string, maxRunes int) bool {
|
||||
return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= maxRunes
|
||||
}
|
||||
|
||||
type pageCursor struct {
|
||||
Kind string `json:"kind"`
|
||||
Resource string `json:"resource"`
|
||||
Time time.Time `json:"time"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
func pageParams(r *http.Request, kind, resource string) (int, *pageCursor, *apiError) {
|
||||
query, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return 0, nil, invalidRequest()
|
||||
}
|
||||
for key := range query {
|
||||
if key != "limit" && key != "cursor" {
|
||||
return 0, nil, invalidRequest()
|
||||
}
|
||||
if len(query[key]) != 1 {
|
||||
return 0, nil, invalidRequest()
|
||||
}
|
||||
}
|
||||
limit := 50
|
||||
if value, present := query["limit"]; present {
|
||||
parsed, err := strconv.Atoi(value[0])
|
||||
if err != nil || parsed < 1 || parsed > 100 {
|
||||
return 0, nil, invalidRequest()
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
if value, present := query["cursor"]; present {
|
||||
cursor, valid := decodeCursor(value[0], kind, resource)
|
||||
if !valid {
|
||||
return 0, nil, invalidRequest()
|
||||
}
|
||||
return limit, cursor, nil
|
||||
}
|
||||
return limit, nil, nil
|
||||
}
|
||||
|
||||
func encodeCursor(kind, resource string, createdAt time.Time, id string) string {
|
||||
data, _ := json.Marshal(pageCursor{kind, resource, createdAt, id})
|
||||
return base64.RawURLEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
func decodeCursor(value, kind, resource string) (*pageCursor, bool) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
var cursor pageCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil || cursor.Kind != kind || cursor.Resource != resource {
|
||||
return nil, false
|
||||
}
|
||||
if cursor.Time.IsZero() {
|
||||
return nil, false
|
||||
}
|
||||
if _, valid := canonicalID(cursor.ID); !valid {
|
||||
return nil, false
|
||||
}
|
||||
return &cursor, true
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type requestHandler func(http.ResponseWriter, *http.Request) *apiError
|
||||
|
||||
func (s *Server) route(w http.ResponseWriter, r *http.Request) *apiError {
|
||||
var handler requestHandler
|
||||
switch r.URL.Path {
|
||||
case "/health/live":
|
||||
handler = s.live
|
||||
case "/health/ready":
|
||||
handler = s.readiness
|
||||
case "/metrics":
|
||||
s.metrics.handler.ServeHTTP(w, r)
|
||||
return nil
|
||||
case "/api/v1/users":
|
||||
handler = s.users
|
||||
case "/api/v1/users/me":
|
||||
handler = s.me
|
||||
case "/api/v1/auth/sessions":
|
||||
handler = s.createSession
|
||||
case "/api/v1/auth/sessions/current":
|
||||
handler = s.revokeSession
|
||||
case "/api/v1/chats":
|
||||
handler = s.chats
|
||||
default:
|
||||
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
||||
if len(parts) == 4 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" {
|
||||
handler = s.chat
|
||||
} else if len(parts) == 5 && parts[0] == "api" && parts[1] == "v1" && parts[2] == "chats" && parts[4] == "messages" {
|
||||
handler = s.messages
|
||||
} else {
|
||||
return notFound()
|
||||
}
|
||||
}
|
||||
return handler(w, r)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
messengerStore MessengerStore
|
||||
sessions SessionStore
|
||||
passwords PasswordService
|
||||
sessionTTL time.Duration
|
||||
requestTTL time.Duration
|
||||
logger *slog.Logger
|
||||
swaggerPort string
|
||||
metrics *metrics
|
||||
ready atomic.Bool
|
||||
}
|
||||
|
||||
type statusWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (w *statusWriter) WriteHeader(status int) {
|
||||
w.status = status
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func NewServer(logger *slog.Logger, messages MessengerStore, sessions SessionStore, passwords PasswordService, sessionTTL, requestTTL time.Duration, swaggerPort string) *Server {
|
||||
s := &Server{
|
||||
messengerStore: messages, sessions: sessions, passwords: passwords,
|
||||
sessionTTL: sessionTTL, requestTTL: requestTTL, logger: logger, swaggerPort: swaggerPort, metrics: newMetrics(),
|
||||
}
|
||||
s.ready.Store(true)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Server) SetReady(ready bool) {
|
||||
s.ready.Store(ready)
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
return http.HandlerFunc(s.serve)
|
||||
}
|
||||
|
||||
func (s *Server) serve(w http.ResponseWriter, r *http.Request) {
|
||||
requestID, err := newID()
|
||||
if err != nil {
|
||||
http.Error(w, "server unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
wrapped := &statusWriter{ResponseWriter: w, status: http.StatusOK}
|
||||
started := time.Now()
|
||||
s.metrics.inFlight.Inc()
|
||||
defer func() {
|
||||
s.metrics.inFlight.Dec()
|
||||
if recovered := recover(); recovered != nil {
|
||||
s.logger.Error("request panic", "request_id", requestID, "method", r.Method, "path", r.URL.Path)
|
||||
if wrapped.status == http.StatusOK {
|
||||
writeError(wrapped, requestID, internalError())
|
||||
}
|
||||
}
|
||||
s.metrics.observe(r, wrapped.status, started)
|
||||
s.logger.Info("request", "request_id", requestID, "method", r.Method, "path", r.URL.Path, "status", wrapped.status, "duration_ms", time.Since(started).Milliseconds())
|
||||
}()
|
||||
if s.allowSwaggerOrigin(wrapped, r) && r.Method == http.MethodOptions && r.Header.Get("Access-Control-Request-Method") != "" {
|
||||
wrapped.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS")
|
||||
wrapped.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type")
|
||||
wrapped.WriteHeader(http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), s.requestTTL)
|
||||
defer cancel()
|
||||
r = r.WithContext(ctx)
|
||||
if apiErr := s.route(wrapped, r); apiErr != nil {
|
||||
if apiErr.status == http.StatusUnauthorized {
|
||||
wrapped.Header().Set("WWW-Authenticate", authenticationChallenge(r))
|
||||
}
|
||||
writeError(wrapped, requestID, apiErr)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) allowSwaggerOrigin(w http.ResponseWriter, r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin != "http://localhost:"+s.swaggerPort && origin != "http://127.0.0.1:"+s.swaggerPort {
|
||||
return false
|
||||
}
|
||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
||||
w.Header().Set("Access-Control-Expose-Headers", "X-Request-Id")
|
||||
w.Header().Add("Vary", "Origin")
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,173 @@
|
||||
package messenger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
var errDependency = errors.New("dependency unavailable")
|
||||
|
||||
type messengerStoreStub struct {
|
||||
pingErr error
|
||||
userByID model.User
|
||||
userByIDErr error
|
||||
credentials model.UserCredentials
|
||||
credentialsErr error
|
||||
}
|
||||
|
||||
func (s messengerStoreStub) Ping(context.Context) error { return s.pingErr }
|
||||
func (messengerStoreStub) CreateUser(context.Context, string, string, string) (model.User, error) {
|
||||
return model.User{}, nil
|
||||
}
|
||||
func (s messengerStoreStub) UserByID(context.Context, string) (model.User, error) {
|
||||
return s.userByID, s.userByIDErr
|
||||
}
|
||||
func (s messengerStoreStub) UserByUsername(context.Context, string) (model.UserCredentials, error) {
|
||||
return s.credentials, s.credentialsErr
|
||||
}
|
||||
func (messengerStoreStub) CreateChat(context.Context, string, []string, string) (model.Chat, error) {
|
||||
return model.Chat{}, nil
|
||||
}
|
||||
func (messengerStoreStub) ChatForUser(context.Context, string, string) (model.Chat, error) {
|
||||
return model.Chat{}, nil
|
||||
}
|
||||
func (messengerStoreStub) ListChats(context.Context, string, *model.Position, int) ([]model.Chat, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
func (messengerStoreStub) CreateMessage(context.Context, string, string, string) (model.Message, error) {
|
||||
return model.Message{}, nil
|
||||
}
|
||||
func (messengerStoreStub) ListMessages(context.Context, string, string, *model.Position, int) ([]model.Message, bool, error) {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
type sessionStub struct {
|
||||
pingErr error
|
||||
userID string
|
||||
userIDErr error
|
||||
}
|
||||
|
||||
func (s sessionStub) Ping(context.Context) error { return s.pingErr }
|
||||
func (sessionStub) Create(context.Context, string, time.Duration) (string, time.Time, error) {
|
||||
return "token", time.Now().Add(time.Hour), nil
|
||||
}
|
||||
func (s sessionStub) UserID(context.Context, string) (string, error) { return s.userID, s.userIDErr }
|
||||
func (sessionStub) Delete(context.Context, string) error { return nil }
|
||||
|
||||
type passwordsStub struct {
|
||||
verified bool
|
||||
err error
|
||||
}
|
||||
|
||||
func (passwordsStub) Hash(string) (string, error) { return "hash", nil }
|
||||
func (passwordsStub) DummyHash() string { return "hash" }
|
||||
func (s passwordsStub) Verify(string, string) (bool, error) {
|
||||
return s.verified, s.err
|
||||
}
|
||||
|
||||
func TestReadinessReturnsUnavailableForDependencyFailure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
store messengerStoreStub
|
||||
sessions sessionStub
|
||||
}{
|
||||
{name: "postgres", store: messengerStoreStub{pingErr: errDependency}},
|
||||
{name: "valkey", sessions: sessionStub{pingErr: errDependency}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := newTestServer(tt.store, tt.sessions, passwordsStub{})
|
||||
response := serveRequest(server, http.MethodGet, "/health/ready", "")
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBusinessRequestReturnsUnavailableForDependencyFailure(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
store messengerStoreStub
|
||||
sessions sessionStub
|
||||
}{
|
||||
{name: "valkey", sessions: sessionStub{userIDErr: errDependency}},
|
||||
{name: "postgres", store: messengerStoreStub{userByIDErr: errDependency}, sessions: sessionStub{userID: "user-id"}},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
server := newTestServer(tt.store, tt.sessions, passwordsStub{})
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
request.Header.Set("Authorization", "Bearer valid-token")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusServiceUnavailable {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusServiceUnavailable)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticationChallenges(t *testing.T) {
|
||||
server := newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{})
|
||||
bearer := serveRequest(server, http.MethodGet, "/api/v1/users/me", "")
|
||||
if bearer.Code != http.StatusUnauthorized || bearer.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
||||
t.Fatalf("business challenge = %d %q", bearer.Code, bearer.Header().Get("WWW-Authenticate"))
|
||||
}
|
||||
|
||||
store := messengerStoreStub{credentials: model.UserCredentials{PasswordHash: "hash"}}
|
||||
server = newTestServer(store, sessionStub{}, passwordsStub{verified: false})
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
request.SetBasicAuth("user", "wrong-password")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusUnauthorized || response.Header().Get("WWW-Authenticate") != `Basic realm="messenger", charset="UTF-8"` {
|
||||
t.Fatalf("session challenge = %d %q", response.Code, response.Header().Get("WWW-Authenticate"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionCreationRejectsChunkedBody(t *testing.T) {
|
||||
server := newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{})
|
||||
request := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", strings.NewReader("{}"))
|
||||
request.ContentLength = -1
|
||||
request.SetBasicAuth("user", "password")
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegistrationRejectsWhitespacePassword(t *testing.T) {
|
||||
server := newTestServer(messengerStoreStub{}, sessionStub{}, passwordsStub{})
|
||||
response := serveRequest(server, http.MethodPost, "/api/v1/users", `{"username":"student","display_name":"Student","password":" "}`)
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf("status = %d, want %d", response.Code, http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func newTestServer(messages MessengerStore, sessions SessionStore, passwords PasswordService) *Server {
|
||||
logger := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return NewServer(logger, messages, sessions, passwords, time.Hour, time.Second, "8081")
|
||||
}
|
||||
|
||||
func serveRequest(server *Server, method, path, body string) *httptest.ResponseRecorder {
|
||||
request := httptest.NewRequest(method, path, strings.NewReader(body))
|
||||
if body != "" {
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
server.Handler().ServeHTTP(response, request)
|
||||
return response
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
)
|
||||
|
||||
const timestampLayout = "2006-01-02T15:04:05.000000Z"
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserCredentials struct {
|
||||
User
|
||||
PasswordHash string `json:"-"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
CreatedAt time.Time
|
||||
ID string
|
||||
}
|
||||
|
||||
type userJSON struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type chatJSON struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type messageJSON struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func (u User) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(userJSON{u.ID, u.Username, u.DisplayName, formatTime(u.CreatedAt)})
|
||||
}
|
||||
|
||||
func (c Chat) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(chatJSON{c.ID, c.Title, c.MemberIDs, formatTime(c.CreatedAt)})
|
||||
}
|
||||
|
||||
func (m Message) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(messageJSON{m.ID, m.ChatID, m.SenderID, m.Text, formatTime(m.CreatedAt)})
|
||||
}
|
||||
|
||||
func FormatTime(value time.Time) string {
|
||||
return formatTime(value)
|
||||
}
|
||||
|
||||
func formatTime(value time.Time) string {
|
||||
return value.UTC().Format(timestampLayout)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUserTimestampHasFixedMicroseconds(t *testing.T) {
|
||||
createdAt := time.Date(2026, time.September, 23, 21, 28, 15, 423030000, time.UTC)
|
||||
data, err := json.Marshal(User{ID: "id", Username: "user", DisplayName: "User", CreatedAt: createdAt})
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal() error = %v", err)
|
||||
}
|
||||
if string(data) != `{"id":"id","username":"user","display_name":"User","created_at":"2026-09-23T21:28:15.423030Z"}` {
|
||||
t.Fatalf("Marshal() = %s", data)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package password
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidConfig = errors.New("invalid password configuration")
|
||||
ErrInvalidHash = errors.New("invalid password hash")
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
MemoryKiB uint32
|
||||
Iterations uint32
|
||||
Parallelism uint8
|
||||
SaltLength uint32
|
||||
KeyLength uint32
|
||||
}
|
||||
|
||||
type Hasher struct {
|
||||
config Config
|
||||
dummyHash string
|
||||
}
|
||||
|
||||
func New(config Config) (*Hasher, error) {
|
||||
if err := validateConfig(config); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
salt := make([]byte, config.SaltLength)
|
||||
digest := argon2.IDKey([]byte("invalid-user-password"), salt, config.Iterations, config.MemoryKiB, config.Parallelism, config.KeyLength)
|
||||
return &Hasher{config: config, dummyHash: encode(config, salt, digest)}, nil
|
||||
}
|
||||
|
||||
func validateConfig(config Config) error {
|
||||
if config.MemoryKiB < 8*1024 || config.MemoryKiB > 1024*1024 ||
|
||||
config.Iterations < 1 || config.Iterations > 20 ||
|
||||
config.Parallelism < 1 ||
|
||||
config.SaltLength < 8 || config.SaltLength > 1024 ||
|
||||
config.KeyLength < 16 || config.KeyLength > 1024 {
|
||||
return ErrInvalidConfig
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (h *Hasher) Hash(value string) (string, error) {
|
||||
salt := make([]byte, h.config.SaltLength)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", fmt.Errorf("generate password salt: %w", err)
|
||||
}
|
||||
digest := argon2.IDKey([]byte(value), salt, h.config.Iterations, h.config.MemoryKiB, h.config.Parallelism, h.config.KeyLength)
|
||||
return encode(h.config, salt, digest), nil
|
||||
}
|
||||
|
||||
func (h *Hasher) Verify(value, encodedHash string) (bool, error) {
|
||||
config, salt, digest, err := decode(encodedHash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
actual := argon2.IDKey([]byte(value), salt, config.Iterations, config.MemoryKiB, config.Parallelism, config.KeyLength)
|
||||
return subtle.ConstantTimeCompare(actual, digest) == 1, nil
|
||||
}
|
||||
|
||||
func (h *Hasher) DummyHash() string {
|
||||
return h.dummyHash
|
||||
}
|
||||
|
||||
func encode(config Config, salt, digest []byte) string {
|
||||
return fmt.Sprintf(
|
||||
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version,
|
||||
config.MemoryKiB,
|
||||
config.Iterations,
|
||||
config.Parallelism,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(digest),
|
||||
)
|
||||
}
|
||||
|
||||
func decode(encodedHash string) (Config, []byte, []byte, error) {
|
||||
parts := strings.Split(encodedHash, "$")
|
||||
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
|
||||
parameters := strings.Split(parts[3], ",")
|
||||
if len(parameters) != 3 {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
memory, err := parameter(parameters[0], "m=", 32)
|
||||
if err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
iterations, err := parameter(parameters[1], "t=", 32)
|
||||
if err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
parallelism, err := parameter(parameters[2], "p=", 8)
|
||||
if err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.Strict().DecodeString(parts[4])
|
||||
if err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
digest, err := base64.RawStdEncoding.Strict().DecodeString(parts[5])
|
||||
if err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
config := Config{
|
||||
MemoryKiB: uint32(memory), Iterations: uint32(iterations), Parallelism: uint8(parallelism),
|
||||
SaltLength: uint32(len(salt)), KeyLength: uint32(len(digest)),
|
||||
}
|
||||
if err := validateConfig(config); err != nil {
|
||||
return Config{}, nil, nil, ErrInvalidHash
|
||||
}
|
||||
return config, salt, digest, nil
|
||||
}
|
||||
|
||||
func parameter(value, prefix string, bitSize int) (uint64, error) {
|
||||
if !strings.HasPrefix(value, prefix) {
|
||||
return 0, ErrInvalidHash
|
||||
}
|
||||
return strconv.ParseUint(strings.TrimPrefix(value, prefix), 10, bitSize)
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package password
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestHasher(t *testing.T) {
|
||||
hasher, err := New(testConfig())
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
hash, err := hasher.Hash("T3st-password-123")
|
||||
if err != nil {
|
||||
t.Fatalf("Hash() error = %v", err)
|
||||
}
|
||||
|
||||
verified, err := hasher.Verify("T3st-password-123", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify() error = %v", err)
|
||||
}
|
||||
if !verified {
|
||||
t.Fatal("Verify() = false for the original password")
|
||||
}
|
||||
|
||||
verified, err = hasher.Verify("wrong-password", hash)
|
||||
if err != nil {
|
||||
t.Fatalf("Verify() error = %v", err)
|
||||
}
|
||||
if verified {
|
||||
t.Fatal("Verify() = true for a different password")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsMalformedHash(t *testing.T) {
|
||||
hasher, err := New(testConfig())
|
||||
if err != nil {
|
||||
t.Fatalf("New() error = %v", err)
|
||||
}
|
||||
if _, err := hasher.Verify("password", "invalid"); err == nil {
|
||||
t.Fatal("Verify() error = nil for malformed hash")
|
||||
}
|
||||
}
|
||||
|
||||
func testConfig() Config {
|
||||
return Config{MemoryKiB: 8 * 1024, Iterations: 1, Parallelism: 1, SaltLength: 16, KeyLength: 32}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package postgres
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrInvalidMembers = errors.New("invalid members")
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
)
|
||||
@@ -0,0 +1,469 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/model"
|
||||
)
|
||||
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool) *Store {
|
||||
return &Store{pool: pool}
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
return s.pool.Ping(ctx)
|
||||
}
|
||||
|
||||
func (s *Store) CreateUser(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
displayName string,
|
||||
passwordHash string,
|
||||
) (model.User, error) {
|
||||
const query = `
|
||||
INSERT INTO users (id, username, display_name, password_hash)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, username, display_name, created_at`
|
||||
|
||||
var user model.User
|
||||
err := s.pool.QueryRow(ctx, query, uuid.NewString(), username, displayName, passwordHash).Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.DisplayName,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
if isUniqueViolation(err) {
|
||||
return model.User{}, ErrConflict
|
||||
}
|
||||
if err != nil {
|
||||
return model.User{}, fmt.Errorf("create user: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Store) UserByID(ctx context.Context, id string) (model.User, error) {
|
||||
const query = `
|
||||
SELECT id, username, display_name, created_at
|
||||
FROM users
|
||||
WHERE id = $1`
|
||||
|
||||
var user model.User
|
||||
err := s.pool.QueryRow(ctx, query, id).Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.DisplayName,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.User{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.User{}, fmt.Errorf("get user by id: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *Store) UserByUsername(ctx context.Context, username string) (model.UserCredentials, error) {
|
||||
const query = `
|
||||
SELECT id, username, display_name, created_at, password_hash
|
||||
FROM users
|
||||
WHERE username = $1`
|
||||
|
||||
var credentials model.UserCredentials
|
||||
err := s.pool.QueryRow(ctx, query, username).Scan(
|
||||
&credentials.ID,
|
||||
&credentials.Username,
|
||||
&credentials.DisplayName,
|
||||
&credentials.CreatedAt,
|
||||
&credentials.PasswordHash,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.UserCredentials{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.UserCredentials{}, fmt.Errorf("get user by username: %w", err)
|
||||
}
|
||||
|
||||
return credentials, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateChat(
|
||||
ctx context.Context,
|
||||
title string,
|
||||
memberIDs []string,
|
||||
callerID string,
|
||||
) (model.Chat, error) {
|
||||
if !validMembers(memberIDs, callerID) {
|
||||
return model.Chat{}, ErrInvalidMembers
|
||||
}
|
||||
|
||||
tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return model.Chat{}, fmt.Errorf("begin create chat transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
|
||||
valid, err := allUsersExist(ctx, tx, memberIDs)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
if !valid {
|
||||
return model.Chat{}, ErrInvalidMembers
|
||||
}
|
||||
|
||||
chat, err := insertChat(ctx, tx, title)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
if err := insertMembers(ctx, tx, chat.ID, memberIDs); err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Chat{}, fmt.Errorf("commit create chat transaction: %w", err)
|
||||
}
|
||||
|
||||
chat.MemberIDs = append([]string(nil), memberIDs...)
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func validMembers(memberIDs []string, callerID string) bool {
|
||||
callerUUID, err := uuid.Parse(callerID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
||||
seen := make(map[uuid.UUID]struct{}, len(memberIDs))
|
||||
callerIncluded := false
|
||||
for _, memberID := range memberIDs {
|
||||
memberUUID, err := uuid.Parse(memberID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
if _, exists := seen[memberUUID]; exists {
|
||||
return false
|
||||
}
|
||||
|
||||
seen[memberUUID] = struct{}{}
|
||||
callerIncluded = callerIncluded || memberUUID == callerUUID
|
||||
}
|
||||
|
||||
return callerIncluded
|
||||
}
|
||||
|
||||
func allUsersExist(ctx context.Context, tx pgx.Tx, memberIDs []string) (bool, error) {
|
||||
const query = `
|
||||
SELECT count(*)
|
||||
FROM users
|
||||
WHERE id::text = ANY($1)`
|
||||
|
||||
var count int
|
||||
if err := tx.QueryRow(ctx, query, memberIDs).Scan(&count); err != nil {
|
||||
return false, fmt.Errorf("check chat members: %w", err)
|
||||
}
|
||||
|
||||
return count == len(memberIDs), nil
|
||||
}
|
||||
|
||||
func insertChat(ctx context.Context, tx pgx.Tx, title string) (model.Chat, error) {
|
||||
const query = `
|
||||
INSERT INTO chats (id, title)
|
||||
VALUES ($1, $2)
|
||||
RETURNING id, title, created_at`
|
||||
|
||||
var chat model.Chat
|
||||
if err := tx.QueryRow(ctx, query, uuid.NewString(), title).Scan(&chat.ID, &chat.Title, &chat.CreatedAt); err != nil {
|
||||
return model.Chat{}, fmt.Errorf("insert chat: %w", err)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func insertMembers(ctx context.Context, tx pgx.Tx, chatID string, memberIDs []string) error {
|
||||
const query = `
|
||||
INSERT INTO chat_members (chat_id, user_id, position)
|
||||
VALUES ($1, $2, $3)`
|
||||
|
||||
batch := &pgx.Batch{}
|
||||
for position, memberID := range memberIDs {
|
||||
batch.Queue(query, chatID, memberID, position)
|
||||
}
|
||||
|
||||
results := tx.SendBatch(ctx, batch)
|
||||
defer func() { _ = results.Close() }()
|
||||
for range memberIDs {
|
||||
if _, err := results.Exec(); err != nil {
|
||||
return fmt.Errorf("insert chat member: %w", err)
|
||||
}
|
||||
}
|
||||
if err := results.Close(); err != nil {
|
||||
return fmt.Errorf("close chat members batch: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) ChatForUser(ctx context.Context, chatID, userID string) (model.Chat, error) {
|
||||
const query = `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
array_agg(cm.user_id::text ORDER BY cm.position)
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_members visible
|
||||
WHERE visible.chat_id = c.id AND visible.user_id = $2
|
||||
)
|
||||
GROUP BY c.id, c.title, c.created_at`
|
||||
|
||||
var chat model.Chat
|
||||
err := s.pool.QueryRow(ctx, query, chatID, userID).Scan(
|
||||
&chat.ID,
|
||||
&chat.Title,
|
||||
&chat.CreatedAt,
|
||||
&chat.MemberIDs,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.Chat{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Chat{}, fmt.Errorf("get chat for user: %w", err)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListChats(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
after *model.Position,
|
||||
limit int,
|
||||
) ([]model.Chat, bool, error) {
|
||||
if after != nil {
|
||||
valid, err := s.visibleChatPositionExists(ctx, userID, *after)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if !valid {
|
||||
return nil, false, ErrInvalidCursor
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
array_agg(all_members.user_id::text ORDER BY all_members.position)
|
||||
FROM chats c
|
||||
JOIN chat_members visible ON visible.chat_id = c.id AND visible.user_id = $1
|
||||
JOIN chat_members all_members ON all_members.chat_id = c.id
|
||||
WHERE ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2, $3::uuid))
|
||||
GROUP BY c.id, c.title, c.created_at
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4`
|
||||
|
||||
var afterTime any
|
||||
var afterID any
|
||||
if after != nil {
|
||||
afterTime = after.CreatedAt
|
||||
afterID = after.ID
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, query, userID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("list chats: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
chats, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Chat](func(row pgx.CollectableRow) (model.Chat, error) {
|
||||
var chat model.Chat
|
||||
err := row.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs)
|
||||
return chat, err
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("scan chats: %w", err)
|
||||
}
|
||||
|
||||
return page(chats, limit)
|
||||
}
|
||||
|
||||
func (s *Store) visibleChatPositionExists(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
position model.Position,
|
||||
) (bool, error) {
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM chats c
|
||||
JOIN chat_members cm ON cm.chat_id = c.id
|
||||
WHERE cm.user_id = $1 AND c.created_at = $2 AND c.id = $3
|
||||
)`
|
||||
|
||||
var exists bool
|
||||
if err := s.pool.QueryRow(ctx, query, userID, position.CreatedAt, position.ID).Scan(&exists); err != nil {
|
||||
return false, fmt.Errorf("validate chat cursor: %w", err)
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (s *Store) CreateMessage(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
senderID string,
|
||||
text string,
|
||||
) (model.Message, error) {
|
||||
const query = `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text)
|
||||
SELECT $1, $2, $3, $4
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = $2 AND user_id = $3
|
||||
)
|
||||
RETURNING id, chat_id, sender_id, text, created_at`
|
||||
|
||||
var message model.Message
|
||||
err := s.pool.QueryRow(ctx, query, uuid.NewString(), chatID, senderID, text).Scan(
|
||||
&message.ID,
|
||||
&message.ChatID,
|
||||
&message.SenderID,
|
||||
&message.Text,
|
||||
&message.CreatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return model.Message{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Message{}, fmt.Errorf("create message: %w", err)
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListMessages(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
userID string,
|
||||
after *model.Position,
|
||||
limit int,
|
||||
) ([]model.Message, bool, error) {
|
||||
visible, err := s.chatVisible(ctx, chatID, userID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if !visible {
|
||||
return nil, false, ErrNotFound
|
||||
}
|
||||
if after != nil {
|
||||
valid, err := s.messagePositionExists(ctx, chatID, *after)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if !valid {
|
||||
return nil, false, ErrInvalidCursor
|
||||
}
|
||||
}
|
||||
|
||||
const query = `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND ($2::timestamptz IS NULL OR (created_at, id) > ($2, $3::uuid))
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4`
|
||||
|
||||
var afterTime any
|
||||
var afterID any
|
||||
if after != nil {
|
||||
afterTime = after.CreatedAt
|
||||
afterID = after.ID
|
||||
}
|
||||
|
||||
rows, err := s.pool.Query(ctx, query, chatID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("list messages: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
messages, err := pgx.CollectRows(rows, pgx.RowToFunc[model.Message](func(row pgx.CollectableRow) (model.Message, error) {
|
||||
var message model.Message
|
||||
err := row.Scan(
|
||||
&message.ID,
|
||||
&message.ChatID,
|
||||
&message.SenderID,
|
||||
&message.Text,
|
||||
&message.CreatedAt,
|
||||
)
|
||||
return message, err
|
||||
}))
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("scan messages: %w", err)
|
||||
}
|
||||
|
||||
return page(messages, limit)
|
||||
}
|
||||
|
||||
func (s *Store) chatVisible(ctx context.Context, chatID, userID string) (bool, error) {
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = $1 AND user_id = $2
|
||||
)`
|
||||
|
||||
var exists bool
|
||||
if err := s.pool.QueryRow(ctx, query, chatID, userID).Scan(&exists); err != nil {
|
||||
return false, fmt.Errorf("check chat visibility: %w", err)
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func (s *Store) messagePositionExists(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
position model.Position,
|
||||
) (bool, error) {
|
||||
const query = `
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM messages
|
||||
WHERE chat_id = $1 AND created_at = $2 AND id = $3
|
||||
)`
|
||||
|
||||
var exists bool
|
||||
if err := s.pool.QueryRow(ctx, query, chatID, position.CreatedAt, position.ID).Scan(&exists); err != nil {
|
||||
return false, fmt.Errorf("validate message cursor: %w", err)
|
||||
}
|
||||
|
||||
return exists, nil
|
||||
}
|
||||
|
||||
func page[T any](items []T, limit int) ([]T, bool, error) {
|
||||
if limit < 1 {
|
||||
return nil, false, fmt.Errorf("limit must be positive")
|
||||
}
|
||||
if len(items) <= limit {
|
||||
return items, false, nil
|
||||
}
|
||||
|
||||
return items[:limit], true, nil
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
var postgresError *pgconn.PgError
|
||||
return errors.As(err, &postgresError) && postgresError.Code == "23505"
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/database"
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/postgres"
|
||||
)
|
||||
|
||||
func TestConcurrentUsernameCreation(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
username := "race_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:16]
|
||||
|
||||
const workers = 8
|
||||
results := make(chan error, workers)
|
||||
var group sync.WaitGroup
|
||||
for range workers {
|
||||
group.Add(1)
|
||||
go func() {
|
||||
defer group.Done()
|
||||
_, err := store.CreateUser(context.Background(), username, "Race user", "password-hash")
|
||||
results <- err
|
||||
}()
|
||||
}
|
||||
group.Wait()
|
||||
close(results)
|
||||
|
||||
var created, conflicts int
|
||||
for err := range results {
|
||||
switch {
|
||||
case err == nil:
|
||||
created++
|
||||
case errors.Is(err, postgres.ErrConflict):
|
||||
conflicts++
|
||||
default:
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
}
|
||||
if created != 1 || conflicts != workers-1 {
|
||||
t.Fatalf("created = %d, conflicts = %d", created, conflicts)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRollsBackForUnknownMember(t *testing.T) {
|
||||
pool := openTestDatabase(t)
|
||||
store := postgres.New(pool)
|
||||
owner, err := store.CreateUser(context.Background(), uniqueUsername(), "Owner", "password-hash")
|
||||
if err != nil {
|
||||
t.Fatalf("CreateUser() error = %v", err)
|
||||
}
|
||||
|
||||
before := chatCount(t, pool)
|
||||
_, err = store.CreateChat(context.Background(), "Invalid chat", []string{owner.ID, uuid.NewString()}, owner.ID)
|
||||
if !errors.Is(err, postgres.ErrInvalidMembers) {
|
||||
t.Fatalf("CreateChat() error = %v, want ErrInvalidMembers", err)
|
||||
}
|
||||
if after := chatCount(t, pool); after != before {
|
||||
t.Fatalf("chat count changed from %d to %d", before, after)
|
||||
}
|
||||
}
|
||||
|
||||
func openTestDatabase(t *testing.T) *pgxpool.Pool {
|
||||
t.Helper()
|
||||
url := testSecret(t, "TEST_DATABASE_URL", "TEST_DATABASE_URL_FILE")
|
||||
pool, err := database.Open(context.Background(), database.Config{
|
||||
URL: url, MinConnections: 0, MaxConnections: 10,
|
||||
MaxConnectionAge: time.Minute, MaxConnectionIdle: time.Minute,
|
||||
HealthCheckPeriod: time.Minute, ConnectTimeout: 5 * time.Second,
|
||||
MigrationTimeout: 30 * time.Second,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("database.Open() error = %v", err)
|
||||
}
|
||||
t.Cleanup(pool.Close)
|
||||
return pool
|
||||
}
|
||||
|
||||
func testSecret(t *testing.T, valueName, fileName string) string {
|
||||
t.Helper()
|
||||
if value := strings.TrimSpace(os.Getenv(valueName)); value != "" {
|
||||
return value
|
||||
}
|
||||
path := strings.TrimSpace(os.Getenv(fileName))
|
||||
if path == "" {
|
||||
t.Skipf("set %s or %s to run integration tests", valueName, fileName)
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read %s: %v", fileName, err)
|
||||
}
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
func uniqueUsername() string {
|
||||
return "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20]
|
||||
}
|
||||
|
||||
func chatCount(t *testing.T, pool *pgxpool.Pool) int {
|
||||
t.Helper()
|
||||
var count int
|
||||
if err := pool.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&count); err != nil {
|
||||
t.Fatalf("count chats: %v", err)
|
||||
}
|
||||
return count
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/valkey-io/valkey-go"
|
||||
)
|
||||
|
||||
const (
|
||||
tokenLength = 32
|
||||
keyPrefix = "session:"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("session not found")
|
||||
ErrInvalidToken = errors.New("invalid session token")
|
||||
ErrInvalidRecord = errors.New("invalid session record")
|
||||
ErrInvalidTTL = errors.New("invalid session TTL")
|
||||
ErrTokenGeneration = errors.New("session token generation failed")
|
||||
ErrStore = errors.New("session store failure")
|
||||
)
|
||||
|
||||
type record struct {
|
||||
UserID string `json:"user_id"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
}
|
||||
|
||||
type Store struct {
|
||||
client valkey.Client
|
||||
}
|
||||
|
||||
func New(rawURL string) (*Store, error) {
|
||||
option, err := valkey.ParseURL(rawURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: parse Valkey URL: %w", ErrStore, err)
|
||||
}
|
||||
|
||||
client, err := valkey.NewClient(option)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: create Valkey client: %w", ErrStore, err)
|
||||
}
|
||||
|
||||
return &Store{client: client}, nil
|
||||
}
|
||||
|
||||
func (s *Store) Create(ctx context.Context, userID string, ttl time.Duration) (string, time.Time, error) {
|
||||
if strings.TrimSpace(userID) == "" {
|
||||
return "", time.Time{}, ErrInvalidRecord
|
||||
}
|
||||
if ttl < time.Second {
|
||||
return "", time.Time{}, ErrInvalidTTL
|
||||
}
|
||||
|
||||
token, err := newToken()
|
||||
if err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
|
||||
record := record{
|
||||
UserID: userID,
|
||||
ExpiresAt: time.Now().UTC().Add(ttl).Truncate(time.Microsecond),
|
||||
}
|
||||
payload, err := json.Marshal(record)
|
||||
if err != nil {
|
||||
return "", time.Time{}, fmt.Errorf("%w: encode session: %w", ErrStore, err)
|
||||
}
|
||||
|
||||
command := s.client.B().Set().Key(s.key(token)).Value(valkey.BinaryString(payload)).Ex(ttl).Build()
|
||||
if err := s.client.Do(ctx, command).Error(); err != nil {
|
||||
return "", time.Time{}, fmt.Errorf("%w: create session: %w", ErrStore, err)
|
||||
}
|
||||
|
||||
return token, record.ExpiresAt, nil
|
||||
}
|
||||
|
||||
func newToken() (string, error) {
|
||||
random := make([]byte, tokenLength)
|
||||
if _, err := rand.Read(random); err != nil {
|
||||
return "", fmt.Errorf("%w: %v", ErrTokenGeneration, err)
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(random), nil
|
||||
}
|
||||
|
||||
func (s *Store) UserID(ctx context.Context, token string) (string, error) {
|
||||
key, err := s.sessionKey(token)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
payload, err := s.client.Do(ctx, s.client.B().Get().Key(key).Build()).AsBytes()
|
||||
if valkey.IsValkeyNil(err) {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%w: get session: %w", ErrStore, err)
|
||||
}
|
||||
|
||||
var record record
|
||||
if err := json.Unmarshal(payload, &record); err != nil {
|
||||
return "", fmt.Errorf("%w: decode session: %w", ErrStore, err)
|
||||
}
|
||||
if strings.TrimSpace(record.UserID) == "" || record.ExpiresAt.IsZero() {
|
||||
return "", fmt.Errorf("%w: %w", ErrStore, ErrInvalidRecord)
|
||||
}
|
||||
if !record.ExpiresAt.After(time.Now()) {
|
||||
return "", ErrNotFound
|
||||
}
|
||||
|
||||
return record.UserID, nil
|
||||
}
|
||||
|
||||
func (s *Store) Delete(ctx context.Context, token string) error {
|
||||
key, err := s.sessionKey(token)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
deleted, err := s.client.Do(ctx, s.client.B().Del().Key(key).Build()).AsInt64()
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: delete session: %w", ErrStore, err)
|
||||
}
|
||||
if deleted == 0 {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Ping(ctx context.Context) error {
|
||||
if err := s.client.Do(ctx, s.client.B().Ping().Build()).Error(); err != nil {
|
||||
return fmt.Errorf("%w: ping Valkey: %w", ErrStore, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) Close() error {
|
||||
s.client.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Store) sessionKey(token string) (string, error) {
|
||||
decoded, err := base64.RawURLEncoding.Strict().DecodeString(token)
|
||||
if err != nil || len(decoded) != tokenLength {
|
||||
return "", ErrInvalidToken
|
||||
}
|
||||
return s.key(token), nil
|
||||
}
|
||||
|
||||
func (s *Store) key(token string) string {
|
||||
digest := sha256.Sum256([]byte(token))
|
||||
return keyPrefix + hex.EncodeToString(digest[:])
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package session_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/dizi/mtusi2026lab2/internal/session"
|
||||
)
|
||||
|
||||
func TestSessionLifecycle(t *testing.T) {
|
||||
store := openTestStore(t)
|
||||
userID := uuid.NewString()
|
||||
token, expiresAt, err := store.Create(context.Background(), userID, time.Minute)
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
if token == "" || !expiresAt.After(time.Now()) {
|
||||
t.Fatalf("invalid token or expiry: token length = %d, expires_at = %v", len(token), expiresAt)
|
||||
}
|
||||
|
||||
actualUserID, err := store.UserID(context.Background(), token)
|
||||
if err != nil {
|
||||
t.Fatalf("UserID() error = %v", err)
|
||||
}
|
||||
if actualUserID != userID {
|
||||
t.Fatalf("UserID() = %q, want %q", actualUserID, userID)
|
||||
}
|
||||
if err := store.Delete(context.Background(), token); err != nil {
|
||||
t.Fatalf("Delete() error = %v", err)
|
||||
}
|
||||
if _, err := store.UserID(context.Background(), token); !errors.Is(err, session.ErrNotFound) {
|
||||
t.Fatalf("UserID() after delete error = %v, want ErrNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionExpires(t *testing.T) {
|
||||
store := openTestStore(t)
|
||||
token, _, err := store.Create(context.Background(), uuid.NewString(), time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("Create() error = %v", err)
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(3 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
_, err := store.UserID(context.Background(), token)
|
||||
if errors.Is(err, session.ErrNotFound) {
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("UserID() error = %v", err)
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
t.Fatal("session did not expire within three seconds")
|
||||
}
|
||||
|
||||
func openTestStore(t *testing.T) *session.Store {
|
||||
t.Helper()
|
||||
url := strings.TrimSpace(os.Getenv("TEST_CACHE_URL"))
|
||||
if url == "" {
|
||||
path := strings.TrimSpace(os.Getenv("TEST_CACHE_URL_FILE"))
|
||||
if path == "" {
|
||||
t.Skip("set TEST_CACHE_URL or TEST_CACHE_URL_FILE to run integration tests")
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read TEST_CACHE_URL_FILE: %v", err)
|
||||
}
|
||||
url = strings.TrimSpace(string(data))
|
||||
}
|
||||
store, err := session.New(url)
|
||||
if err != nil {
|
||||
t.Fatalf("session.New() error = %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
return store
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
# Адрес и внутренний порт HTTP-сервера.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
|
||||
# Адреса публикации сервисов на локальной машине.
|
||||
API_BIND_ADDRESS=127.0.0.1:8080
|
||||
SWAGGER_BIND_ADDRESS=127.0.0.1:8081
|
||||
PROMETHEUS_BIND_ADDRESS=127.0.0.1:9090
|
||||
GRAFANA_BIND_ADDRESS=127.0.0.1:3000
|
||||
|
||||
# Время жизни сессии и используемое хранилище.
|
||||
SESSION_TTL_SECONDS=3600
|
||||
SESSION_STORE=valkey
|
||||
|
||||
# Учётные данные локального PostgreSQL и строка подключения API.
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_DB=messenger
|
||||
POSTGRES_PASSWORD=local-postgres-password
|
||||
DATABASE_URL=postgres://messenger:local-postgres-password@postgres:5432/messenger?sslmode=disable
|
||||
|
||||
# Пароль локального Valkey и строка подключения API.
|
||||
VALKEY_PASSWORD=local-valkey-password
|
||||
CACHE_URL=redis://default:local-valkey-password@valkey:6379/0
|
||||
|
||||
# Размер пула и обслуживание соединений PostgreSQL.
|
||||
DB_MIN_CONNS=1
|
||||
DB_MAX_CONNS=10
|
||||
DB_MAX_CONNECTION_AGE_SECONDS=1800
|
||||
DB_MAX_CONNECTION_IDLE_SECONDS=300
|
||||
DB_HEALTH_CHECK_SECONDS=30
|
||||
|
||||
# Таймауты зависимостей, миграций и HTTP-запросов.
|
||||
DEPENDENCY_TIMEOUT_SECONDS=5
|
||||
MIGRATION_TIMEOUT_SECONDS=30
|
||||
REQUEST_TIMEOUT_SECONDS=5
|
||||
HTTP_READ_HEADER_TIMEOUT_SECONDS=5
|
||||
HTTP_READ_TIMEOUT_SECONDS=10
|
||||
HTTP_WRITE_TIMEOUT_SECONDS=10
|
||||
HTTP_IDLE_TIMEOUT_SECONDS=60
|
||||
HTTP_SHUTDOWN_TIMEOUT_SECONDS=10
|
||||
|
||||
# Параметры Argon2id для новых хешей паролей.
|
||||
PASSWORD_MEMORY_KIB=65536
|
||||
PASSWORD_ITERATIONS=3
|
||||
PASSWORD_PARALLELISM=2
|
||||
PASSWORD_SALT_LENGTH=16
|
||||
PASSWORD_KEY_LENGTH=32
|
||||
|
||||
# Порт Swagger внутри конфигурации CORS API.
|
||||
SWAGGER_PORT=8081
|
||||
|
||||
# Ограничения ресурсов контейнеров приложения и хранилищ.
|
||||
API_CPUS=2
|
||||
API_MEMORY=512m
|
||||
POSTGRES_CPUS=2
|
||||
POSTGRES_MEMORY=1g
|
||||
VALKEY_CPUS=1
|
||||
VALKEY_MEMORY=256m
|
||||
|
||||
# Пароль локального администратора Grafana.
|
||||
GRAFANA_ADMIN_PASSWORD=local-grafana-password
|
||||
@@ -0,0 +1,50 @@
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
display_name TEXT NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT users_username_unique UNIQUE (username),
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_display_name_length CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '[^[:space:]]'),
|
||||
CONSTRAINT users_password_hash_length CHECK (char_length(password_hash) BETWEEN 1 AND 1024),
|
||||
CONSTRAINT users_password_hash_not_blank CHECK (password_hash ~ '[^[:space:]]')
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT chats_title_length CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
CONSTRAINT chats_title_not_blank CHECK (title ~ '[^[:space:]]')
|
||||
);
|
||||
|
||||
CREATE INDEX chats_created_at_id_idx ON chats (created_at, id);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id UUID NOT NULL,
|
||||
user_id UUID NOT NULL,
|
||||
position SMALLINT NOT NULL,
|
||||
CONSTRAINT chat_members_pk PRIMARY KEY (chat_id, user_id),
|
||||
CONSTRAINT chat_members_chat_position_unique UNIQUE (chat_id, position),
|
||||
CONSTRAINT chat_members_position_range CHECK (position BETWEEN 0 AND 99),
|
||||
CONSTRAINT chat_members_chat_fk FOREIGN KEY (chat_id) REFERENCES chats (id) ON DELETE CASCADE,
|
||||
CONSTRAINT chat_members_user_fk FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX chat_members_user_id_chat_id_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id UUID PRIMARY KEY,
|
||||
chat_id UUID NOT NULL,
|
||||
sender_id UUID NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT clock_timestamp(),
|
||||
CONSTRAINT messages_text_length CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
CONSTRAINT messages_text_not_blank CHECK (text ~ '[^[:space:]]'),
|
||||
CONSTRAINT messages_sender_membership_fk FOREIGN KEY (chat_id, sender_id)
|
||||
REFERENCES chat_members (chat_id, user_id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE INDEX messages_chat_id_created_at_id_idx ON messages (chat_id, created_at, id);
|
||||
@@ -0,0 +1,8 @@
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
// FS contains database migrations applied by the application at startup.
|
||||
//
|
||||
//go:embed *.sql
|
||||
var FS embed.FS
|
||||
@@ -0,0 +1,76 @@
|
||||
{
|
||||
"annotations": {"list": []},
|
||||
"editable": false,
|
||||
"graphTooltip": 1,
|
||||
"panels": [
|
||||
{
|
||||
"type": "timeseries", "title": "API requests per second", "id": 1,
|
||||
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 0},
|
||||
"targets": [{"expr": "sum(rate(messenger_http_requests_total[1m]))", "legendFormat": "RPS", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "API responses by status class", "id": 2,
|
||||
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 0},
|
||||
"targets": [{"expr": "sum by (status_class) (rate(messenger_http_requests_total[1m]))", "legendFormat": "{{status_class}}", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "API latency p95", "id": 3,
|
||||
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 8},
|
||||
"fieldConfig": {"defaults": {"unit": "s"}, "overrides": []},
|
||||
"targets": [{"expr": "histogram_quantile(0.95, sum by (le, route) (rate(messenger_http_request_duration_seconds_bucket[5m])))", "legendFormat": "{{route}}", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "API in flight", "id": 4,
|
||||
"gridPos": {"h": 8, "w": 6, "x": 12, "y": 8},
|
||||
"targets": [{"expr": "messenger_http_requests_in_flight", "legendFormat": "requests", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "Go heap", "id": 5,
|
||||
"gridPos": {"h": 8, "w": 6, "x": 18, "y": 8},
|
||||
"fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []},
|
||||
"targets": [{"expr": "go_memstats_heap_alloc_bytes{job=\"messenger\"}", "legendFormat": "heap", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "PostgreSQL connections", "id": 6,
|
||||
"gridPos": {"h": 8, "w": 8, "x": 0, "y": 16},
|
||||
"targets": [{"expr": "sum(pg_stat_activity_count{datname=\"messenger\"})", "legendFormat": "connections", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "PostgreSQL transactions", "id": 7,
|
||||
"gridPos": {"h": 8, "w": 8, "x": 8, "y": 16},
|
||||
"targets": [
|
||||
{"expr": "rate(pg_stat_database_xact_commit{datname=\"messenger\"}[1m])", "legendFormat": "commit/s", "refId": "A"},
|
||||
{"expr": "rate(pg_stat_database_xact_rollback{datname=\"messenger\"}[1m])", "legendFormat": "rollback/s", "refId": "B"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "PostgreSQL database size", "id": 8,
|
||||
"gridPos": {"h": 8, "w": 8, "x": 16, "y": 16},
|
||||
"fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []},
|
||||
"targets": [{"expr": "pg_database_size_bytes{datname=\"messenger\"}", "legendFormat": "database", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "Valkey memory", "id": 9,
|
||||
"gridPos": {"h": 8, "w": 12, "x": 0, "y": 24},
|
||||
"fieldConfig": {"defaults": {"unit": "bytes"}, "overrides": []},
|
||||
"targets": [{"expr": "redis_memory_used_bytes", "legendFormat": "used", "refId": "A"}]
|
||||
},
|
||||
{
|
||||
"type": "timeseries", "title": "Valkey keys and commands", "id": 10,
|
||||
"gridPos": {"h": 8, "w": 12, "x": 12, "y": 24},
|
||||
"targets": [
|
||||
{"expr": "sum(redis_db_keys)", "legendFormat": "keys", "refId": "A"},
|
||||
{"expr": "rate(redis_commands_processed_total[1m])", "legendFormat": "commands/s", "refId": "B"}
|
||||
]
|
||||
}
|
||||
],
|
||||
"refresh": "5s",
|
||||
"schemaVersion": 41,
|
||||
"tags": ["highload", "lab2"],
|
||||
"templating": {"list": []},
|
||||
"time": {"from": "now-15m", "to": "now"},
|
||||
"timezone": "browser",
|
||||
"title": "Messenger overview",
|
||||
"uid": "messenger-overview",
|
||||
"version": 1
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
apiVersion: 1
|
||||
|
||||
providers:
|
||||
- name: Messenger
|
||||
folder: Highload
|
||||
type: file
|
||||
disableDeletion: true
|
||||
editable: false
|
||||
options:
|
||||
path: /etc/grafana/dashboards
|
||||
@@ -0,0 +1,10 @@
|
||||
apiVersion: 1
|
||||
|
||||
datasources:
|
||||
- name: Prometheus
|
||||
type: prometheus
|
||||
uid: prometheus
|
||||
access: proxy
|
||||
url: http://prometheus:9090
|
||||
isDefault: true
|
||||
editable: false
|
||||
@@ -0,0 +1,16 @@
|
||||
global:
|
||||
scrape_interval: 5s
|
||||
evaluation_interval: 5s
|
||||
|
||||
scrape_configs:
|
||||
- job_name: messenger
|
||||
static_configs:
|
||||
- targets: ["api:8080"]
|
||||
|
||||
- job_name: postgres
|
||||
static_configs:
|
||||
- targets: ["postgres-exporter:9187"]
|
||||
|
||||
- job_name: valkey
|
||||
static_configs:
|
||||
- targets: ["valkey-exporter:9121"]
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
docker compose restart postgres valkey
|
||||
docker compose up -d --wait --wait-timeout 90 postgres valkey
|
||||
docker compose restart api
|
||||
docker compose up -d --wait --wait-timeout 90 api
|
||||
Reference in New Issue
Block a user