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
- func IsLoopbackAddr(addr string) bool
- type Access
- type Config
- type PipeInfo
- type Server
- func (s *Server) Add(ctx context.Context, target string) (*PipeInfo, error)
- func (s *Server) AddDir(ctx context.Context, dir string) ([]*PipeInfo, error)
- func (s *Server) AddManifest(ctx context.Context, m *manifest.Manifest) (*PipeInfo, error)
- func (s *Server) Close()
- func (s *Server) Pipes() []PipeInfo
- func (s *Server) Serve(ctx context.Context, l net.Listener) error
- func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *Server) Sessions() int
Constants ¶
const ( DefaultIdleTTL = 60 * time.Second DefaultRetention = 60 * time.Second DefaultMaxSessions = 64 DefaultMaxRequestBytes = 1 << 20 DefaultPortBuffer = 16 )
Defaults of Config.
const Spec = "openflange.org/http/v0.1"
Spec is the protocol identifier carried in the pipe list.
const StatusTrailer = "OpenFlange-Status"
StatusTrailer is the advisory trailer of output responses.
Variables ¶
This section is empty.
Functions ¶
func IsLoopbackAddr ¶
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 (*Server) Add ¶
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 ¶
AddDir serves every pipe discovered in a package directory (see manifest.Discover).
func (*Server) AddManifest ¶
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) Serve ¶
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.