Skip to content

Commit

Permalink
Add Cloudflare worker automation scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
Phillweston committed Mar 31, 2024
1 parent 7d8bd5c commit 1a89496
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Deploy Worker to Cloudflare

on:
push:
branches: ["master"]

jobs:
deploy:
runs-on: ubuntu-latest
defaults:
run:
working-directory: worker
steps:
- uses: actions/checkout@v2

- name: Install Wrangler
run: npm install -g wrangler

- name: Publish the Worker
run: wrangler publish --env production
env:
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
CLOUDFLARE_API_KEY: ${{ secrets.CLOUDFLARE_API_KEY }}
27 changes: 27 additions & 0 deletions worker/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});

async function handleRequest(request) {
// Modify the request URL
const url = new URL(request.url);
url.protocol = 'http:';
url.hostname = 'host.btiplatform.com';
url.port = '8080';

// Fetch the data from the modified URL
const response = await fetch(url.toString(), request);

// Create a new response with CORS headers
const newHeaders = new Headers(response.headers);
newHeaders.set("Access-Control-Allow-Origin", "*"); // Adjust as needed for security
newHeaders.set("Access-Control-Allow-Methods", "GET, POST");
newHeaders.set("Access-Control-Allow-Headers", "Content-Type, Authorization");

// Return the response with the new headers
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders
});
}
8 changes: 8 additions & 0 deletions wrangle.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name = "api"

[site]
bucket = "./worker" # Path to your worker code

[env.production]
# The account_id is set through an environment variable in the GitHub Actions workflow
account_id = "$CLOUDFLARE_ACCOUNT_ID"

0 comments on commit 1a89496

Please sign in to comment.