From afc635613176501acb358f1632e96358fa139b10 Mon Sep 17 00:00:00 2001 From: Arc <90333108+lastarc@users.noreply.github.com> Date: Sun, 31 Mar 2024 23:52:58 +0200 Subject: [PATCH 1/2] new embed proxy --- embed-proxy/.gitignore | 11 ++++ embed-proxy/README.md | 43 ++++++++++++++ embed-proxy/package.json | 16 ++++++ embed-proxy/src/index.ts | 33 +++++++++++ embed-proxy/src/info.ts | 41 ++++++++++++++ embed-proxy/src/v1/index.ts | 35 ++++++++++++ embed-proxy/src/v2/index.ts | 108 ++++++++++++++++++++++++++++++++++++ embed-proxy/tsconfig.json | 16 ++++++ 8 files changed, 303 insertions(+) create mode 100644 embed-proxy/.gitignore create mode 100644 embed-proxy/README.md create mode 100644 embed-proxy/package.json create mode 100644 embed-proxy/src/index.ts create mode 100644 embed-proxy/src/info.ts create mode 100644 embed-proxy/src/v1/index.ts create mode 100644 embed-proxy/src/v2/index.ts create mode 100644 embed-proxy/tsconfig.json diff --git a/embed-proxy/.gitignore b/embed-proxy/.gitignore new file mode 100644 index 0000000..7d178e4 --- /dev/null +++ b/embed-proxy/.gitignore @@ -0,0 +1,11 @@ +node_modules +dist +.wrangler +.dev.vars +wrangler.toml + +# Change them to your taste: +package-lock.json +yarn.lock +pnpm-lock.yaml +bun.lockb \ No newline at end of file diff --git a/embed-proxy/README.md b/embed-proxy/README.md new file mode 100644 index 0000000..739c2b4 --- /dev/null +++ b/embed-proxy/README.md @@ -0,0 +1,43 @@ +# Liben - Discord embed proxy + +Lightweight http app that produces necessary OpenGraph meta tags for Discord +in-app video preview. + +## Self-hosting + +### Prerequisites + +* Node.js (project was built using v18) +* pnpm +* Cloudflare account + +### Steps + +1. Clone the repository and open the `embed-proxy` directory + +```bash +git clone +cd liben/embed-proxy +``` + +1. Install dependencies + +```bash +pnpm i +``` + +1. Rename `wrangler.sample.toml` to `wrangler.toml` + +1. Open and edit the followings + + 1.1. replace the Host value with the address the worker will be accessed on + + 1.2. + +1. Deploy + +```bash +pnpm run deploy +``` + +If it's first time running it, Cloudflare account login might be required. diff --git a/embed-proxy/package.json b/embed-proxy/package.json new file mode 100644 index 0000000..8cf92e8 --- /dev/null +++ b/embed-proxy/package.json @@ -0,0 +1,16 @@ +{ + "scripts": { + "dev": "wrangler dev src/index.ts", + "deploy": "wrangler deploy --minify src/index.ts" + }, + "dependencies": { + "@hono/zod-validator": "^0.2.1", + "haikunator": "^2.1.2", + "hono": "^4.1.5", + "zod": "^3.22.4" + }, + "devDependencies": { + "@cloudflare/workers-types": "^4.20240329.0", + "wrangler": "^3.32.0" + } +} diff --git a/embed-proxy/src/index.ts b/embed-proxy/src/index.ts new file mode 100644 index 0000000..c201a95 --- /dev/null +++ b/embed-proxy/src/index.ts @@ -0,0 +1,33 @@ +import { Hono } from 'hono'; +import v1 from './v1'; +import v2 from './v2'; +import info from './info'; + +export type AppT = { + Bindings: { + Host: string; + Slugs: KVNamespace; + }; +}; + +const app = new Hono({ + strict: false, +}); + +app.route('/v1', v1); +app.route('/v2', v2); + +app.get('/', (c) => { + if ( + c.req.query('src') && + c.req.query('width') && + c.req.query('height') + ) { + const url = new URL(c.req.url); + return c.redirect('/v1/?' + url.searchParams.toString()); + } + + return info(c); +}); + +export default app; diff --git a/embed-proxy/src/info.ts b/embed-proxy/src/info.ts new file mode 100644 index 0000000..cdddf9e --- /dev/null +++ b/embed-proxy/src/info.ts @@ -0,0 +1,41 @@ +import { Context } from 'hono'; + +export default function info(c: Context): Response { + return c.text( + ` +Simple Embed Proxy +================== + +v1 Usage: +--------- + +/?src=&width=&height= +or +/v1/?src=&width=&height= + +Example: + +/?src=https%3A%2F%2Fexample.com%2Fvideo.mp4&width=480&height=360 + + + +v2 Usage: +--------- + +/v2/ + +How to add a new embed: +POST /v2/add +--> +{ + "src": "https://example.com/video.mp4", +} + +<-- +{ + "slug": "my-video-ffff" +} + + `.trim() + ); +} diff --git a/embed-proxy/src/v1/index.ts b/embed-proxy/src/v1/index.ts new file mode 100644 index 0000000..6ffd661 --- /dev/null +++ b/embed-proxy/src/v1/index.ts @@ -0,0 +1,35 @@ +import { Hono } from 'hono'; + +const v1 = new Hono(); + +v1.get('/', async (c) => { + const src = c.req.query('src') || ''; + const width = c.req.query('width') || ''; + const height = c.req.query('height') || ''; + const title = c.req.query('title') || ''; + const img = + c.req.query('img') || + `https://dummyimage.com/${width}x${height}/000000/fff&text=+`; + + return c.html(` + + + + + + + + + + + + + + + +