-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
49 lines (36 loc) · 1.13 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Hono } from "hono";
import twemoji from "twemoji";
import { DOMParser } from "domparser";
import { serveStatic } from "middleware";
import "dotenv";
if (typeof Deno.env.get("SENTRY_DSN") !== "undefined") {
const Sentry = await import("https://deno.land/x/sentry/index.mjs");
const dsn = Deno.env.get("SENTRY_DSN");
console.log(`Sentry DSN: ${dsn}`);
Sentry.init({
dsn: dsn,
});
}
function emoji2Url(emoji: string): string {
const html = twemoji.parse(emoji, {
folder: "svg",
ext: ".svg",
base: "https://cdn.jsdelivr.net/gh/twitter/twemoji@latest/assets/",
});
const docment = new DOMParser().parseFromString(html, "text/html");
const url = docment?.querySelector("img")?.getAttribute("src");
if (url == null) {
return "";
}
return url;
}
const app = new Hono();
app.use("/", serveStatic({ path: "./index.html" }));
app.get("/api/:emoji", async (ctx) => {
const emoji = ctx.req.param("emoji");
const response = await fetch(emoji2Url(emoji));
ctx.header("Content-Type", "image/svg+xml");
const svg = await response.text();
return ctx.body(svg);
});
Deno.serve({ port: 8000 }, app.fetch);