Deploy Natali Proxy Worker #14
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Deploy Natali Proxy Worker | |
| on: | |
| workflow_dispatch: | |
| permissions: | |
| contents: read | |
| jobs: | |
| deploy-proxy: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Deploy Natali Proxy Worker | |
| env: | |
| CF_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }} | |
| run: | | |
| set -x | |
| ZONE_ID="5b3f8237bfa2518bff375eb8163623a5" | |
| WORKER_NAME="natalie-proxy" | |
| cat > /tmp/worker.js << 'WORKEREOF' | |
| const RAW_BASE = 'https://raw.githubusercontent.com/spivanatalie64/spivanatalie64.github.io/gh-pages'; | |
| const MIME_TYPES = { | |
| '.html': 'text/html; charset=utf-8', | |
| '.css': 'text/css; charset=utf-8', | |
| '.js': 'application/javascript; charset=utf-8', | |
| '.json': 'application/json; charset=utf-8', | |
| '.png': 'image/png', | |
| '.jpg': 'image/jpeg', | |
| '.jpeg': 'image/jpeg', | |
| '.gif': 'image/gif', | |
| '.svg': 'image/svg+xml', | |
| '.ico': 'image/x-icon', | |
| '.webp': 'image/webp', | |
| '.pdf': 'application/pdf', | |
| '.woff': 'font/woff', | |
| '.woff2': 'font/woff2', | |
| '.ttf': 'font/ttf', | |
| }; | |
| addEventListener('fetch', event => { | |
| event.respondWith(handleRequest(event.request)); | |
| }); | |
| async function handleRequest(request) { | |
| try { | |
| const url = new URL(request.url); | |
| const path = url.pathname; | |
| const extMatch = path.match(/\.(\w+)$/); | |
| const ext = extMatch ? extMatch[0].toLowerCase() : ''; | |
| const isStaticAsset = !!MIME_TYPES[ext]; | |
| let rawPath = path; | |
| if (path === '/' || path === '') rawPath = '/index.html'; | |
| const rawUrl = RAW_BASE + rawPath; | |
| const response = await fetch(rawUrl); | |
| if (response.ok || isStaticAsset) { | |
| const newHeaders = new Headers(response.headers); | |
| const contentType = MIME_TYPES[ext] || 'text/html; charset=utf-8'; | |
| newHeaders.set('Content-Type', contentType); | |
| newHeaders.delete('content-security-policy'); | |
| if (isStaticAsset) { | |
| newHeaders.set('Cache-Control', 'public, max-age=86400, s-maxage=2592000'); | |
| } else { | |
| newHeaders.set('Cache-Control', 'public, max-age=0, s-maxage=600'); | |
| } | |
| newHeaders.set('X-Proxied-By', 'natalie-worker'); | |
| return new Response(response.body, { status: 200, statusText: 'OK', headers: newHeaders }); | |
| } | |
| // SPA fallback for 404s | |
| const indexRes = await fetch(RAW_BASE + '/index.html'); | |
| if (indexRes.ok) { | |
| const newHeaders = new Headers(indexRes.headers); | |
| newHeaders.set('Content-Type', 'text/html; charset=utf-8'); | |
| newHeaders.delete('content-security-policy'); | |
| newHeaders.set('X-Proxied-By', 'natalie-worker'); | |
| return new Response(indexRes.body, { status: 200, statusText: 'OK', headers: newHeaders }); | |
| } | |
| return new Response('Not Found', { status: 404 }); | |
| } catch (err) { | |
| return new Response(err.message, { status: 500, headers: { 'Content-Type': 'text/plain' } }); | |
| } | |
| } | |
| WORKEREOF | |
| cat > /tmp/metadata.json << 'METADATAEOF' | |
| { | |
| "body_part": "script", | |
| "bindings": [], | |
| "compatibility_date": "2026-06-20", | |
| "compatibility_flags": [] | |
| } | |
| METADATAEOF | |
| curl -s -X PUT \ | |
| "https://api.cloudflare.com/client/v4/accounts/6a46b418508c26278ae2d3373f41da81/workers/scripts/$WORKER_NAME" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -F "metadata=@/tmp/metadata.json;type=application/json" \ | |
| -F "script=@/tmp/worker.js;type=application/javascript" | jq . | |
| EXISTING_ROUTES=$(curl -s -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes") | |
| NATALIE_ROUTE_ID=$(echo "$EXISTING_ROUTES" | jq -r '.result[] | select(.pattern | test("natalie\\.acreetionos\\.org")) | .id') | |
| curl -s -X PUT \ | |
| "https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes/$NATALIE_ROUTE_ID" \ | |
| -H "Authorization: Bearer $CF_API_TOKEN" \ | |
| -H "Content-Type: application/json" \ | |
| -d "{ | |
| \"pattern\": \"natalie.acreetionos.org/*\", | |
| \"script\": \"$WORKER_NAME\" | |
| }" | jq . | |
| echo "=== Done ===" |