Initial commit (by create-cloudflare CLI)

Details:
  C3 = create-cloudflare@2.39.0
  project name = web-tombutcher-work
  package manager = npm@10.8.3
  wrangler = wrangler@3.111.0
  git = 2.39.5 (Apple Git-154)
This commit is contained in:
Tom Butcher 2025-03-03 21:59:21 +00:00
commit 6eecd3c414
15 changed files with 3542 additions and 0 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
# http://editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.yml]
indent_style = space

172
.gitignore vendored Normal file
View File

@ -0,0 +1,172 @@
# Logs
logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
# Runtime data
pids
_.pid
_.seed
\*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
\*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
\*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
\*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*
# wrangler project
.dev.vars
.wrangler/

6
.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"printWidth": 140,
"singleQuote": true,
"semi": true,
"useTabs": true
}

5
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,5 @@
{
"files.associations": {
"wrangler.json": "jsonc"
}
}

3135
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

16
package.json Normal file
View File

@ -0,0 +1,16 @@
{
"name": "web-tombutcher-work",
"version": "0.0.0",
"private": true,
"scripts": {
"deploy": "wrangler deploy",
"dev": "wrangler dev",
"start": "wrangler dev",
"test": "vitest"
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "^0.6.4",
"vitest": "~2.1.9",
"wrangler": "^3.111.0"
}
}

15
src/index.js Normal file
View File

@ -0,0 +1,15 @@
/**
* Welcome to Cloudflare Workers! This is your first worker.
*
* - Run `npm run dev` in your terminal to start a development server
* - Open a browser tab at http://localhost:8787/ to see your worker in action
* - Run `npm run deploy` to publish your worker
*
* Learn more at https://developers.cloudflare.com/workers/
*/
export default {
async fetch(request, env, ctx) {
return new Response('Hello World!');
},
};

44
src/routes/contact.js Normal file
View File

@ -0,0 +1,44 @@
import { getLocation } from '../utils/geolocation.js';
import { addToNotionDatabase } from '../utils/notion.js';
const TURNSTILE_SECRET_KEY = 'your-turnstile-secret-key';
export async function handleContactRequest(request) {
const { email, token } = await request.json();
if (!email || !token) {
return new Response('Email and token are required', { status: 400 });
}
// Extract the IP address from the request headers
const ip = request.headers.get('CF-Connecting-IP');
// Get location info
const location = await getLocation(ip);
// Verify the Turnstile token
const verificationResponse = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
secret: TURNSTILE_SECRET_KEY,
response: token,
}),
});
const verificationData = await verificationResponse.json();
if (!verificationData.success) {
return new Response('Turnstile verification failed', { status: 400 });
}
// Add the email and location to Notion
try {
await addToNotionDatabase(email, location);
return new Response(JSON.stringify({ success: true, message: 'Email processed and added to Notion' }), {
headers: { 'Content-Type': 'application/json' },
});
} catch (error) {
return new Response('Error processing email', { status: 500 });
}
}

0
src/routes/download.js Normal file
View File

0
src/routes/socials.js Normal file
View File

18
src/utils/geolocation.js Normal file
View File

@ -0,0 +1,18 @@
const GEOLOCATION_API_KEY = 'your-geolocation-api-key';
export async function getLocation(ip) {
const geoApiUrl = `https://api.ipgeolocation.io/ipgeo?apiKey=${GEOLOCATION_API_KEY}&ip=${ip}`;
const response = await fetch(geoApiUrl);
const data = await response.json();
if (response.ok) {
return {
city: data.city,
country: data.country_name,
region: data.state_prov,
};
} else {
throw new Error('Error getting location data');
}
}

41
src/utils/notion.js Normal file
View File

@ -0,0 +1,41 @@
const NOTION_API_TOKEN = 'your-notion-api-token';
export async function addToNotionDatabase(params, database) {
const notionApiUrl = `https://api.notion.com/v1/pages`;
const notionPayload = {
parent: {
database_id: database,
},
properties: {},
};
// Loop through each key-value pair in the params object
for (const [key, value] of Object.entries(params)) {
notionPayload.properties[key] = {
rich_text: [
{
text: {
content: value,
},
},
],
};
}
const response = await fetch(notionApiUrl, {
method: 'POST',
headers: {
Authorization: `Bearer ${NOTION_API_TOKEN}`,
'Content-Type': 'application/json',
'Notion-Version': '2021-05-13',
},
body: JSON.stringify(notionPayload),
});
if (!response.ok) {
throw new Error('Failed to add to Notion');
}
return response.json();
}

20
test/index.spec.js Normal file
View File

@ -0,0 +1,20 @@
import { env, createExecutionContext, waitOnExecutionContext, SELF } from 'cloudflare:test';
import { describe, it, expect } from 'vitest';
import worker from '../src';
describe('Hello World worker', () => {
it('responds with Hello World! (unit style)', async () => {
const request = new Request('http://example.com');
// Create an empty context to pass to `worker.fetch()`.
const ctx = createExecutionContext();
const response = await worker.fetch(request, env, ctx);
// Wait for all `Promise`s passed to `ctx.waitUntil()` to settle before running test assertions
await waitOnExecutionContext(ctx);
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
});
it('responds with Hello World! (integration style)', async () => {
const response = await SELF.fetch('http://example.com');
expect(await response.text()).toMatchInlineSnapshot(`"Hello World!"`);
});
});

11
vitest.config.js Normal file
View File

@ -0,0 +1,11 @@
import { defineWorkersConfig } from '@cloudflare/vitest-pool-workers/config';
export default defineWorkersConfig({
test: {
poolOptions: {
workers: {
wrangler: { configPath: './wrangler.jsonc' },
},
},
},
});

47
wrangler.jsonc Normal file
View File

@ -0,0 +1,47 @@
/**
* For more details on how to configure Wrangler, refer to:
* https://developers.cloudflare.com/workers/wrangler/configuration/
*/
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "web-tombutcher-work",
"main": "src/index.js",
"compatibility_date": "2025-02-24",
"observability": {
"enabled": true
}
/**
* Smart Placement
* Docs: https://developers.cloudflare.com/workers/configuration/smart-placement/#smart-placement
*/
// "placement": { "mode": "smart" },
/**
* Bindings
* Bindings allow your Worker to interact with resources on the Cloudflare Developer Platform, including
* databases, object storage, AI inference, real-time communication and more.
* https://developers.cloudflare.com/workers/runtime-apis/bindings/
*/
/**
* Environment Variables
* https://developers.cloudflare.com/workers/wrangler/configuration/#environment-variables
*/
// "vars": { "MY_VARIABLE": "production_value" },
/**
* Note: Use secrets to store sensitive data.
* https://developers.cloudflare.com/workers/configuration/secrets/
*/
/**
* Static Assets
* https://developers.cloudflare.com/workers/static-assets/binding/
*/
// "assets": { "directory": "./public/", "binding": "ASSETS" },
/**
* Service Bindings (communicate between multiple Workers)
* https://developers.cloudflare.com/workers/wrangler/configuration/#service-bindings
*/
// "services": [{ "binding": "MY_SERVICE", "service": "my-service" }]
}