httpserver

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 29 Imported by: 1 module, 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 httpserver serves OpenFlange pipes with the HTTP Pipe Protocol (openflange.org/http/v0.1): versioned discovery, execution sessions, one independent streaming request per port, an authoritative session status, expiry and cancellation.

srv := httpserver.New(httpserver.Config{Registry: eng.Registry, Resolver: eng.Resolver, EvalExpr: openflange.EvalExpr})
if _, err := srv.Add(ctx, "./examples/email/normalize-and-mask-email"); err != nil { ... }
err := srv.Serve(ctx, listener) // until ctx is done; sessions are cancelled on shutdown

A Server is an http.Handler, so it can also be mounted in an existing HTTP server; call Close when done.

Security model. Served pipes run with exactly the grants in Config (Grants / Trusted): a client can choose parameters and ports, never permissions, and never uploads code. Config.Token, when set, is required as a bearer token for discovery and session creation; every session gets its own random, short-lived bearer token that is valid for that session only. Credentials are accepted in the Authorization header only: a request with a query string is rejected. The server speaks plain HTTP; TLS termination is the operator's job, and intermediaries must not buffer requests or responses.

Index

Constants

View Source
const (
	DefaultIdleTTL         = 60 * time.Second
	DefaultRetention       = 60 * time.Second
	DefaultMaxSessions     = 64
	DefaultMaxRequestBytes = 1 << 20
	DefaultPortBuffer      = 16
)

Defaults of Config.

View Source
const Spec = "openflange.org/http/v0.1"

Spec is the protocol identifier carried in the pipe list.

View Source
const StatusTrailer = "OpenFlange-Status"

StatusTrailer is the advisory trailer of output responses.

Variables

This section is empty.

Functions

func IsLoopbackAddr

func IsLoopbackAddr(addr string) bool

IsLoopbackAddr reports whether a listen address ("host:port") binds only loopback interfaces.

Types

type Access

type Access struct {
	Time     time.Time     `json:"time"`
	Method   string        `json:"method"`
	Path     string        `json:"path"`
	Status   int           `json:"status"`
	Bytes    int64         `json:"bytes"`
	Duration time.Duration `json:"-"`
	Millis   int64         `json:"durationMs"`
	Remote   string        `json:"remote"`
	// Aborted is set when a streaming response was cut off (failed or
	// cancelled session, or the client went away).
	Aborted bool `json:"aborted,omitempty"`
}

Access is one access-log entry.

type Config

type Config struct {
	// Registry provides adapters and builtin pipes. Required.
	Registry *adapter.Registry
	// Resolver resolves the references of served pipelines (default: local
	// paths, std: and builtin:).
	Resolver pipeline.Resolver
	// EvalExpr evaluates `$expr` parameter bindings of served pipelines.
	EvalExpr func(ctx context.Context, expr string, params map[string]any) (any, error)
	// Grants are the permissions every served pipe runs with; Trusted grants
	// everything the pipes declare (not sandbox-enforced). Clients cannot
	// widen them.
	Grants  manifest.Permissions
	Trusted bool
	// Env and Secrets are the providers for `$env` and `$secret` bindings of
	// served pipelines (default: the process environment).
	Env     func(string) (string, bool)
	Secrets func(string) (string, bool)

	// Token, when non-empty, must be presented as `Authorization: Bearer` for
	// discovery and session creation.
	Token string
	// BasePath is the URL prefix of the protocol ("{base}" without scheme and
	// host), e.g. "/v1". Default: "".
	BasePath string
	// PublicURL is the externally visible base URL (scheme, host and base
	// path), used for the informative `url`/`urls` members when the server
	// is behind a proxy. Default: derived from each request.
	PublicURL string
	// Revision identifies the deployed implementation in definitions.
	// Default: a hash of the served pipe's package contents.
	Revision string

	// IdleTTL is how long a session may stay without any attachment before
	// it expires, and how long an enabled port may stay unattached once the
	// session has started (default 60 s).
	IdleTTL time.Duration
	// Retention is how long a terminal session stays retrievable (default
	// 60 s); afterwards its URLs answer 410.
	Retention time.Duration
	// MaxDuration bounds the total running time of a session (0 = no limit);
	// exceeding it fails the session with E_TIMEOUT.
	MaxDuration time.Duration
	// MaxSessions bounds concurrently open (non-terminal) sessions (default
	// 64); beyond it session creation answers 429.
	MaxSessions int
	// MaxRequestBytes bounds JSON request bodies (default 1 MiB).
	MaxRequestBytes int64
	// MaxInputBytes bounds each input stream (0 = no limit); exceeding it
	// fails the session with E_RESOURCE.
	MaxInputBytes int64
	// PortBuffer is the number of batches buffered between an HTTP stream
	// and the pipe, per port (default 16). Buffers are bounded: a slow
	// reader blocks the pipe, a slow pipe stops the server reading inputs.
	PortBuffer int

	// Log, when set, receives one entry per finished request. Session ids
	// are abbreviated and credentials never appear.
	Log func(Access)
}

Config configures a Server. The zero value of every optional field selects the documented default.

type PipeInfo

type PipeInfo struct {
	ID       string `json:"id"`
	Version  string `json:"version"`
	Title    string `json:"title,omitempty"`
	Revision string `json:"revision,omitempty"`
	// Target is what is executed (a path or reference).
	Target  string   `json:"target"`
	Inputs  []string `json:"inputs"`
	Outputs []string `json:"outputs"`
}

PipeInfo describes one served pipe.

type Server

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

Server is an HTTP Pipe Protocol server. Create it with New.

func New

func New(cfg Config) *Server

New creates a Server. Add pipes before serving.

func (*Server) Add

func (s *Server) Add(ctx context.Context, target string) (*PipeInfo, error)

Add serves one target: a pipe directory, a manifest file, a standalone pipeline file, or a reference such as `std:filter`. The target is compiled once with the server's grants so that a pipe that cannot run (missing grant, missing interpreter, invalid pipeline) is refused now rather than in every session.

func (*Server) AddDir

func (s *Server) AddDir(ctx context.Context, dir string) ([]*PipeInfo, error)

AddDir serves every pipe discovered in a package directory (see manifest.Discover).

func (*Server) AddManifest

func (s *Server) AddManifest(ctx context.Context, m *manifest.Manifest) (*PipeInfo, error)

AddManifest serves an in-memory manifest, for example a builtin pipe an application registered with adapter.Registry.RegisterBuiltin.

func (*Server) Close

func (s *Server) Close()

Close cancels every session, waits for their pipes to stop and releases all timers. New sessions are refused with 503 afterwards. Idempotent.

func (*Server) Pipes

func (s *Server) Pipes() []PipeInfo

Pipes lists the served pipes, sorted by id.

func (*Server) Serve

func (s *Server) Serve(ctx context.Context, l net.Listener) error

Serve accepts connections on l until ctx is done, then cancels all sessions and shuts the listener down. It returns nil after a clean shutdown.

func (*Server) ServeHTTP

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

ServeHTTP implements http.Handler: the seven routes of http/v0.1 §3.

func (*Server) Sessions

func (s *Server) Sessions() int

Sessions returns the number of open (non-terminal) sessions.

Jump to

Keyboard shortcuts

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