serverorder

package
v0.206.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2026 License: Apache-2.0 Imports: 19 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package serverorder is the laptop's half of a ready server (ADR-114): a server Caprock sells, created for the buyer and set up with the same script New server writes (internal/serverscript), which joins this computer with a key made here for that one order.

On the user's click (Settings → Computers → Add a computer → Get a ready server) the daemon makes an order id and a server key that lives 24 hours (peers.OrderJoinTTL), renders the setup script with that key inside, and registers the order with caprock.dev (Register): the order id, the size, how often it is paid (monthly or yearly) and the script, nothing else. caprock.dev keeps the script sealed until the server is created and deletes it then. It answers the Stripe payment link for that order, which the screen opens. Afterwards the daemon reads the order's status (Read) while the Computers screen shows it: whether it is paid, being created, booting, running, failed or stopping on a date.

The orders are kept in a 0600 file in the data directory (Store), with the kept form of each one's key (peers.KeptJoinKey, the secret's hash only), so a server that boots while this daemon restarts still finds its key open.

Index

Constants

View Source
const (
	Monthly = "month"
	Yearly  = "year"
)

How often a ready server is paid: the payment link caprock.dev answers.

View Source
const (
	DefaultBase = "https://caprock.dev"
	BaseEnv     = "CAPROCK_SERVER_ORDER_URL"
)

DefaultBase is caprock.dev. BaseEnv replaces it — for a preview stand or a test, which must never register an order with the real site.

View Source
const (
	WaitingPayment = "waiting_payment" // registered, not paid yet
	Creating       = "creating"        // paid; the server is being created
	Booting        = "booting"         // created; not running with an address yet
	Running        = "running"         // running: the setup script is installing Caprock
	Failed         = "failed"          // Reason says why, in words for the buyer
	Stopped        = "stopped"         // the subscription ended; the server is stopped
	Unknown        = "unknown"         // caprock.dev has no such order (never paid, and expired)
)

The statuses caprock.dev answers (Remote.Status).

View Source
const File = "server-orders.json"

File is the orders' file in the data directory, mode 0600.

Variables

View Source
var (
	// ErrNotOnSale is caprock.dev not taking orders: switched off, or no
	// payment link for that size and period.
	ErrNotOnSale = errors.New("ready servers are not on sale yet")
	// ErrUnreachable is caprock.dev not answering.
	ErrUnreachable = errors.New("could not reach caprock.dev")
)

Errors the screen tells apart.

View Source
var Sizes = []Size{
	{ID: "24", Memory: "24 GB", PriceUSD: 39, YearUSD: 390},
	{ID: "48", Memory: "48 GB", PriceUSD: 59, YearUSD: 590},
}

Sizes are the two ready servers and their prices, the one place the app states them: the Stripe prices of the products "Caprock Server — 24 GB" (prod_Vi0ETgAutCfxdn) and "Caprock Server — 48 GB" (prod_Vi0FcXDgNS1GIS). Monthly, $39 and $59, set by the owner on 2026-11-28; yearly, $390 and $590 (two months free), created in Stripe on 2026-11-29 (ADR-114; caprock-web docs/servers.md). A change of price in Stripe changes these in the same commit as the docs that state them.

Functions

func Base

func Base() string

Base is where orders are registered and read.

func CheckPayURL

func CheckPayURL(raw, size, id string) error

CheckPayURL holds caprock.dev's payment link to Stripe's pages and to this order's reference, so the screen never opens anything else.

func NewID

func NewID() (string, error)

NewID is an order id: 128 random bits, hex. It is the only thing that reads the order's status at caprock.dev, so it is unguessable.

func PeriodOr added in v0.206.0

func PeriodOr(p string) string

PeriodOr is p, or Monthly for "" (an order kept before yearly existed).

func Reference

func Reference(size, id string) string

Reference is the payment link's client_reference_id for an order: "server-24-<id>", monthly or yearly alike: caprock.dev keeps the period with the order, so the reference never changed. Stripe takes letters, digits, "-" and "_" there, up to 200 characters, and silently drops anything else (so no ".").

func Register

func Register(ctx context.Context, hc *http.Client, base, id, size, period, setup string) (string, error)

Register sends {order_id, size, period, setup} to caprock.dev and answers the order's Stripe payment link, monthly or yearly as period says. The error never carries the script.

func ValidID

func ValidID(id string) bool

ValidID says id is one NewID made.

func ValidPeriod added in v0.206.0

func ValidPeriod(p string) bool

ValidPeriod says p is Monthly or Yearly.

Types

type Order

type Order struct {
	ID   string `json:"id"`
	Size string `json:"size"`
	// Period is "month" or "year", as the buyer chose; absent on an order
	// kept before yearly existed, which was monthly (PeriodOr).
	Period string `json:"period,omitempty"`
	// PayURL is the order's Stripe payment link, to open again until it is paid.
	PayURL string `json:"pay_url,omitempty"`
	// CreatedAt and ExpiresAt are unix milliseconds; ExpiresAt is the key's.
	CreatedAt int64 `json:"created_at"`
	ExpiresAt int64 `json:"expires_at"`
	// Key reopens the order's key after a restart, until the server joins
	// or the key expires; then it is dropped.
	Key *peers.KeptJoinKey `json:"key,omitempty"`
	// Machine is the computer that joined with the key, as GET /v1/machines
	// lists it.
	Machine string `json:"machine,omitempty"`
	// KeyLost says the key stopped waiting without a join (withdrawn, burned
	// by wrong tries, or not reopened): the server cannot join with it.
	KeyLost bool `json:"key_lost,omitempty"`
	// Remote is what caprock.dev said last, and ReadAt when (unix ms).
	Remote Remote `json:"remote"`
	ReadAt int64  `json:"read_at,omitempty"`
}

Order is one ready server ordered from this computer.

type Registered

type Registered struct {
	PayURL string `json:"pay_url"`
}

Registered is caprock.dev's answer to a registration.

type Remote

type Remote struct {
	Status string `json:"status"`
	Size   string `json:"size,omitempty"`
	// Period is "month" or "year": the subscription's own interval once
	// Stripe has said it. A site from before yearly sends none.
	Period string `json:"period,omitempty"`
	Reason string `json:"reason,omitempty"`
	// StopsAt is unix seconds: the subscription is cancelled and the server
	// stops then.
	StopsAt int64 `json:"stops_at,omitempty"`
}

Remote is GET /api/server-order/<id>'s answer.

func Read

func Read(ctx context.Context, hc *http.Client, base, id string) (Remote, error)

Read is the order's status at caprock.dev. A 404 is Unknown, not an error.

type Size

type Size struct {
	// ID is what an order and the payment link's reference carry: "24", "48".
	ID string `json:"id"`
	// Memory is what the screen names the size by.
	Memory string `json:"memory"`
	// PriceUSD is the monthly Stripe price, US dollars a month.
	PriceUSD int `json:"price_usd"`
	// YearUSD is the yearly Stripe price, US dollars a year.
	YearUSD int `json:"year_usd"`
}

Size is one server Caprock sells.

func SizeByID

func SizeByID(id string) (Size, bool)

SizeByID is the size an id names.

type Store

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

Store keeps the orders in File.

func Open

func Open(dir string) *Store

Open is the store in dir ("" keeps nothing on disk: tests).

func (*Store) List

func (s *Store) List() ([]Order, error)

List is every order, newest first.

func (*Store) Update

func (s *Store) Update(fn func([]Order) []Order) error

Update changes the orders under the store's lock and writes them back.

Jump to

Keyboard shortcuts

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