3 Commits

Author SHA1 Message Date
Ryan Hamamura
f4d5a52cf9 Switch to datastar-pro and stop tracking downloaded libs
All checks were successful
CI / Deploy / test (pull_request) Successful in 20s
CI / Deploy / lint (pull_request) Successful in 34s
CI / Deploy / deploy (pull_request) Has been skipped
Datastar-pro is fetched from a private Gitea repo (ryan/vendor-libs)
using VENDOR_TOKEN for CI/Docker builds, with a local fallback from
../optional/ for development. DaisyUI is pinned to v5.5.19 instead of
tracking latest. Downloaded files are now gitignored and fetched at
build time via 'task download', which is a dependency of both build
and live tasks.
2026-03-11 11:33:38 -10:00
8789c5414e Merge pull request 'fix: restore flex layout on #game-content wrapper' (#15) from fix/game-content-layout into main
All checks were successful
CI / Deploy / test (push) Successful in 19s
CI / Deploy / lint (push) Successful in 29s
CI / Deploy / deploy (push) Successful in 1m34s
2026-03-11 20:39:04 +00:00
Ryan Hamamura
7a1c91c858 fix: restore flex layout on #game-content wrapper
All checks were successful
CI / Deploy / test (pull_request) Successful in 17s
CI / Deploy / lint (pull_request) Successful in 27s
CI / Deploy / deploy (pull_request) Has been skipped
The SSE patching refactor (0808c4d) wrapped game elements in a bare
<div id="game-content"> without propagating the flex classes from
<main>. This broke center-alignment and vertical spacing for both
Connect 4 and Snake game pages.
2026-03-11 10:35:29 -10:00
16 changed files with 276 additions and 1167 deletions

View File

@@ -11,6 +11,10 @@
# PORT=7331 # PORT=7331
# Goose CLI migration config (only needed for running goose manually) # Goose CLI migration config (only needed for running goose manually)
# Gitea API token for downloading datastar-pro from private repo (CI/Docker only).
# Not needed for local dev — falls back to copying from ../optional/.
# VENDOR_TOKEN=
GOOSE_DRIVER=sqlite3 GOOSE_DRIVER=sqlite3
GOOSE_DBSTRING=data/games.db?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL) GOOSE_DBSTRING=data/games.db?_pragma=foreign_keys(1)&_pragma=journal_mode(WAL)
GOOSE_MIGRATION_DIR=db/migrations GOOSE_MIGRATION_DIR=db/migrations

View File

@@ -61,11 +61,13 @@ jobs:
mkdir -p $DEPLOY_DIR/data mkdir -p $DEPLOY_DIR/data
- name: Rebuild and restart - name: Rebuild and restart
env:
VENDOR_TOKEN: ${{ secrets.VENDOR_TOKEN }}
run: | run: |
cd $DEPLOY_DIR cd $DEPLOY_DIR
VERSION=$(git describe --tags --always) VERSION=$(git describe --tags --always)
COMMIT=$(git rev-parse --short HEAD) COMMIT=$(git rev-parse --short HEAD)
VERSION=$VERSION COMMIT=$COMMIT docker compose up -d --build --remove-orphans VERSION=$VERSION COMMIT=$COMMIT VENDOR_TOKEN=$VENDOR_TOKEN docker compose up -d --build --remove-orphans
- name: Prune unused images - name: Prune unused images
run: docker image prune -f run: docker image prune -f

4
.gitignore vendored
View File

@@ -27,6 +27,10 @@
*_templ.go *_templ.go
assets/css/output.css assets/css/output.css
# Downloaded client-side libs (fetched by cmd/downloader)
assets/js/datastar/*
assets/css/daisyui/*
# Deploy scripts and configs # Deploy scripts and configs
!deploy/*.sh !deploy/*.sh
!deploy/*.service !deploy/*.service

View File

@@ -10,6 +10,11 @@ COPY go.mod go.sum ./
RUN go mod download RUN go mod download
COPY . . COPY . .
RUN --mount=type=secret,id=vendor_token \
VENDOR_TOKEN=$(cat /run/secrets/vendor_token) \
go run cmd/downloader/main.go
RUN go tool templ generate RUN go tool templ generate
RUN go tool gotailwind -i assets/css/input.css -o assets/css/output.css --minify RUN go tool gotailwind -i assets/css/input.css -o assets/css/output.css --minify
RUN --mount=type=cache,target=/root/.cache/go-build \ RUN --mount=type=cache,target=/root/.cache/go-build \

View File

@@ -2,9 +2,12 @@ version: "3"
tasks: tasks:
download: download:
desc: Download latest client-side libs desc: Download pinned client-side libs
cmds: cmds:
- go run cmd/downloader/main.go - go run cmd/downloader/main.go
status:
- test -f assets/js/datastar/datastar.js
- test -f assets/css/daisyui/daisyui.js
build:templ: build:templ:
desc: Compile .templ files to Go desc: Compile .templ files to Go
@@ -31,6 +34,7 @@ tasks:
cmds: cmds:
- go build -o bin/games . - go build -o bin/games .
deps: deps:
- download
- build:templ - build:templ
- build:styles - build:styles
@@ -58,6 +62,7 @@ tasks:
live: live:
desc: Dev mode with hot-reload desc: Dev mode with hot-reload
deps: deps:
- download
- live:templ - live:templ
- live:styles - live:styles
- live:server - live:server

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,8 +1,8 @@
@import 'tailwindcss'; @import 'tailwindcss';
@source not "./daisyui{,*}.mjs"; @source not "./daisyui/daisyui{,*}.js";
@plugin "./daisyui.mjs"; @plugin "./daisyui/daisyui.js";
@plugin "./daisyui-theme.mjs" { @plugin "./daisyui/daisyui-theme.js" {
name: "stealth"; name: "stealth";
default: true; default: true;
color-scheme: light; color-scheme: light;

1
assets/js/README.md Normal file
View File

@@ -0,0 +1 @@
Downloaded by cmd/downloader at build time.

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,30 +1,20 @@
package main package main
import ( import (
"encoding/json"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"sync" "sync"
)
// Asset directories, relative to project root. "github.com/ryanhamamura/games/assets"
const (
jsDir = "assets/js"
cssDir = "assets/css"
) )
// files maps download URLs to local destination paths.
var files = map[string]string{
"https://raw.githubusercontent.com/starfederation/datastar/main/bundles/datastar.js": jsDir + "/datastar.js",
"https://raw.githubusercontent.com/starfederation/datastar/main/bundles/datastar.js.map": jsDir + "/datastar.js.map",
"https://github.com/saadeghi/daisyui/releases/latest/download/daisyui.mjs": cssDir + "/daisyui.mjs",
"https://github.com/saadeghi/daisyui/releases/latest/download/daisyui-theme.mjs": cssDir + "/daisyui-theme.mjs",
}
func main() { func main() {
if err := run(); err != nil { if err := run(); err != nil {
slog.Error("failure", "error", err) slog.Error("failure", "error", err)
@@ -32,16 +22,243 @@ func main() {
} }
} }
// Pinned dependency versions — update these to upgrade.
const (
datastarVersion = "v1.0.0-RC.8" // Pro build — fetched from private Gitea repo
daisyuiVersion = "v5.5.19"
)
// dependencies tracks pinned versions alongside their GitHub coordinates
// so the version check can look up the latest release for each.
var dependencies = []dependency{
{name: "datastar", owner: "starfederation", repo: "datastar", pinnedVersion: datastarVersion},
{name: "daisyui", owner: "saadeghi", repo: "daisyui", pinnedVersion: daisyuiVersion},
}
type dependency struct {
name string
owner string
repo string
pinnedVersion string
}
// datastar-pro sources, in order of preference.
const (
giteaRawURL = "https://gitea.adriatica.io/ryan/vendor-libs/raw/branch/main/datastar/datastar.js"
localFallbackPath = "../optional/web/resources/static/datastar/datastar.js"
)
func run() error { func run() error {
dirs := []string{jsDir, cssDir} jsDir := assets.DirectoryPath + "/js/datastar"
cssDir := assets.DirectoryPath + "/css/daisyui"
for _, dir := range dirs { daisyuiBase := "https://github.com/saadeghi/daisyui/releases/download/" + daisyuiVersion + "/"
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("create directory %s: %w", dir, err) downloads := map[string]string{
} daisyuiBase + "daisyui.js": cssDir + "/daisyui.js",
daisyuiBase + "daisyui-theme.js": cssDir + "/daisyui-theme.js",
} }
return download(files) directories := []string{jsDir, cssDir}
if err := removeDirectories(directories); err != nil {
return err
}
if err := createDirectories(directories); err != nil {
return err
}
if err := acquireDatastar(jsDir + "/datastar.js"); err != nil {
return err
}
if err := download(downloads); err != nil {
return err
}
checkForUpdates()
return nil
}
// acquireDatastar fetches datastar-pro from the private Gitea repo when
// GITEA_TOKEN is set, otherwise copies from the local optional project.
func acquireDatastar(dest string) error {
if token := os.Getenv("VENDOR_TOKEN"); token != "" {
slog.Info("downloading datastar-pro from private repo...")
return downloadWithAuth(giteaRawURL, dest, token)
}
slog.Info("copying datastar-pro from local fallback...", "src", localFallbackPath)
return copyFile(localFallbackPath, dest)
}
func copyFile(src, dest string) error {
in, err := os.Open(src) //nolint:gosec // paths are hardcoded constants
if err != nil {
return fmt.Errorf("open %s: %w", src, err)
}
defer in.Close() //nolint:errcheck
out, err := os.Create(dest) //nolint:gosec // paths are hardcoded constants
if err != nil {
return fmt.Errorf("create %s: %w", dest, err)
}
if _, err := io.Copy(out, in); err != nil {
out.Close() //nolint:errcheck
return fmt.Errorf("copy to %s: %w", dest, err)
}
if err := out.Close(); err != nil {
return fmt.Errorf("close %s: %w", dest, err)
}
return nil
}
func downloadWithAuth(rawURL, dest, token string) error {
req, err := http.NewRequest(http.MethodGet, rawURL, nil)
if err != nil {
return fmt.Errorf("create request for %s: %w", rawURL, err)
}
req.Header.Set("Authorization", "token "+token)
resp, err := http.DefaultClient.Do(req) //nolint:gosec // URL is built from compile-time constants
if err != nil {
return fmt.Errorf("GET %s: %w", rawURL, err)
}
defer resp.Body.Close() //nolint:errcheck
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GET %s: status %s", rawURL, resp.Status)
}
out, err := os.Create(dest) //nolint:gosec // paths are hardcoded constants
if err != nil {
return fmt.Errorf("create %s: %w", dest, err)
}
if _, err := io.Copy(out, resp.Body); err != nil {
out.Close() //nolint:errcheck
return fmt.Errorf("write %s: %w", dest, err)
}
if err := out.Close(); err != nil {
return fmt.Errorf("close %s: %w", dest, err)
}
return nil
}
// checkForUpdates queries the GitHub releases API for each dependency
// and logs a notice if a newer version is available. Failures are
// logged but never cause the download to fail.
func checkForUpdates() {
var wg sync.WaitGroup
for _, dep := range dependencies {
wg.Go(func() {
latest, err := latestGitHubRelease(dep.owner, dep.repo)
if err != nil {
slog.Warn("could not check for updates", "dependency", dep.name, "error", err)
return
}
if latest != dep.pinnedVersion {
slog.Warn("newer version available",
"dependency", dep.name,
"pinned", dep.pinnedVersion,
"latest", latest,
)
}
})
}
wg.Wait()
}
// githubRelease is the minimal subset of the GitHub releases API response we need.
type githubRelease struct {
TagName string `json:"tag_name"`
}
func latestGitHubRelease(owner, repo string) (string, error) {
u := &url.URL{
Scheme: "https",
Host: "api.github.com",
Path: fmt.Sprintf("/repos/%s/%s/releases/latest", owner, repo),
}
req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return "", fmt.Errorf("creating request: %w", err)
}
req.Header.Set("Accept", "application/vnd.github+json")
resp, err := http.DefaultClient.Do(req) //nolint:gosec
if err != nil {
return "", fmt.Errorf("fetching release: %w", err)
}
defer resp.Body.Close() //nolint:errcheck
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("unexpected status %s", resp.Status)
}
var release githubRelease
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil {
return "", fmt.Errorf("decoding response: %w", err)
}
return release.TagName, nil
}
func removeDirectories(dirs []string) error {
var wg sync.WaitGroup
errCh := make(chan error, len(dirs))
for _, path := range dirs {
wg.Go(func() {
if err := os.RemoveAll(path); err != nil {
errCh <- fmt.Errorf("remove directory %s: %w", path, err)
}
})
}
wg.Wait()
close(errCh)
var errs []error
for err := range errCh {
errs = append(errs, err)
}
return errors.Join(errs...)
}
func createDirectories(dirs []string) error {
var wg sync.WaitGroup
errCh := make(chan error, len(dirs))
for _, path := range dirs {
wg.Go(func() {
if err := os.MkdirAll(path, 0755); err != nil {
errCh <- fmt.Errorf("create directory %s: %w", path, err)
}
})
}
wg.Wait()
close(errCh)
var errs []error
for err := range errCh {
errs = append(errs, err)
}
return errors.Join(errs...)
} }
func download(files map[string]string) error { func download(files map[string]string) error {
@@ -71,15 +288,15 @@ func download(files map[string]string) error {
return errors.Join(errs...) return errors.Join(errs...)
} }
func downloadFile(url, dest string) error { func downloadFile(rawURL, dest string) error {
resp, err := http.Get(url) //nolint:gosec,noctx // static URLs, simple tool resp, err := http.Get(rawURL) //nolint:gosec,noctx // static URLs, simple tool
if err != nil { if err != nil {
return fmt.Errorf("GET %s: %w", url, err) return fmt.Errorf("GET %s: %w", rawURL, err)
} }
defer resp.Body.Close() //nolint:errcheck defer resp.Body.Close() //nolint:errcheck
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GET %s: status %s", url, resp.Status) return fmt.Errorf("GET %s: status %s", rawURL, resp.Status)
} }
out, err := os.Create(dest) //nolint:gosec // paths are hardcoded constants out, err := os.Create(dest) //nolint:gosec // paths are hardcoded constants

View File

@@ -5,6 +5,8 @@ services:
args: args:
VERSION: ${VERSION:-dev} VERSION: ${VERSION:-dev}
COMMIT: ${COMMIT:-unknown} COMMIT: ${COMMIT:-unknown}
secrets:
- vendor_token
container_name: games container_name: games
restart: unless-stopped restart: unless-stopped
ports: ports:
@@ -16,3 +18,7 @@ services:
- PORT=8080 - PORT=8080
volumes: volumes:
- ./data:/data - ./data:/data
secrets:
vendor_token:
environment: VENDOR_TOKEN

View File

@@ -24,7 +24,7 @@ templ GamePage(g *connect4.Game, myColor int, messages []chat.Message, chatCfg c
} }
templ GameContent(g *connect4.Game, myColor int, messages []chat.Message, chatCfg chatcomponents.Config) { templ GameContent(g *connect4.Game, myColor int, messages []chat.Message, chatCfg chatcomponents.Config) {
<div id="game-content"> <div id="game-content" class="flex flex-col items-center gap-4">
@sharedcomponents.LiveClock() @sharedcomponents.LiveClock()
@sharedcomponents.BackToLobby() @sharedcomponents.BackToLobby()
@sharedcomponents.StealthTitle("text-3xl font-bold") @sharedcomponents.StealthTitle("text-3xl font-bold")

View File

@@ -12,7 +12,7 @@ templ Base(title string) {
<head> <head>
<title>{ title }</title> <title>{ title }</title>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0"/>
<script defer type="module" src={ assets.StaticPath("js/datastar.js") }></script> <script defer type="module" src={ assets.StaticPath("js/datastar/datastar.js") }></script>
<link href={ assets.StaticPath("css/output.css") } rel="stylesheet" type="text/css"/> <link href={ assets.StaticPath("css/output.css") } rel="stylesheet" type="text/css"/>
</head> </head>
<body class="flex flex-col h-screen"> <body class="flex flex-col h-screen">

View File

@@ -43,7 +43,7 @@ templ GamePage(sg *snake.SnakeGame, mySlot int, messages []chat.Message, chatCfg
} }
templ GameContent(sg *snake.SnakeGame, mySlot int, messages []chat.Message, chatCfg chatcomponents.Config, gameID string) { templ GameContent(sg *snake.SnakeGame, mySlot int, messages []chat.Message, chatCfg chatcomponents.Config, gameID string) {
<div id="game-content"> <div id="game-content" class="flex flex-col items-center gap-4">
@components.LiveClock() @components.LiveClock()
@components.BackToLobby() @components.BackToLobby()
<h1 class="text-3xl font-bold">~~~~</h1> <h1 class="text-3xl font-bold">~~~~</h1>