Help us shape the future of Go! Take the 2026 Go Developer Survey

epbinding

package
v1.152.0 Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package epbinding is an endpoint's binding (#2171): the HTTP route a managed script answers at /endpoints/{name}/..., who may call it, and the limits each request runs under. It holds the record, its validation, the store contract and the matching of a request path against the binding's template; the store is internal/endpoint/epstore and the request surface internal/endpoint/ephttp.

Index

Constants

View Source
const (
	AuthAuthenticated = "authenticated"
	AuthPublic        = "public"
	AuthBotCheck      = "bot_check"
)

Access modes (#2259). An authenticated endpoint answers a platform API key or a browser session among its callers; a public one answers anyone with no check; a bot_check one answers anyone who passes the bot check it names.

View Source
const (
	CallersAll    = "*"
	CallerPersona = "persona:"
	CallerAPIKey  = "api_key:"
)

Callers an authenticated endpoint names: every authenticated caller, the callers whose persona is the named one, or the named API key.

View Source
const (
	DefaultTimeoutSeconds = 10
	MaxTimeoutSeconds     = 60

	DefaultMaxMemoryBytes int64 = 64 << 20
	MinMaxMemoryBytes     int64 = 1 << 20
	MaxMaxMemoryBytes     int64 = 1 << 30

	DefaultMaxBodyBytes = 1 << 20
	MaxMaxBodyBytes     = 10 << 20

	DefaultRateLimit       = 600
	DefaultCallerRateLimit = 120
	MaxRateLimit           = 100000
)

Limits a binding is held to, and the values one that names none gets.

View Source
const FailureRetention = 7 * 24 * time.Hour

FailureRetention is how long a failed request is kept.

View Source
const PathPrefix = "/endpoints/"

PathPrefix is where every endpoint is served: /endpoints/{name}/...

Variables

View Source
var (
	// ErrNotFound reports a script with no binding, or a name no binding has.
	ErrNotFound = errors.New("this script is not bound to an endpoint")
	// ErrNameTaken reports a name another script's binding already has.
	ErrNameTaken = errors.New("another script's endpoint has this name")
	// ErrNotAllowed reports a caller who may not create or change a binding:
	// everyone but an administrator, the script's owner included.
	ErrNotAllowed = errors.New("only an administrator can bind a script to an endpoint")
	// ErrNoSuchCheck reports a binding naming a bot check that does not
	// exist.
	ErrNoSuchCheck = errors.New("no bot check has this name; an administrator configures them under Admin > Settings > Bot checks")
)

Errors a binding is refused or looked up with.

AuthModes is every access mode, in the order they are offered.

View Source
var Methods = []string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}

Methods is every HTTP method a binding may answer.

Functions

func Callable

func Callable(source string) error

Callable refuses a source an endpoint cannot call: one that defines neither a top-level handle(req) taking one parameter nor a form's form_spec(req) and form_submit(req, values) (#2176). It is asked when a script is bound and when a bound script is saved (internal/platform/scriptsave).

Types

type Binding

type Binding struct {
	ID       string `json:"id"`
	ScriptID string `json:"script_id"`
	// Name is the endpoint's segment of /endpoints/{name}, unique across the
	// deployment.
	Name string `json:"name" example:"order-lookup"`
	// Methods are the HTTP methods it answers.
	Methods []string `json:"methods" example:"GET"`
	// Path is the template under /endpoints/{name}: "" for the name alone,
	// "/{id}" for one parameter a request reads as req.params["id"].
	Path string `json:"path" example:"/{id}"`
	// AuthMode is authenticated, public or bot_check.
	AuthMode string `json:"auth_mode" example:"authenticated"`
	// Callers is who an authenticated endpoint admits besides the
	// administrator: "*" for every authenticated caller, "persona:<name>" and
	// "api_key:<name>". Empty admits no one but the administrator.
	Callers []string `json:"callers" example:"persona:analyst"`
	// BotCheck is the named bot check a bot_check endpoint requires.
	BotCheck string `json:"bot_check,omitempty" example:"default"`
	// CheckedMethods are the methods a bot_check endpoint checks, empty for
	// every method it answers; HEAD is checked where GET is.
	CheckedMethods []string `json:"checked_methods"`
	// TimeoutSeconds, MaxMemoryBytes and MaxBodyBytes bound one request.
	TimeoutSeconds int   `json:"timeout_seconds"`
	MaxMemoryBytes int64 `json:"max_memory_bytes"`
	MaxBodyBytes   int   `json:"max_body_bytes"`
	// AllowedOrigins are the browser origins a cross-origin request may come
	// from; empty answers no preflight.
	AllowedOrigins []string `json:"allowed_origins"`
	// RateLimit is the endpoint's requests per minute on each replica, and
	// CallerRateLimit one signed-in caller's.
	RateLimit       int       `json:"rate_limit"`
	CallerRateLimit int       `json:"caller_rate_limit"`
	Enabled         bool      `json:"enabled"`
	CreatedBy       string    `json:"created_by,omitempty"`
	UpdatedBy       string    `json:"updated_by,omitempty"`
	CreatedAt       time.Time `json:"created_at"`
	UpdatedAt       time.Time `json:"updated_at"`
}

Binding is one script's endpoint.

func Build

func Build(sc *script.Script, prev *Binding, req Request, now time.Time) (*Binding, error)

Build is the binding req sets on sc over prev, which is nil for a script not bound yet. It refuses a library, which nothing calls, and everything Validate refuses.

func (Binding) Admits added in v1.152.0

func (b Binding) Admits(c Caller) bool

Admits reports whether an authenticated endpoint admits c: the administrator always, anyone else when the callers list names them.

func (Binding) Answers

func (b Binding) Answers(method string) bool

Answers reports whether the binding answers method. HEAD is answered wherever GET is.

func (Binding) Checks added in v1.152.0

func (b Binding) Checks(method string) bool

Checks reports whether a request with method must pass the binding's bot check.

func (Binding) Match

func (b Binding) Match(rest string) (map[string]string, bool)

Match reports whether rest, the part of a request path after /endpoints/{name}, is the binding's template, and the parameter values it carries.

func (Binding) URLPath

func (b Binding) URLPath() string

URLPath is the path a request reaches the endpoint at, with the template's parameters in braces.

func (Binding) Validate

func (b Binding) Validate() error

Validate reports the first thing wrong with the binding.

type Caller added in v1.152.0

type Caller struct {
	// Admin is whether the caller is the platform's administrator, whom every
	// authenticated endpoint admits whatever its callers say: an
	// administrator is unrestricted on every surface.
	Admin bool
	// Persona is the persona the caller's roles resolve to, empty when none.
	Persona string
	// APIKey is the name of the API key the request carried, empty for a
	// browser session.
	APIKey string
}

Caller is who sent a request to an authenticated endpoint, as the binding's callers list is matched against it.

type Failure

type Failure struct {
	ID         string    `json:"id"`
	EndpointID string    `json:"endpoint_id"`
	Version    int       `json:"version"`
	Method     string    `json:"method"`
	Status     int       `json:"status"`
	Error      string    `json:"error"`
	DurationMS int64     `json:"duration_ms"`
	CreatedAt  time.Time `json:"created_at"`
}

Failure is one request that failed, kept for the script's owner to read.

type Filter

type Filter struct {
	ScriptIDs []string
	AuthMode  string
	Enabled   *bool
	Limit     int
	Offset    int
}

Filter narrows a listing. A non-nil empty ScriptIDs matches nothing. An empty AuthMode and a nil Enabled match every binding. Limit is a page's size, bounded by the store, and Offset how many to pass over (#2251).

type Request

type Request struct {
	Name     string
	Methods  []string
	Path     *string
	AuthMode string
	// Callers, BotCheck and CheckedMethods are nil or empty to keep what the
	// binding had; a change of mode clears what the old mode named.
	Callers         []string
	BotCheck        string
	CheckedMethods  []string
	TimeoutSeconds  int
	MaxMemoryBytes  int64
	MaxBodyBytes    int
	AllowedOrigins  []string
	RateLimit       int
	CallerRateLimit int
	Enabled         *bool
	Actor           string
}

Request is a binding set on a script. A nil field keeps what the binding had, or takes the default on a new one.

type Stamp

type Stamp struct {
	Binding time.Time
	Script  time.Time
}

Stamp is when a binding and its script were last written. Every write of a script row moves its updated_at, a new version included, so two equal stamps mean nothing a request runs has changed.

type Store

type Store interface {
	// Set creates or replaces sc's binding, keyed by its script; a name
	// another script's binding has is ErrNameTaken.
	Set(ctx context.Context, b *Binding) error
	// Get is a script's binding, ErrNotFound when it has none.
	Get(ctx context.Context, scriptID string) (*Binding, error)
	// GetByName is the binding a name serves, ErrNotFound when none does.
	GetByName(ctx context.Context, name string) (*Binding, error)
	// List is one page of the bindings filter matches, by name, and how
	// many it matches in all.
	List(ctx context.Context, filter Filter) (page []Binding, total int, err error)
	// SetEnabled turns a script's binding on or off.
	SetEnabled(ctx context.Context, scriptID string, enabled bool, actor string) error
	// RecordFailure keeps one failed request.
	RecordFailure(ctx context.Context, f Failure) error
	// Failures is an endpoint's most recent failed requests, newest first.
	Failures(ctx context.Context, endpointID string, limit int) ([]Failure, error)
	// SweepFailures deletes the failures recorded before cutoff.
	SweepFailures(ctx context.Context, cutoff time.Time) (int64, error)
	// Stamp is when the binding a name serves, and the script it binds, were
	// last written: one read that tells a replica whether what it compiled
	// for the name is still current. ErrNotFound when no binding has it.
	Stamp(ctx context.Context, name string) (Stamp, error)
}

Store keeps bindings and the failed requests they answered.

Jump to

Keyboard shortcuts

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