commerce

package
v1.41.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package commerce (pkg/commerce) is the embedded Commerce server. One backend, one HTTP handler. Mirrors pkg/tasks/embed.go shape:

cfg := commerce.EmbedConfig{DataDir: "/var/lib/commerce", HTTPAddr: ":8090"}
srv, err := commerce.Embed(ctx, cfg)
defer srv.Stop(ctx)

The legacy App in commerce.go is the bootstrap — server.go wires it into commerced/main.go cleanly. The /v1/commerce/* surface stays behind hanzoai/gateway and is gated by COMMERCED_REQUIRE_IDENTITY.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type EmbedConfig

type EmbedConfig struct {
	DataDir         string       // "" → COMMERCE_DIR or ./commerce_data
	HTTPAddr        string       // "" → COMMERCE_HTTP or 127.0.0.1:8090
	Dev             bool         // dev mode — gin.DebugMode + reload-friendly logging
	RequireIdentity bool         // gateway trust: refuse requests without X-Org-Id/X-User-Id
	Logger          *slog.Logger // nil → slog.Default()
	AllowedOrigins  []string     // CORS — usually ["*"] behind gateway
}

EmbedConfig configures the in-process Commerce server. Empty values fall through to commerce.DefaultConfig (env-based) so commerced binds the same env contract the legacy commerce binary did.

type Embedded

type Embedded struct {
	// contains filtered or unexported fields
}

Embedded is the handle to a running in-process Commerce server. The underlying *commerceApp.App owns the heavy lifting (DB, infra, KMS, hooks, cron) — Embedded wraps it for clean Stop/HTTPHandler/HTTPAddr access from commerced.

func Embed

func Embed(ctx context.Context, cfg EmbedConfig) (*Embedded, error)

Embed bootstraps the Commerce app and returns a handle. Call Stop before the process exits.

func (*Embedded) App

func (e *Embedded) App() *commerceApp.App

App exposes the underlying App for tests and hook registration.

func (*Embedded) HTTPAddr

func (e *Embedded) HTTPAddr() string

HTTPAddr returns the configured listen address.

func (*Embedded) HTTPHandler

func (e *Embedded) HTTPHandler() http.Handler

HTTPHandler returns the gin router as a plain http.Handler. commerced wraps this with healthz + the embedded SPA at /_/commerce/.

func (*Embedded) Stop

func (e *Embedded) Stop(ctx context.Context) error

Stop shuts the server down. Idempotent.

type Store

type Store struct {
	// contains filtered or unexported fields
}

Store is the per-request, org-scoped data layer. Each request lands on its own *db.DB shard keyed by the gateway-supplied X-Org-Id, so two orgs can never see each other's rows even if a handler bug forgets to scope its query.

func NewStore

func NewStore(mgr *db.Manager) *Store

NewStore wraps a db.Manager. The returned Store has no org binding — call WithOrg to land on a specific shard.

func (*Store) DB

func (s *Store) DB() (db.DB, error)

DB returns the per-org *db.DB shard. It opens the shard lazily; the underlying db.Manager memoizes them so two requests on the same org share the same SQLite handle (with its own connection pool).

func (*Store) FromContext

func (s *Store) FromContext(ctx context.Context) *Store

FromContext returns a Store bound to the org id attached by pkg/auth.Gin (or pkg/auth.RequireIdentity). Empty org → unscoped "system" shard, which is the legacy default for unauthenticated dev requests. A request that asks for tenant data without an org must have failed the gateway-trust gate already.

func (*Store) Org

func (s *Store) Org() string

Org returns the bound org id ("" before WithOrg).

func (*Store) WithOrg

func (s *Store) WithOrg(org string) *Store

WithOrg binds the store to a per-org SQLite shard. Empty org falls back to the "system" shard so unauthenticated probes still land somewhere predictable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL