package main // The public price book (#1374). The setup conversation's plan step runs on // the SPACE origin, which holds no home session — so the book must be // readable without auth. Prices are public information (the same figures // render on this page's own pickers); the payload carries nothing else, and // an empty book simply means the step doesn't happen. Cached so a burst of // setups never fans out into the provisioner, and served stale on refresh // errors so a provisioner blip can't blank a conversation mid-flight. import ( "net/http" "sync" "time" ) const pricebookTTL = 10 * time.Minute type pricebookCache struct { mu sync.Mutex tiers []any fetchedAt time.Time } // pricebookTiers returns the cached book, refreshing from the default // region's provisioner (any region as fallback) when stale. Never errors: // no cloud, billing off, or an unreachable provisioner with no prior copy // all come back as an empty book. func (s *Server) pricebookTiers() []any { c := &s.pricebook c.mu.Lock() defer c.mu.Unlock() if time.Since(c.fetchedAt) < pricebookTTL { return c.tiers } // Default region first — it's the book's source of truth (the same // preference the spaces listing applies to its billing key). clouds := s.allClouds() if s.cloud != nil { ordered := []*cloudProvisioner{s.cloud} for _, cl := range clouds { if cl != s.cloud { ordered = append(ordered, cl) } } clouds = ordered } for _, cl := range clouds { status, body, err := cl.do("GET", "/api/pricebook", nil) if err != nil || status != http.StatusOK { continue } tiers := []any{} if b, ok := body["billing"].(map[string]any); ok { if arr, ok := b["tiers"].([]any); ok { tiers = arr } } c.tiers, c.fetchedAt = tiers, time.Now() return c.tiers } if c.fetchedAt.IsZero() { return []any{} // nothing cached and nothing reachable — an empty book, not an error } return c.tiers // stale copy beats a blanked conversation step } // pricebookHandler: GET /api/pricebook — public and CORS-open (the caller is // a space origin's browser JS). {tiers:[…]}; empty = billing off. func (s *Server) pricebookHandler(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Cache-Control", "public, max-age=300") writeJSON(w, http.StatusOK, map[string]any{"tiers": s.pricebookTiers()}) }