Files
mtusi2026lab2-fork/main.go
T

361 lines
10 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/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
}
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) {
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/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") {
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 {
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" {
body, e := readObject(w, r, 128*1024, []string{"text"})
if e != nil {
return 0, nil, e
}
text, e := textField(body, "text", 4000)
if e != nil {
return 0, nil, e
}
m := Message{ID: uuid(), ChatID: id, SenderID: user, Text: text}
var created time.Time
e = a.db.QueryRow(ctx, "INSERT INTO messages(id,chat_id,sender_id,text) VALUES($1,$2,$3,$4) RETURNING created_at", m.ID, id, user, []byte(text)).Scan(&created)
m.CreatedAt = stamp(created)
return 201, m, dbError(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)
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 recover() != nil {
fail(apiError{500, "internal_error", "Internal server error"})
}
slog.Info("request", "method", r.Method, "status", status, "request_id", requestID, "duration_ms", time.Since(start).Milliseconds())
}()
ctx, cancel := context.WithTimeout(r.Context(), 5*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
w.WriteHeader(code)
if code != 204 {
json.NewEncoder(w).Encode(value)
}
}
func run() error {
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
}
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")
}
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 err = migrate(startup, db); err != nil {
return errors.New("migration failed")
}
if len(os.Args) > 1 && os.Args[1] == "migrate" {
slog.Info("migrations complete")
return nil
}
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()
a := &App{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")}
server := &http.Server{Addr: address, Handler: a, ReadHeaderTimeout: 2 * time.Second, ReadTimeout: 3 * time.Second, WriteTimeout: 8 * 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()
done := make(chan struct{})
go func() {
<-ctx.Done()
a.stopping.Store(true)
slog.Info("shutdown started")
shutdown, cancel := context.WithTimeout(context.Background(), 8*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)
}
}