feat(export): server-rendered artifacts behind one signed download URL (#160)
CI / changes (push) Successful in 1s
CI / unit (push) Successful in 9s
CI / integration (push) Successful in 15s
CI / ui (push) Successful in 1m2s
CI / conformance (push) Successful in 9s
CI / gate (push) Successful in 0s
CI / deploy (push) Successful in 1m42s
CI / changes (push) Successful in 1s
CI / unit (push) Successful in 9s
CI / integration (push) Successful in 15s
CI / ui (push) Successful in 1m2s
CI / conformance (push) Successful in 9s
CI / gate (push) Successful in 0s
CI / deploy (push) Successful in 1m42s
The finished-game export (GCG + a new PNG of the final position) is one signed, short-lived relative URL (game.export_url; HMAC-SHA256, 10-min TTL, BACKEND_EXPORT_SIGN_KEY) resolved against the client's own origin and delivered by the best affordance each platform has (five on-device review rounds): - TG Android/desktop: native showPopup chooser -> native downloadFile dialog (bridge-only chain, activation-safe). - TG iOS: app-modal chooser -> OS share sheet with the fetched file (a popup callback cannot supply the activation the sheet needs). - VK iOS: VKWebAppDownloadFile for both formats. - VK Android: the PNG opens in VK's native image viewer, the GCG copies to the clipboard (the VK Android downloader hangs on any download, Content-Length/Range notwithstanding). - VK desktop iframe / desktop browsers: plain anchor downloads. - Mobile browsers: the OS share sheet (fetch-then-share). - Legacy TG (< Bot API 8.0): app modal + GCG clipboard, no image option. The PNG is rasterized on demand by the new internal `renderer` sidecar (node:22-slim + skia-canvas + baked Liberation/Noto Color Emoji fonts) executing the SAME ui/src/lib/gameimage.ts the ui project unit-tests; the backend rebuilds the render payload from the journal + engine.AlphabetTable, and the device date locale, IANA time zone and localized non-play labels ride the signed URL. Nothing is stored — the artifact re-derives from the immutable journal on each GET. The gateway forwards /dl/* (caddy @gateway matcher extended) behind the per-IP public rate limiter and serves bytes via http.ServeContent. Deploy: renderer service in compose + prod overlay + rolling order + prod push list; TEST_/PROD_EXPORT_SIGN_KEY secrets; the sidecar smoke runs in the ui CI job. Docs: ARCHITECTURE, FUNCTIONAL(+_ru), UI_DESIGN, TESTING, deploy/README, renderer/README.
This commit was merged in pull request #160.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
// Package render is the backend's client for the internal image-render sidecar: it POSTs
|
||||
// a finished game's export payload and receives the rasterized PNG. The sidecar runs the
|
||||
// same drawing code the web client unit-tests (ui/src/lib/gameimage.ts on skia-canvas);
|
||||
// authentication and the signed public URL live in the server layer — this client only
|
||||
// carries bytes.
|
||||
package render
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// maxPNG caps a render response; a scale-2 export of the largest plausible game is well
|
||||
// under 2 MiB, so anything bigger is a sidecar malfunction, not a valid image.
|
||||
const maxPNG = 8 << 20
|
||||
|
||||
// Client calls the render sidecar over the internal network.
|
||||
type Client struct {
|
||||
base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
// New returns a client for the sidecar at baseURL (e.g. http://renderer:8090). A render
|
||||
// of a full game takes well under a second; the timeout leaves room for a cold start.
|
||||
func New(baseURL string) *Client {
|
||||
return &Client{base: baseURL, http: &http.Client{Timeout: 15 * time.Second}}
|
||||
}
|
||||
|
||||
// Render posts payload (the JSON request shape the sidecar consumes) and returns the PNG.
|
||||
func (c *Client) Render(ctx context.Context, payload any) ([]byte, error) {
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render: marshal payload: %w", err)
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.base+"/render", bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render: build request: %w", err)
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("render: sidecar status %d", resp.StatusCode)
|
||||
}
|
||||
png, err := io.ReadAll(io.LimitReader(resp.Body, maxPNG+1))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("render: read response: %w", err)
|
||||
}
|
||||
if len(png) > maxPNG {
|
||||
return nil, fmt.Errorf("render: response exceeds %d bytes", maxPNG)
|
||||
}
|
||||
return png, nil
|
||||
}
|
||||
Reference in New Issue
Block a user