deploy github user contribution endpoint to cloudflare

This commit is contained in:
platane
2025-02-20 23:43:25 +07:00
parent 10c4c3c7bd
commit 852f0ae376
9 changed files with 257 additions and 287 deletions

View File

@@ -1,3 +1,14 @@
# @snk/github-user-contribution-service
Expose github-user-contribution as an endpoint, using vercel.sh
Expose github-user-contribution as an endpoint. hosted on cloudflare
```sh
# deploy
bunx wrangler deploy --branch=production
# change secret
bunx wrangler secret put GITHUB_TOKEN
```

View File

@@ -1,33 +0,0 @@
import { getGithubUserContribution } from "@snk/github-user-contribution";
import { VercelRequest, VercelResponse } from "@vercel/node";
export default async (req: VercelRequest, res: VercelResponse) => {
const { userName } = req.query;
try {
// handle CORS
{
const allowedOrigins = [
"https://platane.github.io",
"https://platane.me",
];
const allowedOrigin = allowedOrigins.find(
(o) => o === req.headers.origin,
);
if (allowedOrigin)
res.setHeader("Access-Control-Allow-Origin", allowedOrigin);
}
res.setHeader("Cache-Control", "max-age=21600, s-maxage=21600");
res.statusCode = 200;
res.json(
await getGithubUserContribution(userName as string, {
githubToken: process.env.GITHUB_TOKEN!,
}),
);
} catch (err) {
console.error(err);
res.statusCode = 500;
res.end();
}
};

View File

@@ -0,0 +1,52 @@
import { getGithubUserContribution } from "@snk/github-user-contribution";
const cors =
<
Req extends { headers: Headers },
Res extends { headers: Headers },
A extends Array<any>,
>(
f: (req: Req, ...args: A) => Res | Promise<Res>,
) =>
async (req: Req, ...args: A) => {
const res = await f(req, ...args);
const origin = req.headers.get("origin");
if (origin) {
const { host, hostname } = new URL(origin);
if (hostname === "localhost" || host === "platane.github.io")
res.headers.set("Access-Control-Allow-Origin", origin);
}
res.headers.set("Access-Control-Allow-Methods", "GET, OPTIONS");
res.headers.set("Access-Control-Allow-Headers", "Content-Type");
return res;
};
export default {
fetch: cors(async (req: Request, env: { GITHUB_TOKEN: string }) => {
const url = new URL(req.url);
const [, userName] =
url.pathname.match(/^\/github-user-contribution\/([^\/]*)\/?$/) ?? [];
if (req.method === "OPTIONS") return new Response();
if (!userName || req.method !== "GET")
return new Response("unknown route", { status: 404 });
const body = await getGithubUserContribution(userName, {
githubToken: env.GITHUB_TOKEN,
});
return new Response(JSON.stringify(body), {
status: 200,
headers: {
"Cache-Control": "max-age=21600, s-maxage=21600",
"Content-Type": "application/json",
},
});
}),
};

View File

@@ -2,7 +2,13 @@
"name": "@snk/github-user-contribution-service",
"version": "1.0.0",
"dependencies": {
"@snk/github-user-contribution": "1.0.0",
"@vercel/node": "5.1.7"
"@snk/github-user-contribution": "1.0.0"
},
"devDependencies": {
"wrangler": "3.109.2",
"@cloudflare/workers-types": "4.20250214.0"
},
"scripts": {
"deploy": "wrangler deploy"
}
}

View File

@@ -1,5 +0,0 @@
{
"github": {
"silent": true
}
}

View File

@@ -0,0 +1,9 @@
name = "github-user-contribution"
main = "index.ts"
compatibility_date = "2024-09-02"
account_id = "56268cde636c288343cb0767952ecf2e"
workers_dev = true
[observability]
enabled = true

View File

@@ -42,12 +42,13 @@ export const getGithubUserContribution = async (
headers: {
Authorization: `bearer ${o.githubToken}`,
"Content-Type": "application/json",
"User-Agent": "me@platane.me",
},
method: "POST",
body: JSON.stringify({ variables, query }),
});
if (!res.ok) throw new Error(res.statusText);
if (!res.ok) throw new Error(await res.text().catch(() => res.statusText));
const { data, errors } = (await res.json()) as {
data: GraphQLRes;