api

package
v1.0.0-rc.1 Latest Latest
Warning

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

Go to latest
Published: Jun 13, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package api is branchd's REST control plane: a thin JSON layer over the engine. stdlib net/http only; routes use Go 1.22 method patterns. All /v1 routes require the bearer token; /healthz does not.

Index

Constants

View Source
const DefaultStuckTimeout = 10 * time.Minute

DefaultStuckTimeout is the fallback cutoff for reconcile's stuck-row pass.

Variables

This section is empty.

Functions

This section is empty.

Types

type Branch

type Branch struct {
	Name   string `json:"name"`
	Source string `json:"source"`
	// Parent is the branch this one was created from (branch-from-branch);
	// "" when created directly from the source.
	Parent string `json:"parent,omitempty"`
	State  string `json:"state"`
	Host   string `json:"host"`
	Port   int    `json:"port"`
	User   string `json:"user"`
	// Password is the branch's own rotated password — present only when
	// branchd runs with --rotate-branch-credentials; otherwise the branch
	// inherits the source's credentials and the field is omitted.
	Password string `json:"password,omitempty"`
	Database string `json:"database"`
	// ProxyDatabase is the database param to use when connecting through the
	// wire-protocol router: dbname@branch.
	ProxyDatabase string `json:"proxy_database"`
	ExpiresAt     string `json:"expires_at,omitempty"`
	CreatedAt     string `json:"created_at"`
}

type CreateBranchRequest

type CreateBranchRequest struct {
	Name       string `json:"name"`
	Source     string `json:"source,omitempty"`
	Parent     string `json:"parent,omitempty"`
	TTLSeconds int    `json:"ttl_seconds"`
}

CreateBranchRequest creates a branch off a source (Source) or off another branch (Parent) — exactly one of the two must be set.

type CreateSourceRequest

type CreateSourceRequest struct {
	Name      string `json:"name"`
	Host      string `json:"host"`
	Port      int    `json:"port"`
	User      string `json:"user"`
	Database  string `json:"database"`
	Network   string `json:"network"`
	PGVersion string `json:"pg_version"`
	Password  string `json:"password"`
	// Via selects the seeding method: "basebackup" (default) or "dump".
	Via string `json:"via,omitempty"`
	// DumpSchemas scopes a via=dump seed to the given schemas (empty = the
	// whole database). Only valid with via=dump.
	DumpSchemas []string `json:"dump_schemas,omitempty"`
}

type CreateTokenRequest

type CreateTokenRequest struct {
	Name string `json:"name"`
	Role string `json:"role"`
}

CreateTokenRequest mints an API token with the given name and role (admin|operator|viewer). The plaintext token is returned once in the response and never recoverable afterwards.

type CreateTokenResponse

type CreateTokenResponse struct {
	Token string `json:"token"`
}

CreateTokenResponse carries the freshly minted plaintext token (shown once).

type LeaderGate

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

LeaderGate is the HA mutating-route gate: an atomic leadership flag the leader-election orchestration flips. It defaults to leader=true so that with leader election OFF (docker/local, single instance) every instance is always the leader and mutating routes behave normally. When false, mutating /v1 routes return 503 "not leader" while reads, /healthz, /readyz and /metrics keep serving (a non-leader backs read traffic and probes off its own read-only registry handle).

func (*LeaderGate) IsLeader

func (g *LeaderGate) IsLeader() bool

IsLeader reports whether this instance currently holds leadership.

func (*LeaderGate) Set

func (g *LeaderGate) Set(leader bool)

Set flips the leadership flag (called from the election callbacks).

type MaskScript

type MaskScript struct {
	Name string `json:"name"`
	SQL  string `json:"sql"`
}

MaskScript is one per-source masking statement, applied in order inside every new/reset branch before it is marked ready.

type Ready

type Ready func(ctx context.Context) error

Ready reports whether branchd can serve traffic: the registry is reachable and the container driver responds. Returns nil when ready, an error otherwise. branchd supplies a closure; tests inject a fake.

type RefreshSourceRequest

type RefreshSourceRequest struct {
	Password string `json:"password"`
}

type Server

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

func New

func New(eng *engine.Engine, reg *registry.Registry, token string, metricsHandler http.Handler, ready Ready, stuckTimeout time.Duration) *Server

New builds the API server. metricsHandler serves /metrics (promhttp over the metrics registry) and ready backs /readyz; both may be nil (then /metrics 404s and /readyz reports ready iff the handler is wired). branchd always passes both. stuckTimeout is the reconcile cutoff for stuck creating/ resetting rows (0 → DefaultStuckTimeout).

func (*Server) Handler

func (s *Server) Handler() http.Handler

func (*Server) LeaderGate

func (s *Server) LeaderGate() *LeaderGate

LeaderGate exposes the HA mutating-route gate so branchd's leader-election orchestration can flip it on gaining/losing leadership. With leader election off it stays leader=true (single-instance default) and is never touched.

type Source

type Source struct {
	Name      string `json:"name"`
	PGVersion string `json:"pg_version"`
	Host      string `json:"host"`
	Port      int    `json:"port"`
	User      string `json:"user"`
	Database  string `json:"database"`
	Network   string `json:"network,omitempty"`
	// Via is the seeding method: "basebackup" (pg_basebackup) or "dump"
	// (pg_dump — managed Postgres without REPLICATION privilege).
	Via         string   `json:"via"`
	DumpSchemas []string `json:"dump_schemas,omitempty"`
	State       string   `json:"state"`
	Generation  int      `json:"generation"`
	CreatedAt   string   `json:"created_at"`
}

type Token

type Token struct {
	Name      string `json:"name"`
	Role      string `json:"role"`
	CreatedAt string `json:"created_at"`
}

Token is a stored token's metadata — never the plaintext or its hash.

Jump to

Keyboard shortcuts

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