{"modulePath":"github.com/bborbe/vault-api","version":"v0.46.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package handler\n\nPackage handler owns the HTTP surface: the five canonical admin endpoints and the typed /api/v1 read endpoints.\n\n## Constants\n\n```go\nconst DefaultLeaseSeconds = 300\n```\nDefaultLeaseSeconds is the lease a claim gets when the caller names none. Five minutes is long enough that a working run renews once rather than continuously, and short enough that a run that died frees its key quickly instead of blocking it until someone notices.\n\n```go\nconst MaxLeaseSeconds = 86400\n```\nMaxLeaseSeconds bounds a requested lease. The bound exists because time.Duration(ttl) \\* time.Second overflows int64 nanoseconds above ~9.2e9 s, and an overflowed lease renders lease\\_until in the past — a claim that is expired the moment it is taken. A day is longer than any run needs.\n\n```go\nconst RenewLeadSeconds = 60\n```\nRenewLeadSeconds is how long before expiry a holder should renew. The response states the instant rather than leaving the caller to re-derive it.\n\n## Variables\n\n```go\nvar MergeCallerLog = glog.Infof\n```\nMergeCallerLog records one line per merge request naming its caller. It is a variable rather than a direct glog call, matching vaultapi.MergeLog, so the specs can assert the line without depending on glog's file sinks. Production never reassigns it.\n\n## Functions\n\n```go\nfunc AcquireClaimHandler(\n\tdeps ClaimDeps,\n\tscope func(*http.Request) (ClaimScope, bool),\n) http.HandlerFunc\n```\nAcquireClaimHandler takes a claim. A conflict means another holder has it and answers 409 claim\\_held with the holder that won.\n\n```go\nfunc ListClaimsHandler(deps ClaimDeps) http.HandlerFunc\n```\nListClaimsHandler enumerates claims, optionally narrowed to one holder.\n\nThe route exists because a claim surface that can only be read one key at a time cannot answer the question a restarting executor actually asks: which claims do I already hold? Without it, an executor that restarts cannot see the refs it left behind and has to wait out every lease instead of re-adopting or fencing its own work.\n\nFiltering by holder is a convenience, not a security boundary: the gateway secret is what separates callers, and a holder that names someone else simply sees someone else's claims. It is offered because the caller's own filter is the common case and doing it server-side keeps the response small.\n\nAn expired claim is reported with its stored lease\\_until rather than flagged or omitted. Expiry is a comparison against the caller's own clock, and computing it here would bake this server's clock into an answer that goes stale the moment it is sent — the caller already has lease\\_until and knows what now is.\n\n```go\nfunc NewHandler(\n\tctx context.Context,\n\tindex *vaultapi.Index,\n\tstore storage.PageStorage,\n\tgatewaySecret string,\n\tvaultPath string,\n\ttasksDir string,\n\tvaultName string,\n\twriter gitrest.Client,\n\tclock libtime.CurrentDateTime,\n\ttaskPublisher publisher.TaskPublisher,\n\trefs gitrest.RefClient,\n\tlinks *vaultapi.LinkRegistry,\n\tchanges ...*stream.Hub,\n) http.Handler\n```\nNewHandler builds the service router. When gatewaySecret is non-empty every /api/v1/\\* request must carry it in X-Gateway-Secret; the admin endpoints stay unauthenticated, matching the fleet's contract.\n\nThe write endpoints need five things the read endpoints do not: vaultPath, to read the original bytes before re-rendering them; vaultName, which stamps the change event so a consumer sharing one topic across vaults can tell its own tasks apart; writer, to hand the rendered file to git-rest; clock, so an approval's instant is injected rather than read from the wall clock; and taskPublisher, which turns a landed write into a change event.\n\n```go\nfunc PRClaimScope() func(*http.Request) (ClaimScope, bool)\n```\nPRClaimScope resolves {owner}, {repo} and {number} to a per-PR claim scope. Like a repository, a pull request needs no index entry to be claimed.\n\n```go\nfunc ReadClaimHandler(\n\tdeps ClaimDeps,\n\tscope func(*http.Request) (ClaimScope, bool),\n) http.HandlerFunc\n```\nReadClaimHandler reports the current holder, or 404 when the claim is free.\n\n```go\nfunc ReleaseClaimHandler(\n\tdeps ClaimDeps,\n\tscope func(*http.Request) (ClaimScope, bool),\n) http.HandlerFunc\n```\nReleaseClaimHandler releases a claim. The token is mandatory: releasing someone else's claim because you hold a stale one is the failure the compare-and-swap exists to prevent.\n\n```go\nfunc RenewClaimHandler(\n\tdeps ClaimDeps,\n\tscope func(*http.Request) (ClaimScope, bool),\n) http.HandlerFunc\n```\nRenewClaimHandler extends a held claim. A conflict means the lease was lost and answers 409 lease\\_lost.\n\n```go\nfunc RepoClaimScope() func(*http.Request) (ClaimScope, bool)\n```\nRepoClaimScope resolves the {owner} and {repo} path variables to a repo claim scope. It fails only for a name that cannot be part of a claim key: a repository needs no index entry to be claimed.\n\n```go\nfunc TaskClaimScope(index *vaultapi.Index) func(*http.Request) (ClaimScope, bool)\n```\nTaskClaimScope resolves the {id} path variable to a task claim scope.\n\n## Types\n\n```go\ntype ClaimDeps struct {\n\tRefs  gitrest.RefClient\n\tClock libtime.CurrentDateTime\n}\n```\nClaimDeps is what the claim handlers need: the index to resolve an identifier, the ref client that carries the claim to git-rest, and an injected clock so a lease instant is computed from a supplied time rather than the wall clock.\n\n```go\ntype ClaimListResponse struct {\n\tItems   []ClaimSummary `json:\"items\"`\n\tTotal   int            `json:\"total\"`\n\tSkipped int            `json:\"skipped\"`\n}\n```\nClaimListResponse is the body of GET /api/v1/claims.\n\nIt carries no index\\_age\\_seconds, unlike the task and goal listings: claims are read live from git-rest and no index describes them, so an age here would be a fabricated number a caller could gate on. Skipped counts refs under the claims prefix that are not addressable claims — counted rather than dropped, for the same reason the task index counts unparsable pages instead of coercing them.\n\n```go\ntype ClaimScope struct {\n\tScope string\n\tID    string\n\tKey   string\n}\n```\nClaimScope is one claim's address: the key the transport stores it under and the identifier the typed API answers with.\n\n```go\nfunc NewClaimScope(scope string, id string, key string) ClaimScope\n```\nNewClaimScope builds an address for a claim. It is exported so the handlers can be exercised without an index: the resolver's job is to produce one of these, and a test that supplies it directly is testing the handler, not the resolver.\n\n```go\ntype ClaimSummary struct {\n\tScope       string `json:\"scope\"`\n\tID          string `json:\"id\"`\n\tHolder      string `json:\"holder\"`\n\tTaskID      string `json:\"task_id\"`\n\tLeaseUntil  string `json:\"lease_until\"`\n\tRenewBefore string `json:\"renew_before\"`\n\tLeaseToken  string `json:\"lease_token\"`\n\t// SpawnedAt is absent on a claim whose holder has not created its Job.\n\tSpawnedAt string `json:\"spawned_at,omitempty\"`\n}\n```\nClaimSummary is one claim as the listing reports it.\n\nIt carries claimResponse's identity fields plus task\\_id, and it deliberately omits the transport key: the key is this API's business, never the caller's. A caller addresses a claim by scope and id, which is exactly what the renew and release routes accept — so a listed claim is actionable without the caller ever learning how it is stored.\n\ntask\\_id is the field that makes the listing worth having. A repository claim belongs to no task, so the claiming run's own task identifier is what the transport stores; a restarting executor groups its claims by it to recover one run's holdings together rather than key by key.\n\n```go\ntype PageDeps struct {\n\tLinks *vaultapi.LinkRegistry\n}\n```\nPageDeps is what the page routes need: the per-vault link indexes.\n\n```go\ntype TaskDocument struct {\n\tID          string         `json:\"id\"`\n\tTitle       string         `json:\"title\"`\n\tFrontmatter map[string]any `json:\"frontmatter\"`\n\tBody        string         `json:\"body\"`\n\t// Reopened marks a create that replaced a terminal occupant rather than\n\t// writing a free path. Absent on every ordinary create, so a caller that\n\t// ignores it is unaffected and one that logs it can record the reopen.\n\tReopened bool `json:\"reopened,omitempty\"`\n}\n```\nTaskDocument is one task read in full: the identifier the index resolves, the title, the whole frontmatter block and the markdown body. The listing serves the flat projection; this serves the page, which is what a caller that needs to read or rewrite a task's content actually wants.\n\n","path":"github.com/bborbe/vault-api/pkg/handler","name":"handler","synopsis":"Package handler owns the HTTP surface: the five canonical admin endpoints and the typed /api/v1 read endpoints.","isRedistributable":true}
