mirror of
https://github.com/PCSX2/web-api.git
synced 2024-11-27 03:30:24 +00:00
api: update page rule cache when a release is added/deleted/modified
This commit is contained in:
parent
f505f97f89
commit
5eb3bb5035
@ -5,7 +5,7 @@
|
||||
"scripts": {
|
||||
"setup-db": "node ./db/run-migrations.mjs",
|
||||
"dev": "npx wrangler dev",
|
||||
"publish": "npx wrangler publish",
|
||||
"deploy": "npx wrangler deploy",
|
||||
"format": "prettier --write '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
|
||||
"lint": "eslint --max-warnings=0 src && prettier --check '*.{json,js}' 'src/**/*.{js,ts}' 'test/**/*.{js,ts}'",
|
||||
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js"
|
||||
|
@ -73,11 +73,12 @@ export async function recentReleases(
|
||||
};
|
||||
headers = appendCorsHeaders(cfRequest.headers.get("origin"), headers);
|
||||
const resp = new Response(body, { headers });
|
||||
// NOTE - cache in a custom page rule (this avoids a worker invocation)
|
||||
// NOTE - cache in a custom page rule (this helps avoid a worker invocation)
|
||||
const cacheKey = new Request(
|
||||
"https://cache.pcsx2.net/v1/releases/recent",
|
||||
req
|
||||
);
|
||||
console.log("updating page rule");
|
||||
ctx.waitUntil(cache.put(cacheKey, resp.clone()));
|
||||
// Cache for 30 mins max
|
||||
// TODO - if we had a more fancy CF tier (enterprise), we could purge the cache appropriately
|
||||
|
@ -1,11 +1,15 @@
|
||||
import { RequestLike } from "itty-router";
|
||||
import { Env } from "../..";
|
||||
import { serializeGithubRelease } from "../../external/github";
|
||||
import { verifySignedGithubWebhookPayload } from "../../lib/github";
|
||||
import {
|
||||
archiveRelease,
|
||||
editOrInsertRelease,
|
||||
getRecentReleases,
|
||||
insertNewRelease,
|
||||
} from "../../storage/d1";
|
||||
import { ReleaseType } from "../../lib/releases";
|
||||
import { appendCorsHeaders } from "../cors";
|
||||
|
||||
type ReleaseAction =
|
||||
| "created"
|
||||
@ -19,7 +23,8 @@ type ReleaseAction =
|
||||
| undefined;
|
||||
|
||||
export async function githubReleaseWebhookEvent(
|
||||
req: Request,
|
||||
req: RequestLike,
|
||||
cfRequest: Request,
|
||||
env: Env,
|
||||
ctx: ExecutionContext
|
||||
) {
|
||||
@ -46,20 +51,46 @@ export async function githubReleaseWebhookEvent(
|
||||
const body = JSON.parse(reqBody);
|
||||
const releaseAction: ReleaseAction = body.action;
|
||||
const isDraft = body?.release?.draft;
|
||||
let updateReleaseCache = false;
|
||||
if (releaseAction === "published" && !isDraft) {
|
||||
const release = serializeGithubRelease(body.release, false);
|
||||
if (release !== undefined) {
|
||||
const result = await insertNewRelease(env.DB, release);
|
||||
}
|
||||
updateReleaseCache = true;
|
||||
} else if (releaseAction === "deleted") {
|
||||
await archiveRelease(env.DB, body.release.tag_name);
|
||||
updateReleaseCache = true;
|
||||
} else if (releaseAction === "edited" && !isDraft) {
|
||||
const release = serializeGithubRelease(body.release, false);
|
||||
if (release !== undefined) {
|
||||
await editOrInsertRelease(env.DB, release);
|
||||
}
|
||||
updateReleaseCache = true;
|
||||
} else {
|
||||
console.log(`Unhandled release action: ${releaseAction}`);
|
||||
}
|
||||
|
||||
if (updateReleaseCache) {
|
||||
const latestNightly = await getRecentReleases(env.DB, ReleaseType.Nightly);
|
||||
const latestStable = await getRecentReleases(env.DB, ReleaseType.Stable);
|
||||
const body = JSON.stringify({
|
||||
nightly: latestNightly,
|
||||
stable: latestStable,
|
||||
});
|
||||
let headers: Record<string, string> = {
|
||||
"Content-type": "application/json",
|
||||
};
|
||||
headers = appendCorsHeaders(cfRequest.headers.get("origin"), headers);
|
||||
const resp = new Response(body, { headers });
|
||||
// NOTE - cache in a custom page rule (this helps avoid a worker invocation)
|
||||
const cacheKey = new Request(
|
||||
"https://cache.pcsx2.net/v1/releases/recent",
|
||||
req
|
||||
);
|
||||
console.log("updating page rule");
|
||||
ctx.waitUntil(caches.default.put(cacheKey, resp.clone()));
|
||||
}
|
||||
|
||||
return new Response(undefined, { status: 204 });
|
||||
}
|
||||
|
@ -176,7 +176,7 @@ export async function getRecentReleases(
|
||||
type: ReleaseType
|
||||
): Promise<Release[] | undefined> {
|
||||
const recentReleasesQuery = db.prepare(
|
||||
"SELECT * FROM releases WHERE release_type = ? AND archived = 0 ORDER BY version_integral DESC LIMIT 100;"
|
||||
"SELECT * FROM releases WHERE release_type = ? AND archived = 0 ORDER BY version_integral DESC LIMIT 200;"
|
||||
);
|
||||
const queryResults: D1Result = await recentReleasesQuery.bind(type).all();
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user