forked from epesooj/webring
Move to Deno+Bunny.net, add a way to test webring locally.
This commit is contained in:
parent
9f66f0b37b
commit
8670aa3412
33 changed files with 599 additions and 3360 deletions
130
scripts/bunny_api/main.ts
Normal file
130
scripts/bunny_api/main.ts
Normal file
|
@ -0,0 +1,130 @@
|
|||
export async function deployScript(scriptName: string, contents: string) {
|
||||
const scriptIdEnvName = `SCRIPT_ID_${scriptName.toUpperCase()}`;
|
||||
const scriptId = Deno.env.get(scriptIdEnvName);
|
||||
const deployKeyEnvName = `SCRIPT_KEY_${scriptName.toUpperCase()}`;
|
||||
const deployKey = Deno.env.get(deployKeyEnvName);
|
||||
|
||||
if (scriptId === undefined) {
|
||||
throw new Error(
|
||||
`Can't deploy script '${scriptName}' because we don't know its id on bunny. Please set it by setting the environment variable '${scriptIdEnvName}'.`
|
||||
);
|
||||
}
|
||||
if (deployKey === undefined) {
|
||||
throw new Error(
|
||||
`Can't deploy script '${scriptName}' because we don't know its deploy key on bunny. Please set it by setting the environment variable '${scriptIdEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/json',
|
||||
deploymentkey: deployKey,
|
||||
};
|
||||
|
||||
const setCodeResp = await fetch(
|
||||
`https://api.bunny.net/compute/script/${scriptId}/code`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers,
|
||||
body: JSON.stringify({ Code: contents }),
|
||||
}
|
||||
);
|
||||
|
||||
if (!setCodeResp.ok) {
|
||||
console.error(await setCodeResp.text());
|
||||
throw new Error(
|
||||
`Failed to upload new script code: ${setCodeResp.statusText}`
|
||||
);
|
||||
}
|
||||
|
||||
const publishResp = await fetch(
|
||||
`https://api.bunny.net/compute/script/${scriptId}/publish`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers,
|
||||
}
|
||||
);
|
||||
|
||||
if (!publishResp.ok) {
|
||||
console.error(await setCodeResp.text());
|
||||
throw new Error(`Failed to publish script: ${setCodeResp.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadFile(filepath: string, contents: Uint8Array) {
|
||||
const storageZoneNameEnvName = 'STORAGE_ZONE_NAME';
|
||||
const storageZoneName = Deno.env.get(storageZoneNameEnvName);
|
||||
const storageApiKeyEnvName = 'STORAGE_API_KEY';
|
||||
const apiKey = Deno.env.get(storageApiKeyEnvName);
|
||||
|
||||
if (storageZoneName === undefined) {
|
||||
throw new Error(
|
||||
`Can't upload file '${filepath}' because we don't know which storage zone to upload to. Please set it by setting the environment variable '${storageZoneNameEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
if (apiKey === undefined) {
|
||||
throw new Error(
|
||||
`Can't upload file '${filepath}' because we don't have an API key. Please set it by setting the environment variable '${storageApiKeyEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/octet-stream',
|
||||
accesskey: apiKey,
|
||||
};
|
||||
|
||||
const res = await fetch(
|
||||
`https://storage.bunnycdn.com/${storageZoneName}/${filepath}`,
|
||||
{
|
||||
method: 'PUT',
|
||||
headers,
|
||||
body: contents,
|
||||
}
|
||||
);
|
||||
|
||||
if (!res.ok) {
|
||||
console.error(await res.text());
|
||||
throw new Error(`Failed to upload file: ${res.statusText}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function purgePath(filepath: string) {
|
||||
const cdnBaseUrlEnvName = 'BUNNY_CDN_BASE_URL';
|
||||
const cdnBaseUrl = Deno.env.get(cdnBaseUrlEnvName);
|
||||
const accessKeyEnvName = 'BUNNY_ACCESS_KEY';
|
||||
const accessKey = Deno.env.get(accessKeyEnvName);
|
||||
|
||||
if (cdnBaseUrl === undefined) {
|
||||
throw new Error(
|
||||
`Can't purge cache for '${filepath}' because we don't know the CDN base URL. Please set it by setting the environment variable '${cdnBaseUrlEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
if (accessKey === undefined) {
|
||||
throw new Error(
|
||||
`Can't purge cache for '${filepath}' because we don't have an API key. Please set it by setting the environment variable '${accessKeyEnvName}'.`
|
||||
);
|
||||
}
|
||||
|
||||
const headers = {
|
||||
accept: 'application/json',
|
||||
'content-type': 'application/json',
|
||||
accesskey: accessKey,
|
||||
};
|
||||
|
||||
const fetchUrl = new URL(`https://api.bunny.net/purge`);
|
||||
fetchUrl.searchParams.append('async', 'false');
|
||||
fetchUrl.searchParams.append('url', `${cdnBaseUrl}/${filepath}`);
|
||||
|
||||
const res = await fetch(fetchUrl.toString(), {
|
||||
method: 'POST',
|
||||
headers,
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
console.error(await res.text());
|
||||
throw new Error(`Failed to purge cache: ${res.statusText}`);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue