chore: add cloudflare worker configurations
This commit is contained in:
parent
0c2b2f7d9b
commit
d6e779bd9d
|
@ -0,0 +1,10 @@
|
|||
/target
|
||||
/dist
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
bin/
|
||||
pkg/
|
||||
wasm-pack.log
|
||||
worker/
|
||||
node_modules/
|
||||
.cargo-ok
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"singleQuote": true,
|
||||
"semi": false,
|
||||
"trailingComma": "all",
|
||||
"tabWidth": 2,
|
||||
"printWidth": 80
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
import { getAssetFromKV } from '@cloudflare/kv-asset-handler'
|
||||
|
||||
addEventListener('fetch', event => {
|
||||
event.respondWith(handleEvent(event))
|
||||
})
|
||||
|
||||
async function handleEvent(event) {
|
||||
const url = new URL(event.request.url)
|
||||
|
||||
if (url.pathname.startsWith('/register')) {
|
||||
// Handle registration
|
||||
return handleRegister(event)
|
||||
} else {
|
||||
// Serve static assets
|
||||
return getAssetFromKV(event)
|
||||
}
|
||||
}
|
||||
|
||||
async function handleRegister(event) {
|
||||
if (event.request.method === 'POST') {
|
||||
const userData = await event.request.json()
|
||||
const auth0Id = userData.auth0Id
|
||||
|
||||
// Save to KV
|
||||
await KV_ECS.put(auth0Id, '0')
|
||||
|
||||
return new Response('User registered successfully', { status: 200 })
|
||||
} else {
|
||||
return new Response('Method not allowed', { status: 405 })
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
{
|
||||
"name": "cf_kv_connect",
|
||||
"version": "0.0.0",
|
||||
"devDependencies": {
|
||||
"@cloudflare/workers-types": "^4.20230518.0",
|
||||
"typescript": "^5.0.4",
|
||||
"wrangler": "2.20.0"
|
||||
},
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"start": "wrangler dev",
|
||||
"deploy": "wrangler publish"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
name = "cf_kv_connect"
|
||||
compatibility_date = "2023-05-23"
|
||||
|
||||
account_id = "214294b910a03a76514142a6719221f7"
|
||||
workers_dev = true
|
||||
|
||||
route = "https://enginecodesolutions.com/register"
|
||||
|
||||
kv_namespaces = [
|
||||
{ binding = "KV_ECS", id = "f1821473524c43eea0353b80f96e41e3" }
|
||||
]
|
|
@ -0,0 +1,10 @@
|
|||
/target
|
||||
/dist
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
bin/
|
||||
pkg/
|
||||
wasm-pack.log
|
||||
worker/
|
||||
node_modules/
|
||||
.cargo-ok
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"singleQuote": true,
|
||||
"semi": false,
|
||||
"trailingComma": "all",
|
||||
"tabWidth": 2,
|
||||
"printWidth": 80
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
// Import the Stripe library
|
||||
const stripe = require('stripe')(STRIPE_SECRET_KEY);
|
||||
|
||||
addEventListener('fetch', event => {
|
||||
event.respondWith(handleRequest(event.request));
|
||||
})
|
||||
|
||||
async function handleRequest(request) {
|
||||
// We assume 1 token = 1 USD
|
||||
const price = 1;
|
||||
|
||||
// Extract quantity from the request
|
||||
const requestBody = await request.json();
|
||||
const quantity = requestBody.quantity;
|
||||
|
||||
// Create a checkout session
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
payment_method_types: ['card'],
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
product_data: {
|
||||
name: 'Token',
|
||||
},
|
||||
unit_amount: price * 100, // Stripe uses cents for unit amount
|
||||
},
|
||||
quantity: quantity,
|
||||
},
|
||||
],
|
||||
mode: 'payment',
|
||||
success_url: 'https://enginecodesolutions.com/success',
|
||||
cancel_url: 'https://enginecodesolutions.com/cancel',
|
||||
});
|
||||
|
||||
return new Response(JSON.stringify({ id: session.id }), { status: 200 });
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
{
|
||||
"private": true,
|
||||
"name": "token-purchase-worker",
|
||||
"version": "1.0.0",
|
||||
"description": "A template for kick starting a Cloudflare Workers project",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"format": "prettier --write '**/*.{js,css,json,md}'"
|
||||
},
|
||||
"author": "{{ authors }}",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"prettier": "^1.18.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"stripe": "^12.6.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
name = "token-purchase-worker"
|
||||
|
||||
account_id = "214294b910a03a76514142a6719221f7"
|
||||
workers_dev = true
|
||||
|
||||
compatibility_date = "2023-05-23"
|
Loading…
Reference in New Issue