web

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2026 License: Apache-2.0 Imports: 33 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 web is the bweb server: a read-only HTTP face over sites kept in bstore buckets. It answers GET and HEAD for anyone, takes each site's behavior from that site's settings, and reads through a Store.

Index

Constants

View Source
const HealthPath = "/.well-known/bweb/health"

HealthPath answers 200 on any host name.

View Source
const StatusClientClosed = 499

StatusClientClosed is the status a request is counted under when its reader left before any answer was written. It is never sent.

Variables

View Source
var ErrBusy = errors.New("store: too many reads in flight")

ErrBusy is a read refused because every slot to the store stayed taken for the whole wait.

View Source
var ErrSlowDown = errors.New("store: the store refused the read (503)")

ErrSlowDown is a read the store refused because it is shedding load.

Functions

This section is empty.

Types

type Answer

type Answer struct {
	Status int
	Header http.Header // Content-Type, Content-Length, ETag, Last-Modified, Content-Range, Accept-Ranges
	Root   string      // the object's closure root, hex, when the store names it
	Body   io.ReadCloser
}

Answer is a store's answer to one read. Body is never nil; the caller closes it.

type Config

type Config struct {
	// Listen is the address the server answers on. A web server the
	// operator already runs sits in front of it and supplies TLS.
	Listen string `json:"listen"`
	// Store names where objects are read from.
	Store StoreConfig `json:"store"`
	// MetricsListen is an address the server's metrics are served on
	// (/metrics, Prometheus form); PprofListen one for Go's profiles
	// (/debug/pprof). Loopback or a private address: neither is for
	// readers. Optional.
	MetricsListen string `json:"metrics_listen,omitempty"`
	PprofListen   string `json:"pprof_listen,omitempty"`
	// DenyFile is a list of what this server refuses to serve, one entry a
	// line (see Deny). Optional.
	DenyFile string `json:"deny_file,omitempty"`
	// Engine names the node's own overlay engine, where a site's record is
	// read from. Required when a site names no bucket.
	Engine EngineConfig `json:"engine,omitzero"`
	// Host is the node's own wallet and its view of the chain, for what a
	// host publishes itself: its serving advertisements. Optional; a node
	// without it serves and advertises nothing.
	Host HostConfig `json:"host,omitzero"`
	// Publisher is a site owner's side: the wallet that pays for a site's
	// tokens and the engines they are submitted to (bweb site). A node
	// that only serves has none.
	Publisher PublisherConfig `json:"publisher,omitzero"`
	// Sites are the sites served. A site that names a bucket takes its
	// settings from this file. A site that names none is read from its
	// record (docs/spec.md section 2): its id is the site id, 64 lower-case
	// hex digits, and everything but hosts comes from the record.
	Sites []Site `json:"sites"`
}

Config is the server's configuration file.

func LoadConfig

func LoadConfig(path string) (*Config, error)

LoadConfig reads and checks a configuration file.

type Deny

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

Deny is what a server refuses to serve: a site by its identifier, an object by its closure root, an object by its content hash. One entry a line, "site:<id>", "root:<hex>" or "sha256:<hex>"; blank lines and lines starting with # are skipped. The file is read again when it changes.

func OpenDeny

func OpenDeny(path string) (*Deny, error)

OpenDeny reads a deny list; an empty path is a list that denies nothing.

func (*Deny) Has

func (d *Deny) Has(kind, value string) bool

Has reports whether kind:value is denied.

type EngineConfig added in v0.3.0

type EngineConfig struct {
	// URL is the engine's base URL, such as http://127.0.0.1:8280.
	URL string `json:"url,omitempty"`
	// RefreshMS is how often each site's record is read again (default
	// 2000): how soon a new token's settings are served.
	RefreshMS int `json:"refresh_ms,omitempty"`
	// AskTopics has the server ask the engine to carry each record site's
	// topics (POST /admin/topics, engines with topics on demand): the
	// site's own topic and, once its record names the bucket, the bucket's
	// two. Off, the engine's own settings must name them.
	AskTopics bool `json:"ask_topics,omitempty"`
	// AskTTLSeconds is an ask's lifetime (default 600); it is asked again
	// at half of it. An engine refuses one above its own bound.
	AskTTLSeconds int `json:"ask_ttl_seconds,omitempty"`
}

EngineConfig names the overlay engine on the same host.

type HeaderRule

type HeaderRule struct {
	Prefix string            `json:"prefix,omitempty"`
	Suffix string            `json:"suffix,omitempty"`
	Set    map[string]string `json:"set"`
}

HeaderRule sets response headers on paths with a prefix and a suffix (either may be empty).

type HostConfig added in v0.3.0

type HostConfig struct {
	// Data is the directory that holds the wallet (bweb init) and the
	// host's state. Mode 0700; it holds a private key.
	Data string `json:"data,omitempty"`
	// Network is main, test or regtest.
	Network string `json:"network,omitempty"`
	// Chain is the chain view (woc:main, woc:test, asset:URL); empty is
	// woc:<network>. Settle is the settlement leg (arcade:URL, arc:URL,
	// rpc:URL); empty is the network's public arcade. Headers is the
	// header source; empty is woc:<network>. A regtest chain names all
	// three.
	Chain   string `json:"chain,omitempty"`
	Settle  string `json:"settle,omitempty"`
	Headers string `json:"headers,omitempty"`
	// ArcadeKey is the bearer token of an arcade or ARC leg; RPCUser and
	// RPCPassFile authenticate an rpc: leg.
	ArcadeKey   string `json:"arcade_key,omitempty"`
	RPCUser     string `json:"rpc_user,omitempty"`
	RPCPassFile string `json:"rpc_pass_file,omitempty"`
	// Fee is the miner fee policy, bstore's fee block; absent is 100
	// satoshis per 1,000 bytes.
	Fee *feepolicy.Config `json:"fee,omitempty"`

	// URL is where this node serves, as readers and auditors reach it: a
	// scheme, a host and optionally a port, nothing else. Set, the node
	// advertises the record sites it serves (docs/spec.md section 4);
	// empty, it advertises nothing.
	URL string `json:"url,omitempty"`
	// Location is the node's IATA metro code, Country its ISO 3166-1
	// alpha-2 code and Subdivision its ISO 3166-2 code (optional).
	Location    string `json:"location,omitempty"`
	Country     string `json:"country,omitempty"`
	Subdivision string `json:"subdivision,omitempty"`
	// Advertise says which served sites are advertised: "terms" (the
	// default), those with hosting terms open; "always", every one.
	Advertise string `json:"advertise,omitempty"`
	// TermSeconds is how long an advertisement lasts (default 86400, at
	// least 600). RenewBeforeSeconds is how long before its expiry it is
	// renewed (default a quarter of the term).
	TermSeconds        int `json:"term_seconds,omitempty"`
	RenewBeforeSeconds int `json:"renew_before_seconds,omitempty"`
	// contains filtered or unexported fields
}

HostConfig is a host's wallet and chain settings. The chain keys are bstore's (bstore docs/provider.md, chaincfg): the same keys mean the same thing.

func (HostConfig) Spec added in v0.3.0

func (h HostConfig) Spec() chaincfg.Spec

Spec is the chain settings as bstore's chaincfg reads them.

type Metrics added in v0.3.0

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

Metrics are the server's counters, in Prometheus form. Every method is safe on a nil Metrics, so a server built without one runs unchanged.

What they are for, by the USE method: utilization (requests and reads in flight, read slots in use against their bound), saturation (the wait for a read slot, reads refused busy, the store's SlowDown), errors (answers by status, store reads by result).

func NewMetrics added in v0.3.0

func NewMetrics(version string) *Metrics

NewMetrics are metrics for one server, with the Go runtime's and the process's own.

func (*Metrics) Ad added in v0.3.0

func (m *Metrics) Ad(event string)

Ad counts an advertisement event: minted, mined, published or dropped.

func (*Metrics) Handler added in v0.3.0

func (m *Metrics) Handler() http.Handler

Handler serves the metrics.

func (*Metrics) Wallet added in v0.3.0

func (m *Metrics) Wallet(outputs int, satoshis uint64)

Wallet records the host wallet's coin.

type PublisherConfig added in v0.3.0

type PublisherConfig struct {
	HostConfig
	// IdentityFile is the bucket owner's identity: a site's key is its
	// bucket owner's key. Empty, the identity is the one bweb init made
	// under data. The coin is kept under data either way, apart from the
	// owner's bstore wallet.
	IdentityFile string `json:"identity_file,omitempty"`
	// Engines are the overlay engines a site's tokens are submitted to.
	Engines []string `json:"engines,omitempty"`
}

PublisherConfig is a site owner's wallet, chain settings and engines. The wallet and chain keys are HostConfig's; the advertisement keys are not a publisher's and are refused.

type Records added in v0.3.0

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

Records keeps the server's sites current with their records: for each site the configuration names without a bucket, it asks the node's own overlay engine (ls_bweb) for the site's latest token and serves the settings that token carries.

The engine admitted the token against the chain's headers, and it is the node's own, asked over loopback: Records checks that the token is the site's (its id, its owner's lock and signature) and does not check the proof again.

A site with no token (not yet arrived, or closed) is not served. An engine that does not answer changes nothing: the last settings read stand, so an engine restart does not take a site down.

func NewRecords added in v0.3.0

func NewRecords(c *Config, srv *Server) *Records

NewRecords watches the record sites of c for srv. It returns nil when c names none.

func (*Records) Poll added in v0.3.0

func (r *Records) Poll(ctx context.Context)

Poll reads every site's record once and serves what it found.

func (*Records) Run added in v0.3.0

func (r *Records) Run(ctx context.Context)

Run reads every site's record at once and again every refresh interval, until ctx is done.

func (*Records) Served added in v0.3.0

func (r *Records) Served() []string

Served are the record sites served now: those with a token that says public. A site's id is in hex.

func (*Records) Sites added in v0.3.0

func (r *Records) Sites() []string

Sites are the ids of the sites read from their records, in hex.

type S3Store

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

S3Store reads from an S3 endpoint, path style, Signature Version 4.

func NewS3Store

func NewS3Store(c StoreConfig) (*S3Store, error)

NewS3Store is a store over the endpoint c names.

func (*S3Store) Instrument added in v0.3.0

func (s *S3Store) Instrument(m *Metrics)

Instrument has the store count its reads in m.

func (*S3Store) Read

func (s *S3Store) Read(ctx context.Context, bucket, key string, head bool, cond http.Header) (*Answer, error)

type Server

type Server struct {

	// Metrics, when set, counts every answer.
	Metrics *Metrics
	// contains filtered or unexported fields
}

Server answers readers.

func NewServer

func NewServer(c *Config, store Store, deny *Deny) *Server

NewServer is a server for the sites of c, reading from store.

func (*Server) ServeHTTP

func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Server) SetSites added in v0.3.0

func (s *Server) SetSites(sites []*Site)

SetSites replaces the sites served, by host. A name two sites carry goes to the first. Requests in flight finish on the site they started with.

type Site

type Site struct {
	ID string `json:"id"` // the site identifier; the site id in hex when read from its record
	// Hosts are the names it answers to, lower case, no port. For a site
	// read from its record they are optional: given, they are the names
	// this node binds to the site, whatever the record claims; omitted,
	// the record's names are used.
	Hosts  []string `json:"hosts,omitempty"`
	Bucket string   `json:"bucket,omitempty"` // the bucket's S3 name; empty to read the site from its record
	Index  string   `json:"index"`            // the index document suffix, "index.html"
	Error  string   `json:"error"`            // the error document key, "404.html"; optional
	Public bool     `json:"public"`           // only a public site is served
	// Headers are response headers set by path, first match of each name
	// wins, in order.
	Headers []HeaderRule `json:"headers,omitempty"`
}

Site is one site's settings: the S3 website configuration, with the names it answers to and the bucket it reads.

type Store

type Store interface {
	// Read asks for an object. head asks for no body. cond carries the
	// request's Range and conditional headers. The answer's Status is 200,
	// 206, 304, 404, 412 or 416; anything else is an error.
	Read(ctx context.Context, bucket, key string, head bool, cond http.Header) (*Answer, error)
}

Store is where the server reads objects. The S3 store reads through the node's bstore gateway, which proves every answer before it gives it; a reader linked in process is the store to come.

type StoreConfig

type StoreConfig struct {
	Endpoint   string `json:"endpoint"` // http://127.0.0.1:9000
	Region     string `json:"region"`
	AccessKey  string `json:"access_key"`
	Secret     string `json:"secret,omitempty"`
	SecretFile string `json:"secret_file,omitempty"`
	// MaxReads bounds the reads in flight to the store (default 24). A
	// gateway sheds reads past its own bound (bstore's limits.reads, 32 by
	// default); staying under it turns a burst into a short wait here
	// instead of refusals there.
	MaxReads int `json:"max_reads,omitempty"`
	// ReadWaitMS is how long a read waits for a slot before it is refused
	// with 503 (default 2000).
	ReadWaitMS int `json:"read_wait_ms,omitempty"`
}

StoreConfig names an S3 endpoint: the node's bstore gateway.

Jump to

Keyboard shortcuts

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