Added project files

This commit is contained in:
J-onasJones 2024-06-09 12:46:27 +02:00
parent 6e645ce02a
commit 40c8e77367
4 changed files with 125 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
/node_modules/

12
package.json Normal file
View file

@ -0,0 +1,12 @@
{
"name": "api-worker",
"version": "0.0.1+alpha-build01",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"start": "wrangler dev"
},
"devDependencies": {
"wrangler": "^3.28.0"
}
}

61
src/index.js Normal file
View file

@ -0,0 +1,61 @@
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const url = new URL(request.url);
// Check if the path is /blog
if (url.pathname === '/blog') {
// Fetch the index.json file from your CDN
const response = await fetch('https://cdn.jonasjones.dev/blog/index.json');
const data = await response.json();
// Process the JSON data to create the RSS feed
const rssFeed = createRSSFeed(data);
// Return the RSS feed as the response
return new Response(rssFeed, {
headers: { 'Content-Type': 'application/rss+xml' },
});
} else {
// Handle other routes or return a 404 response
return new Response('Not Found', { status: 404 });
}
}
function createRSSFeed(data) {
const DOMAIN = 'https://blog.jonasjones.dev';
const items = data.slice(-50).reverse().map(item => `
<item>
<title><![CDATA[${item.title}]]></title>
<link>${DOMAIN}/#/post/${formatDateForLink(item.date)}/${item.id}</link>
<guid isPermaLink="true">${DOMAIN}/#/post/${formatDateForLink(item.date)}/${item.id}</guid>
<author><![CDATA[${item.author}]]></author>
<pubDate>${new Date(item.date).toUTCString()}</pubDate>
<description><![CDATA[${item.description}]]></description>
</item>
`).join('');
return `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>Your Blog Title</title>
<link>${DOMAIN}</link>
<description>Your blog description</description>
<language>en-us</language>
<pubDate>${new Date().toUTCString()}</pubDate>
<lastBuildDate>${new Date().toUTCString()}</lastBuildDate>
<generator>Cloudflare Worker</generator>
${items}
</channel>
</rss>`;
}
function formatDateForLink(dateString) {
const date = new Date(dateString);
const year = date.getUTCFullYear();
const month = String(date.getUTCMonth() + 1).padStart(2, '0');
const day = String(date.getUTCDate()).padStart(2, '0');
return `${year}/${month}/${day}`;
}

51
wrangler.toml Normal file
View file

@ -0,0 +1,51 @@
name = "rss-worker"
main = "src/index.js"
compatibility_date = "2023-09-04"
# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Note: Use secrets to store sensitive data.
# Docs: https://developers.cloudflare.com/workers/platform/environment-variables
# [vars]
# MY_VARIABLE = "production_value"
# Bind a KV Namespace. Use KV as persistent storage for small key-value pairs.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/kv
# [[kv_namespaces]]
# binding = "MY_KV_NAMESPACE"
# id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Bind an R2 Bucket. Use R2 to store arbitrarily large blobs of data, such as files.
# Docs: https://developers.cloudflare.com/r2/api/workers/workers-api-usage/
# [[r2_buckets]]
# binding = "MY_BUCKET"
# bucket_name = "my-bucket"
# Bind a Queue producer. Use this binding to schedule an arbitrary task that may be processed later by a Queue consumer.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.producers]]
# binding = "MY_QUEUE"
# queue = "my-queue"
# Bind a Queue consumer. Queue Consumers can retrieve tasks scheduled by Producers to act on them.
# Docs: https://developers.cloudflare.com/queues/get-started
# [[queues.consumers]]
# queue = "my-queue"
# Bind another Worker service. Use this binding to call another Worker without network overhead.
# Docs: https://developers.cloudflare.com/workers/platform/services
# [[services]]
# binding = "MY_SERVICE"
# service = "/api/*"
# Bind a Durable Object. Durable objects are a scale-to-zero compute primitive based on the actor model.
# Durable Objects can live for as long as needed. Use these when you need a long-running "server", such as in realtime apps.
# Docs: https://developers.cloudflare.com/workers/runtime-apis/durable-objects
# [[durable_objects.bindings]]
# name = "MY_DURABLE_OBJECT"
# class_name = "MyDurableObject"
# Durable Object migrations.
# Docs: https://developers.cloudflare.com/workers/learning/using-durable-objects#configure-durable-object-classes-with-migrations
# [[migrations]]
# tag = "v1"
# new_classes = ["MyDurableObject"]