# Running an instance This runs the **published image** `lgtmsupplyco/commerce` as your own storefront instance. The reference implementation is Postgres-canonical; you supply a Postgres, a master key, and per-store config. Start in **fake-payments** mode (browsable, no external accounts), then cut over to real payments. ## Prerequisites - A Postgres 16 database. - A base64-encoded 32-byte `LGTM_MASTER_KEY`. Generate one: ```bash openssl rand -base64 32 ``` - A base64 `LGTM_SESSION_SECRET`: `openssl rand -base64 32`. > **Architecture:** the image is currently **`linux/amd64` only** (no arm64 manifest). Cloud > hosts (Railway, Fly, etc.) run amd64, so this is a non-issue there. On Apple Silicon / > Colima, force the platform: `export DOCKER_DEFAULT_PLATFORM=linux/amd64` (or pass > `--platform linux/amd64`) — it runs fine under emulation. ## Boot behavior The image entrypoint is `server --role all`: it runs migrations (advisory-locked) then serves the HTTP API + admin UI **and** the apalis worker in one process. It listens on `$PORT` (default `8080`). ## Environment See the table in the [README](../../README.md#environment-variables). Minimum for a fake-payments instance: ```dotenv LGTM_TENANCY=single LGTM_DATABASE_URL=postgres://USER:PASS@HOST:5432/lgtm LGTM_MASTER_KEY= LGTM_SESSION_SECRET= LGTM_PUBLIC_URL=https://your-instance.example LGTM_STORE_DID=did:web:your-instance.example LGTM_ATPROTO_OAUTH_CLIENT_ID=https://your-instance.example/client-metadata.json LGTM_FAKE_PAYMENTS=1 # fake mode ignores the values but they must be non-empty: LGTM_STRIPE_SECRET_KEY=x LGTM_STRIPE_WEBHOOK_SECRET=x LGTM_PRINTFUL_API_KEY=x LGTM_PRINTFUL_WEBHOOK_SECRET=x ``` ## Seed the store (one-off) `--role seed` runs a one-shot: migrate, create a tenant + an ES256 store signing key (sealed with `LGTM_MASTER_KEY` — it MUST match the app's) + a storefront record + a demo product, then exit. Run it once against the same database and env: ```bash docker run --rm --env-file .env lgtmsupplyco/commerce:v0.2.1 --role seed ``` *(v0.2.1+: seeding is a role of the main binary — no entrypoint override. On older images use `--entrypoint /usr/local/bin/seed-demo`.)* ### Importing the Printful catalog With `LGTM_PRINTFUL_API_KEY` set, pull your Printful store's sync products into the local catalog (one-shot, safe to re-run — refreshes titles/prices/SKUs, never deletes, never overwrites local availability): ```bash docker run --rm --env-file .env lgtmsupplyco/commerce:v0.2.3 --role import ``` *(v0.2.3+: `--role import` is supported. On older images the flag is ignored and the server boots normally — pin the tag to avoid this.)* The import also sets each Printful sync variant's `external_id` to the local variant id. This is required for fulfillment: order submission references variants by that id. Run the import at least once before going live, and re-run it after adding or repricing products in Printful. Skipped or failed variants are listed as warnings in the summary but the command still exits 0 — review the summary (especially any "FULFILLMENT AT RISK" warnings) before going live. ## Railway quickstart 1. **New Project → Deploy from Docker image** → `lgtmsupplyco/commerce:v0.2.1`. 2. **Add a Postgres plugin.** In the service variables, set `LGTM_DATABASE_URL = ${{Postgres.DATABASE_URL}}` (Railway exposes `DATABASE_URL`; the app reads the `LGTM_`-prefixed name). 3. Set the rest of the env from the block above. Leave `PORT` unset — Railway injects it and the app binds it automatically. 4. **Seed:** run the one-off with the same env — override the service's start command to `--role seed` for one run (or `railway run … lgtmsupplyco/commerce:v0.2.1 --role seed`). No restart needed afterwards — the worker polls for the signing key and self-starts (v0.2.0+). 5. Open the Railway-provided URL; the storefront is browsable. 6. Attach your custom domain (`lgtm.shop`) in Railway settings; TLS is automatic. ## Verify the instance - `GET /healthz` returns 200 with JSON `{"status":"ok","version":"","commit":""}` — use it to confirm which image is deployed. `GET /readyz` returns 200 when DB + integrations are healthy. - Storefront is browsable at the public URL. - Full fake-payment loop: create a checkout → open the returned `/dev/pay?order_id=…` URL → the worker fulfills → a signed ES256 receipt is written → the order reaches `Fulfilled`. ## Store identity — connecting your DID Receipts are signed with the store's ES256 key; a verifier trusts a receipt by resolving your store DID → that key (SPEC §9). So the key must be published **under your DID**. The backend holds the key and exposes its public half; **how you publish it depends on your DID method** (`LGTM_STORE_DID` can be any DID — it's opaque to the backend): - **`GET /api/signing-key`** (any DID method) returns the key as a ready-to-register verification method: `{ "did": "", "verificationMethod": { id, type: "Multikey", controller, publicKeyMultibase } }`. 404 until the store is seeded. - **`did:web:` store** → also served as a full DID document at **`GET /.well-known/did.json`**; front it at the host root (e.g. an SSR frontend proxies `https:///.well-known/did.json` to the backend). Nothing else to do. - **`did:plc:…` store** (the atproto default) → `/.well-known/did.json` does **not** apply (a `did:plc` resolves via the PLC directory). Take the `verificationMethod` from `/api/signing-key` and add it to your DID document via a **PLC operation** (signed with your account's rotation key — you control the DID, the backend can't). Once atproto repo writes land (deferred in P1), the idiomatic alternative is publishing it as a `shop.lgtm.commerce.attestationKey` record in your repo, which works for any DID method. (This only gates *external* receipt verification — the store operates and stores signed receipts regardless, so it's not required for the demo.) ## Admin - **Admin UI (`/admin/*`) is not operable yet.** It gates on atproto OAuth, which is **stubbed** in this reference implementation (the callback is a `501`; no client-metadata document or token exchange). Real atproto OAuth (PAR + DPoP + authorization-server discovery) is a planned follow-up. Until then, provision the store with `--role seed` and manage catalog via the DB / API; the customer-facing storefront needs no admin login. ## Go-live cutover (real money) When you're ready to take real orders: 1. Set `LGTM_FAKE_PAYMENTS=0`. 2. Set the real keys in the env — these are read directly (single-tenant): `LGTM_STRIPE_SECRET_KEY`, `LGTM_STRIPE_WEBHOOK_SECRET`, `LGTM_PRINTFUL_API_KEY`, `LGTM_PRINTFUL_WEBHOOK_SECRET`. 3. **Stripe webhook:** in the Stripe Dashboard add an endpoint at `https:///webhooks/stripe` for events `checkout.session.completed` + `charge.refunded`; copy its signing secret (`whsec_…`) into `LGTM_STRIPE_WEBHOOK_SECRET`. 4. **Printful webhook:** Printful does **not** sign webhooks, so the backend authenticates via a secret **token in the URL**. Set `LGTM_PRINTFUL_WEBHOOK_SECRET` to a URL-safe random token (e.g. `openssl rand -hex 16`), then in the Printful Dashboard register the webhook URL as `https:///webhooks/printful?token=`. Keep the token URL-safe — it is compared verbatim, not URL-decoded. The backend handles the `package_shipped` event (marks the order's fulfillment `shipped` and stores the carrier tracking). The core payment→fulfillment path (checkout → Stripe → worker submits the Printful order via API) works without it; the webhook only syncs shipment status back. 5. Confirm `LGTM_PUBLIC_URL`, `LGTM_STORE_DID`, and your custom domain are the real ones. 6. Redeploy and run a real low-value test order end-to-end before announcing. ## Trusted channels (optional) If your deployment fronts the write plane with a transport that cryptographically verifies buyer DIDs (e.g. an SSH storefront gateway), enable **channel assertions** so a claimed `buyerDid` must be proven: - `LGTM_CHANNEL_ASSERTION_SECRET` — shared HS256 secret; give the same value to the transport (e.g. a Railway shared variable). Setting it turns the feature on. - `LGTM_CHANNEL_ASSERTION_AUDIENCE` — expected JWT `aud`; defaults to `LGTM_STORE_DID`. - `LGTM_LINK_URL` — base URL for device-link pages shown to buyers (`{base}/{code}`); defaults to `{shop_url}/link`. The transport sends the assertion as an `X-Channel-Assertion` header on write-plane calls and on the `/api/link/*` device-linking endpoints (see REFERENCE §8). Deployments without a trusted channel leave the secret unset and are unaffected. ## Consuming the image from your own repo Your instance repo (e.g. `lgtm-shop/lgtm.shop`) does not need the source. It holds a compose or Railway config that pins the image tag plus your secrets. See [`docker-compose.prod.yml`](../../docker-compose.prod.yml) for the shape — copy it, add a `.env`, and bump the pinned tag to upgrade.