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 7f8141f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/worker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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: Configure Wrangler with API Token
run: |
wrangler config --api-key
env:
CF_API_KEY: ${{ secrets.CF_API_TOKEN }}

- name: Publish the Worker
run: wrangler publish --env production
env:
CF_ACCOUNT_ID: ${{ secrets.CF_ACCOUNT_ID }}
CF_API_TOKEN: ${{ secrets.CF_API_TOKEN }}
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 = "$CF_ACCOUNT_ID"

0 comments on commit 7f8141f

Please sign in to comment.