RPGTheory/aeternalis-portal had diverged from this repo (the git/CI/CD copy) with an in-progress control portal feature that was never synced back here, so none of it had gone through CI or been deployed: - New site/game/control routing split: index.html becomes a world directory landing page (site.ts), game.html is the renamed former index.html (main.ts), control.html is a new admin panel (control.ts, portal-shared.ts, portal.css). - Server: new internal/store/portal.go (character/world registry, command queue, admin grants) and internal/transport/control.go (portal API routes), plus a new internal/gitops package that lets the portal provision new worlds via a GitOps repo commit. - game.World gains BornAt/DiedAt/DeathReason and an operator Control() path (close/open admissions, awaken boss, end world, per-character rename/heal/unstuck/retire); death no longer reuses the same character id on rejoin, matching the "new life" permadeath model in Plan.md rather than the old bypass-prevention test's expectations. - deploy/: new control-egress network policy and portal/control values (admin role, GitOps repo/branch/values file/CIDRs). - New e2e coverage: tests/portal.spec.ts, tools/capture-world.mjs. Verified this repo's own independent commits (800ms movement-staleness window, 20s boss-attack poll, SRS/TLS deploy values, ubuntu-22.04 runner pin, DOCKER_HOST fix) were already present in the portal copy before merging, so nothing here regresses them. `tsc --noEmit`, the vite client build, and the protocol contract check all pass; no go.mod changes. Could not run `go build`/`go test` locally (no Go toolchain on PATH, Docker daemon not running) — every new/changed Go symbol was manually cross-referenced and resolves, but this should still be built and tested in CI before merging to master. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
178 lines
5.2 KiB
Go
178 lines
5.2 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"net"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"os/signal"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"aeternalis/server/content"
|
|
"aeternalis/server/internal/game"
|
|
"aeternalis/server/internal/gitops"
|
|
"aeternalis/server/internal/store"
|
|
"aeternalis/server/internal/transport"
|
|
)
|
|
|
|
func env(k, def string) string {
|
|
if v := os.Getenv(k); v != "" {
|
|
return v
|
|
}
|
|
return def
|
|
}
|
|
func run() error {
|
|
dev := env("APP_ENV", "production") == "development"
|
|
listen := env("LISTEN_ADDR", "127.0.0.1:18080")
|
|
base := env("PUBLIC_URL", "http://127.0.0.1:18080")
|
|
if dev {
|
|
host, _, err := net.SplitHostPort(listen)
|
|
if err != nil || net.ParseIP(host) == nil || !net.ParseIP(host).IsLoopback() {
|
|
return errors.New("development auth requires an explicit loopback listen address")
|
|
}
|
|
u, err := url.Parse(base)
|
|
if err != nil || net.ParseIP(u.Hostname()) == nil || !net.ParseIP(u.Hostname()).IsLoopback() {
|
|
return errors.New("development PUBLIC_URL must be loopback")
|
|
}
|
|
}
|
|
if !dev && os.Getenv("DATABASE_URL") == "" {
|
|
return errors.New("production requires DATABASE_URL")
|
|
}
|
|
dsn := env("DATABASE_URL", "file:../.local/aeternalis.db")
|
|
if !dev && !(strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://")) {
|
|
return errors.New("production requires PostgreSQL")
|
|
}
|
|
if !dev {
|
|
u, err := url.Parse(dsn)
|
|
if err != nil || u.Query().Get("sslmode") != "verify-full" {
|
|
return errors.New("production DATABASE_URL requires sslmode=verify-full")
|
|
}
|
|
}
|
|
if dev && strings.HasPrefix(dsn, "file:") {
|
|
path := strings.Split(strings.TrimPrefix(dsn, "file:"), "?")[0]
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
b, err := content.Load()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if dev {
|
|
for key, dest := range map[string]*float64{"DEV_SEASON_SECONDS": &b.SeasonSeconds, "DEV_COLLAPSE_SECONDS": &b.CollapseSeconds, "DEV_DAILY_SECONDS": &b.DailyPlaySeconds} {
|
|
if raw := os.Getenv(key); raw != "" {
|
|
value, err := strconv.ParseFloat(raw, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*dest = value
|
|
}
|
|
}
|
|
}
|
|
if err = b.Validate(); err != nil {
|
|
return err
|
|
}
|
|
seed, err := strconv.ParseInt(env("WORLD_SEED", "734291"), 10, 64)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
id := env("WORLD_ID", "world-001")
|
|
if len(id) > 64 || strings.ContainsAny(id, "/\\ \t\n") {
|
|
return errors.New("invalid WORLD_ID")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
defer cancel()
|
|
db, err := store.Open(ctx, dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer db.Close()
|
|
w := game.New(id, seed, time.Now().UTC(), b)
|
|
lease, raw, err := db.Acquire(ctx, w)
|
|
if err != nil {
|
|
return fmt.Errorf("acquire world: %w", err)
|
|
}
|
|
released := false
|
|
defer func() {
|
|
if !released {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
_ = lease.Release(ctx)
|
|
}
|
|
}()
|
|
if err = json.Unmarshal(raw, w); err != nil {
|
|
return err
|
|
}
|
|
if err = w.Restore(time.Now().UTC()); err != nil {
|
|
return err
|
|
}
|
|
auth, err := transport.NewAuth(ctx, db, dev, base, os.Getenv("OIDC_ISSUER"), os.Getenv("OIDC_CLIENT_ID"), os.Getenv("OIDC_CLIENT_SECRET"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
auth.AdminRole = env("OIDC_ADMIN_ROLE", "aeternalis-admin")
|
|
auth.DevAdmin = dev && env("DEV_ADMIN", "false") == "true"
|
|
worlds := []transport.Destination{}
|
|
if raw := os.Getenv("WORLD_DIRECTORY"); raw != "" {
|
|
if err = json.Unmarshal([]byte(raw), &worlds); err != nil {
|
|
return err
|
|
}
|
|
for _, dest := range worlds {
|
|
u, err := url.Parse(dest.URL)
|
|
if err != nil || u.Host == "" || (!dev && u.Scheme != "https") || u.User != nil {
|
|
return errors.New("invalid WORLD_DIRECTORY URL")
|
|
}
|
|
}
|
|
}
|
|
svc := transport.NewService(w, lease)
|
|
handler := transport.NewHTTP(svc, auth, env("CLIENT_DIR", "../client/dist"), worlds)
|
|
if env("CONTROL_PRIMARY", "true") == "true" {
|
|
handler.Provisioner, err = gitops.New(gitops.Config{Repository: os.Getenv("CONTROL_GITOPS_REPOSITORY"), Branch: os.Getenv("CONTROL_GITOPS_BRANCH"), File: os.Getenv("CONTROL_GITOPS_VALUES_FILE"), Token: os.Getenv("CONTROL_GITOPS_TOKEN"), PublicURL: base})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
server := &http.Server{Addr: listen, Handler: handler.Handler(), ReadHeaderTimeout: 5 * time.Second, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second, IdleTimeout: 60 * time.Second, MaxHeaderBytes: 16384}
|
|
runtimeCtx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer stop()
|
|
go svc.Run(runtimeCtx)
|
|
go handler.RunProvisioning(runtimeCtx)
|
|
errorsCh := make(chan error, 1)
|
|
go func() {
|
|
slog.Info("world listening", "url", base, "world", id, "development", dev)
|
|
errorsCh <- server.ListenAndServe()
|
|
}()
|
|
select {
|
|
case <-runtimeCtx.Done():
|
|
case err = <-errorsCh:
|
|
if !errors.Is(err, http.ErrServerClosed) {
|
|
return err
|
|
}
|
|
}
|
|
stop()
|
|
shutdown, cancelShutdown := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancelShutdown()
|
|
err = svc.Stop(shutdown)
|
|
released = true
|
|
httpErr := server.Shutdown(shutdown)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return httpErr
|
|
}
|
|
func main() {
|
|
if err := run(); err != nil {
|
|
slog.Error("world stopped", "error", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|