Files
mtusi2026lab5-fork/main.go
T

442 lines
13 KiB
Go

package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"log/slog"
"net"
"net/http"
"os"
"os/signal"
"strconv"
"strings"
"sync/atomic"
"syscall"
"time"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/minio/minio-go/v7"
"github.com/redis/go-redis/v9"
)
type App struct {
db *pgxpool.Pool
sessions *redis.Client
secret []byte
ttl time.Duration
slots, kdf chan struct{}
stopping atomic.Bool
dummyHash string
instance string
idempotencyTTL time.Duration
s3 *minio.Client
bucket string
uploadSlots, decodeSlot chan struct{}
}
func env(key, value string) string {
if v := os.Getenv(key); v != "" {
return v
}
return value
}
func positive(key string, fallback int) int {
n, e := strconv.Atoi(env(key, strconv.Itoa(fallback)))
if e != nil || n <= 0 {
panic("invalid positive configuration: " + key)
}
return n
}
func secretFile(key string) (string, error) {
b, e := os.ReadFile(os.Getenv(key))
if e != nil {
return "", fmt.Errorf("cannot read %s", key)
}
s := strings.TrimSpace(string(b))
if s == "" {
return "", fmt.Errorf("empty %s", key)
}
return s, nil
}
func (a *App) dispatch(ctx context.Context, w http.ResponseWriter, r *http.Request) (int, any, error) {
path := r.URL.Path
if r.Method == "GET" && (path == "/health/live" || path == "/health/ready") {
if path == "/health/ready" && (a.stopping.Load() || a.db.Ping(ctx) != nil || a.sessions.Ping(ctx).Err() != nil || !a.s3Ready(ctx)) {
return 0, nil, unavailable()
}
return 200, map[string]string{"status": "ok"}, nil
}
if a.stopping.Load() {
return 0, nil, unavailable()
}
if r.Method == "POST" && path == "/api/v1/auth/sessions" {
return a.login(ctx, r)
}
if r.Method == "POST" && path == "/api/v1/users" {
body, err := readObject(w, r, 128*1024, []string{"username", "display_name", "password"})
if err != nil {
return 0, nil, err
}
name, err := textField(body, "username", 32)
if err != nil || !usernamePattern.MatchString(name) {
return 0, nil, invalid()
}
display, err := textField(body, "display_name", 100)
if err != nil {
return 0, nil, err
}
p, err := password(body)
if err != nil {
return 0, nil, err
}
if err = a.kdfSlot(ctx); err != nil {
return 0, nil, err
}
hash := hashPassword(p)
<-a.kdf
u, err := scanUser(a.db.QueryRow(ctx, "INSERT INTO users(id,username,display_name,password_hash) VALUES($1,$2,$3,$4) RETURNING id::text,username,display_name,created_at", uuid(), name, []byte(display), hash))
return 201, u, err
}
user, key, err := a.authenticate(ctx, r)
if err != nil {
return 0, nil, err
}
if r.Method == "DELETE" && path == "/api/v1/auth/sessions/current" {
count, e := a.sessions.Del(ctx, key).Result()
if e != nil {
return 0, nil, unavailable()
}
if count == 0 {
return 0, nil, unauthorized()
}
return 204, nil, nil
}
if r.Method == "GET" && path == "/api/v1/users/me" {
u, e := a.user(ctx, user)
return 200, u, e
}
if path == "/api/v1/chats" {
if r.Method == "GET" {
scope := "chats:" + user
limit, c, e := a.pagination(r, scope)
if e != nil {
return 0, nil, e
}
page, e := a.listChats(ctx, user, limit, c, scope)
return 200, page, e
}
if r.Method == "POST" {
body, e := readObject(w, r, 128*1024, []string{"title", "member_ids"})
if e != nil {
return 0, nil, e
}
title, e := textField(body, "title", 100)
if e != nil {
return 0, nil, e
}
var ids []string
if json.Unmarshal(body["member_ids"], &ids) != nil || len(ids) < 1 || len(ids) > 100 {
return 0, nil, invalid()
}
seen := map[string]bool{}
for i, id := range ids {
normalized, e := normalizeID(id)
if e != nil || seen[normalized] {
return 0, nil, invalid()
}
ids[i] = normalized
seen[normalized] = true
}
if !seen[user] {
return 0, nil, invalid()
}
chat, e := a.createChat(ctx, title, ids)
return 201, chat, e
}
}
if strings.HasPrefix(path, "/api/v1/attachments/") {
parts := strings.Split(strings.TrimPrefix(path, "/api/v1/attachments/"), "/")
id, e := normalizeID(parts[0])
if e != nil {
return 0, nil, e
}
if r.Method != "GET" || len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") {
return 0, nil, missing()
}
x, e := a.attachment(ctx, id, user)
if e != nil {
return 0, nil, e
}
if len(parts) == 1 {
return 200, x, nil
}
b, e := a.content(ctx, r, x)
return 200, b, e
}
if strings.HasPrefix(path, "/api/v1/chats/") {
parts := strings.Split(strings.TrimPrefix(path, "/api/v1/chats/"), "/")
id, e := normalizeID(parts[0])
if e != nil {
return 0, nil, e
}
if len(parts) > 2 || (len(parts) == 2 && parts[1] != "messages" && parts[1] != "attachments") {
return 0, nil, missing()
}
chat, e := a.chat(ctx, id, user)
if e != nil {
return 0, nil, e
}
if len(parts) == 1 && r.Method == "GET" {
return 200, chat, nil
}
if len(parts) == 2 && parts[1] == "attachments" {
if r.Method != "POST" {
return 0, nil, missing()
}
x, e := a.upload(ctx, w, r, id, user)
return 202, x, e
}
if len(parts) == 2 {
if r.Method == "GET" {
scope := "messages:" + id
limit, c, e := a.pagination(r, scope)
if e != nil {
return 0, nil, e
}
page, e := a.history(ctx, id, limit, c, scope)
return 200, page, e
}
if r.Method == "POST" {
keys := r.Header.Values("Idempotency-Key")
if len(keys) != 1 || !idempotencyPattern.MatchString(keys[0]) {
return 0, nil, invalid()
}
body, e := readObject(w, r, 128*1024, []string{"text", "attachment_ids"})
if e != nil {
return 0, nil, e
}
text, ids, e := messageFields(body)
if e != nil {
return 0, nil, e
}
m, e := a.createMessage(ctx, id, user, keys[0], text, ids)
return 201, m, e
}
}
}
return 0, nil, missing()
}
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
start := time.Now()
requestID := uuid()
status := 500
w.Header().Set("Content-Type", "application/json")
w.Header().Set("X-Request-Id", requestID)
w.Header().Set("X-Instance-Id", a.instance)
fail := func(err error) {
var e apiError
if !errors.As(err, &e) {
e = unavailable()
}
status = e.status
if status == 401 {
scheme := `Bearer realm="messenger"`
if r.URL.Path == "/api/v1/auth/sessions" {
scheme = `Basic realm="messenger", charset="UTF-8"`
}
w.Header().Set("WWW-Authenticate", scheme)
}
if status == 503 {
w.Header().Set("Retry-After", "1")
}
w.WriteHeader(status)
json.NewEncoder(w).Encode(map[string]any{"error": map[string]string{"code": e.code, "message": e.message, "request_id": requestID}})
}
defer func() {
if recovered := recover(); recovered != nil {
if recovered == http.ErrAbortHandler {
panic(recovered)
}
fail(apiError{500, "internal_error", "Internal server error"})
}
if env("REQUEST_LOG", "1") == "1" {
slog.Info("request", "method", r.Method, "status", status, "request_id", requestID, "duration_ms", time.Since(start).Milliseconds())
}
}()
ctx, cancel := context.WithTimeout(r.Context(), 30*time.Second)
defer cancel()
health := r.Method == "GET" && (r.URL.Path == "/health/live" || r.URL.Path == "/health/ready")
if !health {
select {
case a.slots <- struct{}{}:
defer func() { <-a.slots }()
default:
fail(unavailable())
return
}
}
code, value, err := a.dispatch(ctx, w, r)
if err != nil {
fail(err)
return
}
status = code
if b, ok := value.(binaryContent); ok {
writeContent(w, b)
return
}
w.WriteHeader(code)
if code != 204 {
json.NewEncoder(w).Encode(value)
}
}
func run() error {
if len(os.Args) > 1 && os.Args[1] == "memory-snapshot" {
return memorySnapshot()
}
if env("SESSION_STORE", "valkey") != "valkey" {
return errors.New("SESSION_STORE must be valkey")
}
address := net.JoinHostPort(env("APP_HOST", "0.0.0.0"), env("APP_PORT", "8080"))
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
client := http.Client{Timeout: 2 * time.Second}
res, e := client.Get("http://127.0.0.1:" + env("APP_PORT", "8080") + "/health/ready")
if e != nil {
return errors.New("not ready")
}
defer res.Body.Close()
if res.StatusCode != 200 {
return errors.New("not ready")
}
return nil
}
dbURL, err := secretFile("DATABASE_URL_FILE")
if err != nil {
return err
}
cfg, err := pgxpool.ParseConfig(dbURL)
if err != nil {
return errors.New("invalid database configuration")
}
cfg.MaxConns = int32(positive("DB_POOL_MAX", 8))
cfg.MinConns = 0
cfg.ConnConfig.ConnectTimeout = 2 * time.Second
cfg.ConnConfig.RuntimeParams["statement_timeout"] = "3000"
cfg.ConnConfig.RuntimeParams["lock_timeout"] = "1000"
cfg.ConnConfig.RuntimeParams["synchronous_commit"] = "on"
startup, cancel := context.WithTimeout(context.Background(), 45*time.Second)
defer cancel()
db, err := pgxpool.NewWithConfig(startup, cfg)
if err != nil {
return errors.New("database setup failed")
}
defer db.Close()
if len(os.Args) > 1 && os.Args[1] == "migrate" {
if err = migrate(startup, db); err != nil {
return fmt.Errorf("migration failed: %w", err)
}
slog.Info("migrations complete")
return nil
}
var schemaReady bool
if err = db.QueryRow(startup, "SELECT EXISTS(SELECT 1 FROM schema_migrations WHERE version='005_image_jobs.sql')").Scan(&schemaReady); err != nil || !schemaReady {
return errors.New("run the migrate service before starting API")
}
if len(os.Args) > 1 {
return imageCommand(db, os.Args[1:])
}
cacheURL, err := secretFile("CACHE_URL_FILE")
if err != nil {
return err
}
signing, err := secretFile("CURSOR_SECRET_FILE")
if err != nil {
return err
}
if len(signing) < 32 {
return errors.New("cursor secret too short")
}
options, err := redis.ParseURL(cacheURL)
if err != nil {
return errors.New("invalid session store configuration")
}
options.PoolSize = 8
options.MaxActiveConns = 8
options.DialTimeout = time.Second
options.ReadTimeout = time.Second
options.WriteTimeout = time.Second
options.PoolTimeout = time.Second
options.MaxRetries = -1
options.ContextTimeoutEnabled = true
sessions := redis.NewClient(options)
defer sessions.Close()
hostname, err := os.Hostname()
if err != nil {
return errors.New("cannot identify instance")
}
s3, bucket, err := s3Client()
if err != nil {
return err
}
a := &App{s3: s3, bucket: bucket, uploadSlots: make(chan struct{}, 5), decodeSlot: make(chan struct{}, 1), db: db, sessions: sessions, secret: []byte(signing), ttl: time.Duration(positive("SESSION_TTL_SECONDS", 3600)) * time.Second, slots: make(chan struct{}, positive("MAX_INFLIGHT", 32)), kdf: make(chan struct{}, 2), dummyHash: hashPassword("unknown-user-dummy-password"), instance: hostname, idempotencyTTL: time.Duration(positive("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second}
if a.idempotencyTTL < 24*time.Hour {
return errors.New("idempotency retention must be at least 24 hours")
}
server := &http.Server{Addr: address, Handler: a, ReadHeaderTimeout: 2 * time.Second, ReadTimeout: 30 * time.Second, WriteTimeout: 35 * time.Second, IdleTimeout: 5 * time.Second, MaxHeaderBytes: 16 * 1024}
listener, err := net.Listen("tcp", address)
if err != nil {
return errors.New("listen failed")
}
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
if env("POOL_METRICS", "0") == "1" {
go func() {
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s := db.Stat()
slog.Info("pool", "instance", a.instance, "acquired", s.AcquiredConns(), "max", s.MaxConns(), "empty_acquires", s.EmptyAcquireCount(), "acquire_ns", s.AcquireDuration().Nanoseconds())
}
}
}()
}
done := make(chan struct{})
go func() {
<-ctx.Done()
a.stopping.Store(true)
slog.Info("shutdown started")
// Allow two LB readiness checks to drain this backend before closing it.
time.Sleep(1500 * time.Millisecond)
shutdown, cancel := context.WithTimeout(context.Background(), 6*time.Second)
defer cancel()
err := server.Shutdown(shutdown)
if err != nil {
server.Close()
}
slog.Info("shutdown complete", "forced", err != nil)
close(done)
}()
slog.Info("listening", "address", address, "environment", env("APP_ENV", "lab"), "session_store", "valkey", "session_ttl_seconds", int(a.ttl.Seconds()))
if err = server.Serve(listener); err != nil && !errors.Is(err, http.ErrServerClosed) {
return errors.New("serve failed")
}
<-done
return nil
}
func main() {
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, nil)))
if err := run(); err != nil {
slog.Error("startup failed", "reason", err.Error())
os.Exit(1)
}
}