@storiny/discovery
Source for the Discovery service, responsible for embedding and rendering third-party content
Source for the Discovery service, responsible for embedding and rendering third-party content on Storiny.
Tech stack
- Framework: Actix Web serves our HTTP endpoints and middlewares
- State & caching: Redis handles rate limiting
- Metadata & DOM parsing: Visdom, html5ever, and
markup5ever_rcdomfor parsing, querying and safely manipulating HTML DOM nodes - HTTP client: reqwest for making HTTP requests to oEmbed endpoints and web pages
- Serialization:
serdeandserde_jsonfor mapping oEmbed specifications into typed Rust structures - Telemetry: Sentry for tracing, monitoring, and capturing errors in production
URL discovery & oEmbed
The heart of this repository is its embed resolution logic. Whenever a user pastes a link into a story/document, this service determines exactly how to render it:
- as a rich interactive iframe
- a native video
- a photo
- or a standard link card
Under the hood
Request & URL decompression: When the client requests an embed from the frontend, they pass an LZ-compressed URL to our
/embed/{compressed_url}endpoint. Inside the route handler, we uselz-strto decompress the string back into a valid URL. This simplifies caching behaviour as all of our/embed/endpoints are heavily cached to prevent third-party rate limits.Provider resolution: We maintain a curated registry of supported oEmbed providers in
providers.jsonfile. The decompressed URL is matched against pre compiled Regex schemas to see if it belongs to a known provider (like YouTube, Twitter, Spotify…).- If a match is found, we know exactly which
endpointto hit and whether the provider supports light/dark binary themes. - If no match is found, we immediately fallback to our custom metadata scraper.
- If a match is found, we know exactly which
oEmbed fetching & parsing: For supported providers, we make a request to their oEmbed endpoint. The JSON response is parsed into our
EmbedResponseandEmbedTypeenums (Photo,Video,Link,Rich).- Based on the
EmbedType, we parse the returned HTML usingvisdom, inject our own iframe styling, apply responsive aspect ratio paddings, and safely sanitize the output. - The heavily customized HTML (or raw JSON for scripted embeds) is then piped through the
sailfishtemplating engine and returned to the client.
- Based on the
Metadata fallback: If the link isn’t supported by an oEmbed provider or if the provider returns a generic
Linktype, we fall back toget_metadata. This utility fetches the raw HTML of the webpage and extracts opengraph (og:) tags, Twitter card tags, titles and descriptions; returning a JSON metadata payload for rendering link preview cards on the frontend.
Steps to launch a local instance of the service and a list of API endpoints are available in the repository README.