Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use r2 for data storage #15

Merged
merged 8 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/cache.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/routes/gateway_github.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import type { HonoContext } from "../types";
import { OpenAPIHono } from "@hono/zod-openapi";
import { cache, createError } from "../utils";
import { cache } from "hono/cache";
import { createError } from "../utils";
import { GITHUB_EMOJIS_ROUTE } from "./gateway_github.openapi";

export const GATEWAY_GITHUB_ROUTER = new OpenAPIHono<HonoContext>().basePath("/api/gateway/github");

GATEWAY_GITHUB_ROUTER.get("*", cache({
cacheName: "github-emojis",
cacheControl: "max-age=3600, immutable",
}));
GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {
const response = await fetch("https://api.github.com/emojis", {
headers: {
Expand All @@ -24,7 +29,5 @@ GATEWAY_GITHUB_ROUTER.openapi(GITHUB_EMOJIS_ROUTE, async (c) => {

const emojis = await response.json<Record<string, string>>();

cache(c, 3600, true);

return c.json(emojis, 200);
});
8 changes: 6 additions & 2 deletions src/routes/random-emoji.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import type { HonoContext } from "../types";
import { Hono } from "hono";
import { cache } from "../utils";
import { cache } from "hono/cache";

export const RANDOM_EMOJI_ROUTER = new Hono<HonoContext>();

RANDOM_EMOJI_ROUTER.get("/random-emoji.png", cache({
cacheName: "random-emoji",
cacheControl: "max-age=3600, immutable",
}));

RANDOM_EMOJI_ROUTER.get("/random-emoji.png", async (c) => {
cache(c, 60 * 60);
return c.redirect("https://image.luxass.dev/api/image/emoji");
});
23 changes: 17 additions & 6 deletions src/routes/v1_categories.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { HonoContext } from "../types";
import { OpenAPIHono, z } from "@hono/zod-openapi";
import { cache } from "hono/cache";
import { HTTPException } from "hono/http-exception";
import { versionMiddleware } from "../middlewares/version";
import { EmojiCategorySchema } from "../schemas";
import { createError } from "../utils";
Expand All @@ -9,13 +11,20 @@ export const V1_CATEGORIES_ROUTER = new OpenAPIHono<HonoContext>().basePath("/ap

V1_CATEGORIES_ROUTER.use(versionMiddleware);

V1_CATEGORIES_ROUTER.get("*", cache({
cacheName: "v1-categories",
cacheControl: "max-age=3600, immutable",
}));

V1_CATEGORIES_ROUTER.openapi(ALL_CATEGORIES_ROUTE, async (c) => {
const version = c.req.param("version");

const res = await fetch(`https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${version}/groups.json`);
const res = await c.env.EMOJI_DATA.get(`v${version}/groups.json`);

if (!res.ok) {
return createError(c, 500, "failed to fetch categories");
if (res == null) {
throw new HTTPException(500, {
message: "failed to fetch categories",
});
}

const data = await res.json();
Expand All @@ -38,10 +47,12 @@ V1_CATEGORIES_ROUTER.openapi(GET_CATEGORY_ROUTE, async (c) => {
const version = c.req.param("version");
const categorySlug = c.req.param("category");

const res = await fetch(`https://raw.githubusercontent.com/mojisdev/emoji-data/refs/heads/main/data/v${version}/groups.json`);
const res = await c.env.EMOJI_DATA.get(`v${version}/groups.json`);

if (!res.ok) {
return createError(c, 500, "failed to fetch categories");
if (res == null) {
throw new HTTPException(500, {
message: "failed to fetch categories",
});
}

const data = await res.json();
Expand Down
12 changes: 0 additions & 12 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,3 @@ export async function getAvailableVersions(): Promise<EmojiLock["versions"]> {

return result.data.versions;
}

export function cache<TCtx extends Context>(ctx: TCtx, age: number, immutable = false) {
if (age === -1) {
ctx.header("Expires", "0");
ctx.header("Pragma", "no-cache");
ctx.header("Cache-Control", "no-cache, no-store, must-revalidate");
return;
}

ctx.header("Expires", new Date(Date.now() + age * 1000).toUTCString());
ctx.header("Cache-Control", ["public", `max-age=${age}`, immutable ? "immutable" : null].filter((x) => !!x).join(", "));
};
3 changes: 2 additions & 1 deletion worker-configuration.d.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Generated by Wrangler by running `wrangler types --env-interface CloudflareBindings` (hash: e71cf0de9c927e7e3f2e02a5b583aba3)
// Generated by Wrangler by running `wrangler types --env-interface CloudflareBindings` (hash: c476c03844aa0429b0aacf2d2d7c8815)
// Runtime types generated with [email protected] 2025-03-13
declare namespace Cloudflare {
interface Env {
API_VERSION: "x.y.z";
ENVIRONMENT: "preview" | "production";
GITHUB_TOKEN: string;
EMOJI_DATA: R2Bucket;
}
}
interface CloudflareBindings extends Cloudflare.Env {}
Expand Down
22 changes: 20 additions & 2 deletions wrangler.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"GITHUB_TOKEN": "",
"API_VERSION": "x.y.z"
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis"
}
],
"placement": { "mode": "smart" },
"env": {
"preview": {
Expand All @@ -24,7 +30,13 @@
"route": {
"custom_domain": true,
"pattern": "api.preview.mojis.dev"
}
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis-preview"
}
]
},
"production": {
"name": "mojis-api",
Expand All @@ -35,7 +47,13 @@
"route": {
"custom_domain": true,
"pattern": "api.mojis.dev"
}
},
"r2_buckets": [
{
"binding": "EMOJI_DATA",
"bucket_name": "mojis"
}
]
}
}
}
Loading