Documentation
¶
Overview ¶
Package embisto turns a URL into the data for a link preview: title, description, image, icon, video and the provider's embed HTML.
A preview goes through a fixed pipeline: fetch the page, run the generic sources over it, ask the site provider that knows its address, merge their facts field by field, enrich the result. New builds the pipeline from Options, which start from Defaults. The Core it returns is a card.Extractor, as is every layer around it, such as the cache in package cache, so layers stack in any order:
core := embisto.New(embisto.Defaults())
previews := cache.New(core, cache.NewMemory(64<<20, nil), cache.Options{TTL: time.Hour, FailureTTL: time.Minute})
result, err := previews.Extract(ctx, card.Request{URL: "https://github.com"})
The modules of each stage are in their own packages: site providers in sites, output formats in render, the resized pictures in thumbs. The contract they share is package card.
Index ¶
Constants ¶
const Version = "0.1.0"
Version is the library's release.
Variables ¶
This section is empty.
Functions ¶
func NewFetcher ¶
NewFetcher is the client the options configure, which refuses addresses that are not public; a fetcher of your own can wrap it, such as to record or to route through a proxy.
Types ¶
type Core ¶
type Core struct {
// contains filtered or unexported fields
}
Core is the pipeline. It keeps nothing between previews but what Options.HomePages stores.
type Observer ¶
type Observer interface {
// Fetched is one request a stage made, with its outcome: ok, a card code, or canceled. The stage is page, or the
// name of the source, provider or enricher that asked.
Fetched(stage, outcome string, took time.Duration)
// Matched is a provider asked about a URL it knows.
Matched(provider string)
// Made is a card made afresh, with its class (full, partial, minimal, wall or error) and the provider that shaped
// it, or generic.
Made(class, source string)
}
Observer hears what the pipeline does, for metrics. It is told names and codes, never URLs.
type Options ¶
type Options struct {
// Timeout bounds one preview, every request it makes included.
Timeout time.Duration
// MaxBodySize is the most bytes read of one response; a larger one fails with too_large.
MaxBodySize int64
MaxRedirects int
UserAgent string
// DefaultAcceptLanguage is the Accept-Language a page is asked in, unless a request names its own language; sites
// otherwise answer in the language of wherever the server runs.
DefaultAcceptLanguage string
// AllowedNetworks are reachable although not public, for a deployment that previews its own intranet.
AllowedNetworks []netip.Prefix
// Fetcher replaces the client the fields above configure, and with it the guard against non-public addresses.
Fetcher card.Fetcher
Sources []card.Source
// HomePages remembers what each site's home page says, so that a description or a picture a site puts on every
// page is not taken for one page's own. Nil reads no home page. A store shared between instances shares what
// they learn; purging a URL forgets its site.
HomePages cache.Store
// Providers know particular sites and are asked first, in order: the first that knows a URL is the only one asked.
Providers []card.Provider
// Fallbacks are asked only about a URL no provider knows, such as the provider for every site on the oEmbed
// list; so a provider added to Providers always wins over them.
Fallbacks []card.Provider
Enrichers []card.Enricher
// Log hears about sources and providers that failed; the preview goes on without them. Nil logs nothing.
Log *slog.Logger
// Observer hears what the pipeline does, for metrics; nil hears nothing.
Observer Observer
}
Options configure the pipeline. A zero number takes its default; a nil list takes the default modules, an empty one runs none.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package cache keeps previews: a layer around any card.Extractor, and the stores it keeps them in, in the process or shared between instances through Redis.
|
Package cache keeps previews: a layer around any card.Extractor, and the stores it keeps them in, in the process or shared between instances through Redis. |
|
Package card is the contract between embisto and its modules: the preview a client draws, the parsed page, the facts a source finds on it, and the interfaces of every stage of the pipeline.
|
Package card is the contract between embisto and its modules: the preview a client draws, the parsed page, the facts a source finds on it, and the interfaces of every stage of the pipeline. |
|
cmd
|
|
|
embisto
command
Command embisto serves link previews over HTTP.
|
Command embisto serves link previews over HTTP. |
|
internal
|
|
|
api
Package api is the HTTP surface of the library: huma routes, the problem+json error shape and request ids.
|
Package api is the HTTP surface of the library: huma routes, the problem+json error shape and request ids. |
|
assemble
Package assemble merges what the sources found into one preview, field by field, by a table of priorities.
|
Package assemble merges what the sources found into one preview, field by field, by a table of priorities. |
|
debug
Package debug serves a page that shows how a preview was made: the card, every fetch, source, provider and enricher with its time and outcome, which source won each field and why, and the image proxy's output.
|
Package debug serves a page that shows how a preview was made: the card, every fetch, source, provider and enricher with its time and outcome, which source won each field and why, and the image proxy's output. |
|
fetch
Package fetch is the only code that goes to the network: an HTTP client that refuses addresses which are not public, checked on every connection it opens, so neither a redirect nor a DNS answer that changes between lookups reaches the host's own network.
|
Package fetch is the only code that goes to the network: an HTTP client that refuses addresses which are not public, checked on every connection it opens, so neither a redirect nor a DNS answer that changes between lookups reaches the host's own network. |
|
fixture
Package fixture records what a preview fetched and replays it without the network, so a page saved once stays a test of the whole pipeline.
|
Package fixture records what a preview fetched and replays it without the network, so a page saved once stays a test of the whole pipeline. |
|
freshness
Package freshness reads how long an origin lets a response be kept, as a shared cache reads it.
|
Package freshness reads how long an origin lets a response be kept, as a shared cache reads it. |
|
homepage
Package homepage learns what a site says of every page from what its home page says: a description or a picture the home page shares with an item's page is the site's, not the item's.
|
Package homepage learns what a site says of every page from what its home page says: a description or a picture the home page shares with an item's page is the site's, not the item's. |
|
imagesize
Package imagesize measures a preview's pictures from the first bytes of their files, so a client can lay a card out before the pictures arrive.
|
Package imagesize measures a preview's pictures from the first bytes of their files, so a client can lay a card out before the pictures arrive. |
|
logging
Package logging writes JSON logs through slog and carries per-request attributes on the context.
|
Package logging writes JSON logs through slog and carries per-request attributes on the context. |
|
meta
Package meta parses a page's metadata and reads the generic sources from it: Open Graph, Twitter Cards and plain HTML.
|
Package meta parses a page's metadata and reads the generic sources from it: Open Graph, Twitter Cards and plain HTML. |
|
metrics
Package metrics counts what the server does for Prometheus.
|
Package metrics counts what the server does for Prometheus. |
|
oembed
Package oembed reads oEmbed: the endpoint a page links to, or the one the oembed.com provider list names for a URL.
|
Package oembed reads oEmbed: the endpoint a page links to, or the one the oembed.com provider list names for a URL. |
|
pipeline
Package pipeline makes a preview: fetch the page, run the generic sources over it, ask the site provider that knows it, merge their facts, enrich the result.
|
Package pipeline makes a preview: fetch the page, run the generic sources over it, ask the site provider that knows it, merge their facts, enrich the result. |
|
quality
Package quality classes a card by how good it came out, from the client's side.
|
Package quality classes a card by how good it came out, from the client's side. |
|
rediscache
Package rediscache keeps previews in Redis or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every instance, with the lock that lets them make one preview between them.
|
Package rediscache keeps previews in Redis or anything that speaks its protocol (Valkey, KeyDB, Dragonfly), shared by every instance, with the lock that lets them make one preview between them. |
|
settings
Package settings reads the server's configuration from the environment.
|
Package settings reads the server's configuration from the environment. |
|
target
Package target reads the URL and language of a request as the pipeline does, so that a cache keys equal requests written differently alike.
|
Package target reads the URL and language of a request as the pipeline does, so that a cache keys equal requests written differently alike. |
|
trace
Package trace records what the pipeline did for one preview, step by step, for the debug page.
|
Package trace records what the pipeline did for one preview, step by step, for the debug page. |
|
valkeytest
Package valkeytest starts a Valkey container for tests of the shared cache.
|
Package valkeytest starts a Valkey container for tests of the shared cache. |
|
Package render holds the output formats a preview is written in.
|
Package render holds the output formats a preview is written in. |
|
scripts
|
|
|
iframely-compare
command
Command iframely-compare asks a running Iframely and a running embisto about the same URLs and reports where their answers differ in status, in the fields they carry, in the groups of their links and in their meta values.
|
Command iframely-compare asks a running Iframely and a running embisto about the same URLs and reports where their answers differ in status, in the fields they carry, in the groups of their links and in their meta values. |
|
Package sites holds the site providers: each knows one site, or a family of them, and outranks the generic sources there.
|
Package sites holds the site providers: each knows one site, or a family of them, and outranks the generic sources there. |
|
Package thumbs serves the pictures of a preview through embisto, resized and re-encoded: sources that expire or refuse hotlinking stay viewable, clients never reveal their address to other sites and download no more pixels than they show.
|
Package thumbs serves the pictures of a preview through embisto, resized and re-encoded: sources that expire or refuse hotlinking stay viewable, clients never reveal their address to other sites and download no more pixels than they show. |