mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 23:43:44 +00:00
a1ae36a54c
Same discovery pattern as ResultSubmitter/SessionIssuer, one level deeper: api.Service.rankedProfile and .profile both only ever read from an in-memory RankedProfiles map with no durable-store equivalent at all -- not "adapter exists but unwired" this time, there was no adapter. Every real request to GET /v1/profile/ranked or /api/v1/profile always 404'd regardless of a player's actual rating. Add RankedProfileProvider (an interface, not a struct-literal adapter this time) and store.PostgresRankedProfiles reading the ratings table; Service.rankedProfileFor prefers it when set and falls back to the map otherwise, so every existing test/direct Service literal keeps compiling and passing unchanged. A missing ratings row maps to the exact same (zero value, false, nil) the map lookup already produced, preserving existing not-found semantics rather than reinterpreting them. LastSeasonID/SeasonHistory are deliberately left unset -- the ratings table has no season pointer, and reconstructing history needs its own query and display semantics, not bundled in here speculatively. Wired into both cmd/control-plane and cmd/testkit-api. Verified against real PostgreSQL via curl: a fresh identity's ranked profile correctly 404s through the real adapter (same behavior as before, now for a real reason instead of an empty map).
114 lines
4.5 KiB
Go
114 lines
4.5 KiB
Go
// Package main is a TEST-ONLY control-plane binary, built solely to give
|
|
// scripts/verify_control_plane_integration.sh a real, running HTTP server --
|
|
// backed by real PostgreSQL, running the actual api.Service used in
|
|
// production -- for the Godot client to talk to over a real network
|
|
// connection. It is never referenced by any Dockerfile stage or Kubernetes
|
|
// manifest and must never be treated as a deployment target: fakeSteamLogin
|
|
// below accepts ANY non-empty ticket string as a valid identity instead of
|
|
// verifying it against the real Steam Web API, which is exactly the kind of
|
|
// bypass that must stay confined to a clearly-separate binary, never a flag
|
|
// on the real one (see cmd/control-plane, which has no such flag and never
|
|
// should). Every other adapter here is wired identically to cmd/control-plane.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"crypto/sha256"
|
|
"database/sql"
|
|
"encoding/hex"
|
|
"flag"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/cosmic-clash/cosmic-clash/server/api"
|
|
"github.com/cosmic-clash/cosmic-clash/server/domain"
|
|
"github.com/cosmic-clash/cosmic-clash/server/migrations"
|
|
"github.com/cosmic-clash/cosmic-clash/server/store"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
)
|
|
|
|
func main() {
|
|
listen := flag.String("listen", "127.0.0.1:0", "HTTP listen address; port 0 picks a free port, printed on startup")
|
|
dsn := flag.String("dsn", os.Getenv("COSMIC_CLASH_POSTGRES_DSN"), "PostgreSQL connection string")
|
|
migrationDir := flag.String("migrations", "migrations", "directory containing numbered SQL migrations")
|
|
flag.Parse()
|
|
if *dsn == "" {
|
|
fatalf("--dsn or COSMIC_CLASH_POSTGRES_DSN is required")
|
|
}
|
|
db, err := sql.Open("pgx", *dsn)
|
|
if err != nil {
|
|
fatalf("open PostgreSQL: %v", err)
|
|
}
|
|
defer db.Close()
|
|
startupCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
if err := db.PingContext(startupCtx); err != nil {
|
|
fatalf("ping PostgreSQL: %v", err)
|
|
}
|
|
if err := migrations.Apply(startupCtx, db, *migrationDir); err != nil {
|
|
fatalf("apply migrations: %v", err)
|
|
}
|
|
handler := (&api.Service{
|
|
SessionBackend: store.PostgresSessions{DB: db},
|
|
SessionIssuer: store.PostgresSessions{DB: db},
|
|
SteamLogin: fakeSteamLogin{db: db},
|
|
QueueBackend: store.PostgresQueue{DB: db},
|
|
ProposalBackend: api.ProposalProviderFromStore(db),
|
|
ProposalPromoter: api.ProposalPromoterFromStore(db),
|
|
ServerRegistrar: api.ServerRegistrarFromStore(db),
|
|
ResultSubmitter: store.PostgresResults{DB: db},
|
|
RankedProfileProvider: store.PostgresRankedProfiles{DB: db},
|
|
Assignment: api.AssignmentProviderFromStore(db),
|
|
ProbeRecorder: store.PostgresQueue{DB: db},
|
|
Now: func() time.Time { return time.Now().UTC() },
|
|
}).Handler()
|
|
listener, err := net.Listen("tcp", *listen)
|
|
if err != nil {
|
|
fatalf("listen: %v", err)
|
|
}
|
|
fmt.Printf("testkit-api listening on http://%s\n", listener.Addr())
|
|
server := &http.Server{Handler: handler, ReadHeaderTimeout: 5 * time.Second}
|
|
serveErr := make(chan error, 1)
|
|
go func() { serveErr <- server.Serve(listener) }()
|
|
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
select {
|
|
case err := <-serveErr:
|
|
if err != nil && err != http.ErrServerClosed {
|
|
fatalf("serve: %v", err)
|
|
}
|
|
case <-ctx.Done():
|
|
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer shutdownCancel()
|
|
_ = server.Shutdown(shutdownCtx)
|
|
}
|
|
}
|
|
|
|
// fakeSteamLogin derives a deterministic identity from the ticket string
|
|
// itself (never a real Steam Web API ticket in this binary) and ensures its
|
|
// identities row exists so session issuance's foreign key is satisfied.
|
|
type fakeSteamLogin struct{ db *sql.DB }
|
|
|
|
func (f fakeSteamLogin) Authenticate(ctx context.Context, ticket string, _ time.Time) (domain.VerifiedIdentity, error) {
|
|
if ticket == "" {
|
|
return domain.VerifiedIdentity{}, fmt.Errorf("empty ticket")
|
|
}
|
|
digest := sha256.Sum256([]byte(ticket))
|
|
playerID := "testkit-" + hex.EncodeToString(digest[:8])
|
|
steamID := "testkit-steam-" + hex.EncodeToString(digest[8:16])
|
|
if _, err := f.db.ExecContext(ctx, `INSERT INTO identities (player_id, steam_id) VALUES ($1, $2) ON CONFLICT (player_id) DO NOTHING`, playerID, steamID); err != nil {
|
|
return domain.VerifiedIdentity{}, err
|
|
}
|
|
return domain.VerifiedIdentity{PlayerID: playerID, SteamID: steamID}, nil
|
|
}
|
|
|
|
func fatalf(format string, args ...any) {
|
|
fmt.Fprintf(os.Stderr, "testkit-api: "+format+"\n", args...)
|
|
os.Exit(1)
|
|
}
|