From 3c1b525da735ff2c24424349b208fb5caeb365ff Mon Sep 17 00:00:00 2001 From: Lyric <5h3ll3x@gmail.com> Date: Mon, 24 Jun 2024 18:44:48 +0900 Subject: [PATCH] chore: Update Cloudflare deployment configuration --- .github/workflows/deploy-1000h.yml | 2 +- 1000-hours/workers-site/.cargo-ok | 0 1000-hours/workers-site/.gitignore | 2 + 1000-hours/workers-site/index.js | 142 +++++++++++++++++++++++++++ 1000-hours/workers-site/package.json | 12 +++ 1000-hours/wrangler.toml | 13 +++ entry/wrangler.toml | 2 +- 7 files changed, 171 insertions(+), 2 deletions(-) create mode 100644 1000-hours/workers-site/.cargo-ok create mode 100644 1000-hours/workers-site/.gitignore create mode 100644 1000-hours/workers-site/index.js create mode 100644 1000-hours/workers-site/package.json create mode 100644 1000-hours/wrangler.toml diff --git a/.github/workflows/deploy-1000h.yml b/.github/workflows/deploy-1000h.yml index ecce014e..d809b915 100644 --- a/.github/workflows/deploy-1000h.yml +++ b/.github/workflows/deploy-1000h.yml @@ -42,6 +42,6 @@ jobs: with: apiToken: ${{ secrets.CLOUDFLARE_API_TOKEN }} accountId: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }} - command: pages deploy .vitepress/dist --project-name=1000-hours-prod + command: deploy workingDirectory: "1000-hours" diff --git a/1000-hours/workers-site/.cargo-ok b/1000-hours/workers-site/.cargo-ok new file mode 100644 index 00000000..e69de29b diff --git a/1000-hours/workers-site/.gitignore b/1000-hours/workers-site/.gitignore new file mode 100644 index 00000000..75c573a4 --- /dev/null +++ b/1000-hours/workers-site/.gitignore @@ -0,0 +1,2 @@ +node_modules +worker diff --git a/1000-hours/workers-site/index.js b/1000-hours/workers-site/index.js new file mode 100644 index 00000000..9b0d4095 --- /dev/null +++ b/1000-hours/workers-site/index.js @@ -0,0 +1,142 @@ +import { getAssetFromKV, mapRequestToAsset } from '@cloudflare/kv-asset-handler' + +/** + * The DEBUG flag will do two things that help during development: + * 1. we will skip caching on the edge, which makes it easier to + * debug. + * 2. we will return an error message on exception in your Response rather + * than the default 404.html page. + */ +const DEBUG = true + +addEventListener('fetch', event => { + try { + event.respondWith(handleEvent(event)) + } catch (e) { + if (DEBUG) { + return event.respondWith( + new Response(e.message || e.toString(), { + status: 500, + }), + ) + } + event.respondWith(new Response('Internal Error', { status: 500 })) + } +}) + +async function handleEvent(event) { + // const url = new URL(event.request.url) + let options = {} + + // const key = url.pathname.slice(1); + // if (event.request.method === 'GET' && key.startsWith("storage/")) { + // const object = await STORAGE_BUCKET.get(key); + + // if (object === null) { + // event.respondWith(Response('Object Not Found', { status: 404 })); + // return + // } + + // const headers = new Headers(); + // object.writeHttpMetadata(headers); + // headers.set('etag', object.httpEtag); + + // event.respondWith(new Response(object.body, { + // headers, + // })); + // return ; + // } + + /** + * You can add custom logic to how we fetch your assets + * by configuring the function `mapRequestToAsset` + */ + // options.mapRequestToAsset = handlePrefix(/^\/docs/) + + try { + if (DEBUG) { + // customize caching + options.cacheControl = { + bypassCache: true, + }; + } + + // no extension and not end with '/', redirect to the same path with '/' + if (!event.request.url.endsWith("/")) { + if (!event.request.url.split("/").pop().includes(".")) { + return Response.redirect(`${event.request.url}/`, 301); + } + } + + const page = await getAssetFromKV(event, options); + + // allow headers to be altered + const response = new Response(page.body, page); + + response.headers.set("X-XSS-Protection", "1; mode=block"); + response.headers.set("X-Content-Type-Options", "nosniff"); + response.headers.set("X-Frame-Options", "DENY"); + response.headers.set("Referrer-Policy", "unsafe-url"); + response.headers.set("Feature-Policy", "none"); + + return response; + + } catch (e) { + // if an error is thrown try to serve the asset at 404.html + // if (!DEBUG) { + // try { + // let notFoundResponse = await getAssetFromKV(event, { + // mapRequestToAsset: req => new Request(`${new URL(req.url).origin}/404.html`, req), + // }) + + // return new Response(notFoundResponse.body, { ...notFoundResponse, status: 404 }) + // } catch (e) {} + // } + return getAssetFromKV(event, { + mapRequestToAsset: (req) => { + const url = new URL(req.url); + let pathname = url.pathname; + + if (pathname.startsWith("/tools/")) { + // remove the '/tools' prefix + pathname = pathname.substring(6); + } + + // If the pathname ends with '/', assume it's a directory and append 'index.html' + if (pathname.endsWith('/')) { + pathname += 'index.html'; + } + + // If it doesn't have an extension, assume it's a directory and append '/index.html' + else if (!pathname.split("/").pop().includes(".")) { + pathname += '/index.html'; + } + + return new Request(`${url.origin}${pathname}`, req); + }, + }) + + // return new Response(e.message || e.toString(), { status: 500 }) + } +} + +/** + * Here's one example of how to modify a request to + * remove a specific prefix, in this case `/docs` from + * the url. This can be useful if you are deploying to a + * route on a zone, or if you only want your static content + * to exist at a specific path. + */ +function handlePrefix(prefix) { + return request => { + // compute the default (e.g. / -> index.html) + let defaultAssetKey = mapRequestToAsset(request) + let url = new URL(defaultAssetKey.url) + + // strip the prefix from the path for lookup + url.pathname = url.pathname.replace(prefix, '/') + + // inherit all other props from the default request + return new Request(url.toString(), defaultAssetKey) + } +} \ No newline at end of file diff --git a/1000-hours/workers-site/package.json b/1000-hours/workers-site/package.json new file mode 100644 index 00000000..d298f1bc --- /dev/null +++ b/1000-hours/workers-site/package.json @@ -0,0 +1,12 @@ +{ + "private": true, + "name": "worker", + "version": "1.0.0", + "description": "A template for kick starting a Cloudflare Workers project", + "main": "index.js", + "author": "Ashley Lewis ", + "license": "MIT", + "dependencies": { + "@cloudflare/kv-asset-handler": "~0.1.2" + } +} diff --git a/1000-hours/wrangler.toml b/1000-hours/wrangler.toml new file mode 100644 index 00000000..0b31a14a --- /dev/null +++ b/1000-hours/wrangler.toml @@ -0,0 +1,13 @@ +name = "1000h-vtp" +main = "workers-site/index.js" +type = "webpack" +workers_dev = false +compatibility_date = "2023-03-23" + +routes = ["vtp.1000h.org/*"] + +[site] +bucket = "./.vitepress/dist" + + + diff --git a/entry/wrangler.toml b/entry/wrangler.toml index 0d306aee..99041fd2 100644 --- a/entry/wrangler.toml +++ b/entry/wrangler.toml @@ -5,7 +5,7 @@ compatibility_date = "2023-03-23" routes = ["next.1000h.org/*"] services = [ - { binding = "vtp", service = "1000-hours-prod" }, + { binding = "vtp", service = "1000h-vtp" }, { binding = "portal", service = "1000h-portal" }, ]