Skip to content

Commit

Permalink
rebased embedr (#3511)
Browse files Browse the repository at this point in the history
* skeleton of embedr service, based on bskyweb

* embedr container setup

* builds on this branch

* actual routes

* fix embedr go:embed

* tweak embedr dockerfile

* progress on embedr

* fix path params

* tweaks to build process

* try to get embedr dockerfile to install embed deps

* build this branch

* updates to match sam's output HTML

* try to unbreak embedr dockerfile

* small embedr tweak

* docker hack

* get embed.js copied over to embedr

* don't x-frame-options for embed.bsky.app

* bskyembed: remove a console.log

* use html/template for golang snippet generation

* simplify embedr API fetches

* missing file

* Rm console.log fully

---------

Co-authored-by: Dan Abramov <[email protected]>
  • Loading branch information
bnewbold and gaearon authored Apr 13, 2024
1 parent 196dd3a commit 58842d0
Show file tree
Hide file tree
Showing 30 changed files with 923 additions and 53 deletions.
57 changes: 57 additions & 0 deletions .github/workflows/build-and-push-embedr-aws.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: build-and-push-embedr-aws
on:
push:
branches:
- main
- bnewbold/embedr
- bnewbold/embedr-rebase

env:
REGISTRY: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_REGISTRY }}
USERNAME: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_USERNAME }}
PASSWORD: ${{ secrets.AWS_ECR_REGISTRY_USEAST2_PACKAGES_PASSWORD }}
IMAGE_NAME: embed

jobs:
embedr-container-aws:
if: github.repository == 'bluesky-social/social-app'
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup Docker buildx
uses: docker/setup-buildx-action@v1

- name: Log into registry ${{ env.REGISTRY }}
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ env.USERNAME}}
password: ${{ env.PASSWORD }}

- name: Extract Docker metadata
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha,enable=true,priority=100,prefix=,suffix=,format=long
- name: Build and push Docker image
id: build-and-push
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
file: ./Dockerfile.embedr
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
78 changes: 78 additions & 0 deletions Dockerfile.embedr
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
FROM golang:1.21-bullseye AS build-env

WORKDIR /usr/src/social-app

ENV DEBIAN_FRONTEND=noninteractive

# Node
ENV NODE_VERSION=18
ENV NVM_DIR=/usr/share/nvm

# Go
ENV GODEBUG="netdns=go"
ENV GOOS="linux"
ENV GOARCH="amd64"
ENV CGO_ENABLED=1
ENV GOEXPERIMENT="loopvar"

COPY . .

#
# Generate the JavaScript webpack. NOTE: this will change
#
RUN mkdir --parents $NVM_DIR && \
wget \
--output-document=/tmp/nvm-install.sh \
https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh && \
bash /tmp/nvm-install.sh

RUN \. "$NVM_DIR/nvm.sh" && \
nvm install $NODE_VERSION && \
nvm use $NODE_VERSION && \
npm install --global yarn && \
yarn && \
cd bskyembed && yarn install --frozen-lockfile && cd .. && \
yarn intl:build && \
yarn build-embed

# DEBUG
RUN find ./bskyweb/embedr-static && find ./bskyweb/embedr-templates && find ./bskyembed/dist

# hack around issue with empty directory and go:embed
RUN touch bskyweb/static/js/empty.txt

#
# Generate the embedr Go binary.
#
RUN cd bskyweb/ && \
go mod download && \
go mod verify

RUN cd bskyweb/ && \
go build \
-v \
-trimpath \
-tags timetzdata \
-o /embedr \
./cmd/embedr

FROM debian:bullseye-slim

ENV GODEBUG=netdns=go
ENV TZ=Etc/UTC
ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install --yes \
dumb-init \
ca-certificates

ENTRYPOINT ["dumb-init", "--"]

WORKDIR /embedr
COPY --from=build-env /embedr /usr/bin/embedr

CMD ["/usr/bin/embedr"]

LABEL org.opencontainers.image.source=https://github.com/bluesky-social/social-app
LABEL org.opencontainers.image.description="embed.bsky.app Web App"
LABEL org.opencontainers.image.licenses=MIT
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ build-web: ## Compile web bundle, copy to bskyweb directory
yarn intl:build
yarn build-web

.PHONY: build-web-embed
build-web-embed: ## Compile web embed bundle, copy to bskyweb/embedr* directories
yarn intl:build
yarn build-embed

.PHONY: test
test: ## Run all tests
NODE_ENV=test EXPO_PUBLIC_ENV=test yarn test
Expand All @@ -28,6 +33,7 @@ lint: ## Run style checks and verify syntax
.PHONY: deps
deps: ## Installs dependent libs using 'yarn install'
yarn install --frozen-lockfile
cd bskyembed && yarn install --frozen-lockfile

.PHONY: nvm-setup
nvm-setup: ## Use NVM to install and activate node+yarn
Expand Down
3 changes: 0 additions & 3 deletions bskyembed/src/screens/post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const agent = new BskyAgent({
})

const uri = `at://${window.location.pathname.slice('/embed/'.length)}`

console.log(uri)

if (!uri) {
throw new Error('No uri in path')
}
Expand Down
6 changes: 6 additions & 0 deletions bskyweb/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ test-coverage.out

# Don't check in the binary.
/bskyweb
/embedr

# Don't accidentally commit JS-generated code
static/js/*.js
static/js/*.map
static/js/*.js.LICENSE.txt
static/js/empty.txt
templates/scripts.html
templates/*-embed.html
static/embed/*.html
static/embed/assets/*.js
static/embed/assets/*.css
embedr-static/post-*.js
embedr-static/post-*.css
embedr-static/index-*.js
embedr-static/polyfills-*.js

# Don't ignore this file
!.gitignore
5 changes: 5 additions & 0 deletions bskyweb/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ help: ## Print info about all commands
.PHONY: build
build: ## Build all executables
go build ./cmd/bskyweb
go build ./cmd/embedr

.PHONY: test
test: ## Run all tests
Expand Down Expand Up @@ -43,3 +44,7 @@ check: ## Compile everything, checking syntax (does not output binaries)
.PHONY: run-dev-bskyweb
run-dev-bskyweb: .env ## Runs 'bskyweb' for local dev
GOLOG_LOG_LEVEL=info go run ./cmd/bskyweb serve

.PHONY: run-dev-embedr
run-dev-embedr: .env ## Runs 'embedr' for local dev
GOLOG_LOG_LEVEL=info go run ./cmd/embedr serve
52 changes: 52 additions & 0 deletions bskyweb/README.embed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

## oEmbed

<https://oembed.com/>

* URL scheme: `https://bsky.app/profile/*/post/*`
* API endpoint: `https://embed.bsky.app/oembed`

Request params:

- `url` (required): support both AT-URI and bsky.app URL
- `maxwidth` (optional): [220..550], 325 is default
- `maxheight` (not supported!)
- `format` (optional): only `json` supported

Response format:

- `type` (required): "rich"
- `version` (required): "1.0"
- `author_name` (optional): display name
- `author_url` (optional): profile URL
- `provider_name` (optional): "Bluesky Social"
- `provider_url` (optional): "https://bsky.app"
- `cache_age` (optional, integer seconds): 86400 (24 hours) (?)
- `width` (required): ?
- `height` (required): ?

Not used:

- title (optional): A text title, describing the resource.
- thumbnail_url (optional): A URL to a thumbnail image representing the resource. The thumbnail must respect any maxwidth and maxheight parameters. If this parameter is present, thumbnail_width and thumbnail_height must also be present.
- thumbnail_width (optional): The width of the optional thumbnail. If this parameter is present, thumbnail_url and thumbnail_height must also be present.
- thumbnail_height (optional): The height of the optional thumbnail. If this parameter is present, thumbnail_url and thumbnail_width must also be present.

Only `json` is supported; `xml` is a 501.

```
<link rel="alternate" type="application/json+oembed" href="https://embed.bsky.app/oembed?format=json&url=https://bsky.app/profile/bnewbold.net/post/abc123" />
```


## iframe URL

`https://embed.bsky.app/embed/<did>/app.bsky.feed.post/<rkey>`
`https://embed.bsky.app/static/embed.js`

```
<blockquote class="bluesky-post" data-lang="en" data-align="center">
<p lang="en" dir="ltr">{{ post-text }}</p>
&mdash; US Department of the Interior (@Interior) <a href="https://twitter.com/Interior/status/463440424141459456?ref_src=twsrc%5Etfw">May 5, 2014</a>
</blockquote>
```
1 change: 1 addition & 0 deletions bskyweb/cmd/embedr/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/bskyweb
Loading

0 comments on commit 58842d0

Please sign in to comment.