Initial commit (by create-cloudflare CLI)
Details: C3 = create-cloudflare@2.21.1 project name = status package manager = npm@9.6.7 wrangler = wrangler@3.52.0 git = 2.39.3 (Apple Git-146)
This commit is contained in:
commit
80f6b69d04
10 changed files with 1660 additions and 0 deletions
12
.editorconfig
Normal file
12
.editorconfig
Normal 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
172
.gitignore
vendored
Normal 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
6
.prettierrc
Normal file
|
@ -0,0 +1,6 @@
|
|||
{
|
||||
"printWidth": 140,
|
||||
"singleQuote": true,
|
||||
"semi": true,
|
||||
"useTabs": true
|
||||
}
|
1280
package-lock.json
generated
Normal file
1280
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load diff
13
package.json
Normal file
13
package.json
Normal file
|
@ -0,0 +1,13 @@
|
|||
{
|
||||
"name": "status",
|
||||
"version": "0.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"deploy": "wrangler deploy",
|
||||
"dev": "wrangler dev",
|
||||
"start": "wrangler dev"
|
||||
},
|
||||
"devDependencies": {
|
||||
"wrangler": "^3.0.0"
|
||||
}
|
||||
}
|
23
src/proxy.js
Normal file
23
src/proxy.js
Normal file
|
@ -0,0 +1,23 @@
|
|||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
const url = new URL(request.url);
|
||||
|
||||
const proxyUrl = url.searchParams.get('proxyUrl'); // get a query param value (?proxyUrl=...)
|
||||
const modify = url.searchParams.has('modify'); // check if a query param is set (?proxyUrl=...&modify)
|
||||
|
||||
if (!proxyUrl) {
|
||||
return new Response('Bad request: Missing `proxyUrl` query param', { status: 400 });
|
||||
}
|
||||
|
||||
// make subrequests with the global `fetch()` function
|
||||
let res = await fetch(proxyUrl, request);
|
||||
|
||||
// optionally, modify the respone
|
||||
if (modify) {
|
||||
res = new Response(res.body, res)
|
||||
res.headers.set('X-My-Header', 'My Header Value');
|
||||
}
|
||||
|
||||
return res;
|
||||
},
|
||||
};
|
13
src/redirect.js
Normal file
13
src/redirect.js
Normal file
|
@ -0,0 +1,13 @@
|
|||
export default {
|
||||
async fetch(request, env, ctx) {
|
||||
const url = new URL(request.url);
|
||||
const redirectUrl = url.searchParams.get('redirectUrl'); // get a query param value (?redirectUrl=...)
|
||||
|
||||
if (!redirectUrl) {
|
||||
return new Response('Bad request: Missing `redirectUrl` query param', { status: 400 });
|
||||
}
|
||||
|
||||
// The Response class has static methods to create common Response objects as a convenience
|
||||
return Response.redirect(redirectUrl);
|
||||
},
|
||||
};
|
90
src/router.js
Normal file
90
src/router.js
Normal file
|
@ -0,0 +1,90 @@
|
|||
// An example 'toy' implementation of a router using URLPattern: https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
|
||||
// If you're interested in more production-ready routing, you may want to check out one of the following:
|
||||
|
||||
// itty-router: https://www.npmjs.com/package/itty-router
|
||||
// Hono: https://www.npmjs.com/package/hono
|
||||
|
||||
class Router {
|
||||
routes = [];
|
||||
|
||||
handle(request) {
|
||||
for (const route of this.routes) {
|
||||
const match = route[0](request);
|
||||
if (match) {
|
||||
return route[1]({ ...match, request });
|
||||
}
|
||||
}
|
||||
const match = this.routes.find(([matcher]) => matcher(request));
|
||||
if (match) {
|
||||
return match[1](request);
|
||||
}
|
||||
}
|
||||
|
||||
register(handler, path, method) {
|
||||
const urlPattern = new URLPattern({ pathname: path });
|
||||
this.routes.push([
|
||||
(request) => {
|
||||
if (method === undefined || request.method.toLowerCase() === method) {
|
||||
const match = urlPattern.exec({
|
||||
pathname: new URL(request.url).pathname,
|
||||
});
|
||||
if (match) {
|
||||
return { params: match.pathname.groups };
|
||||
}
|
||||
}
|
||||
},
|
||||
(args) => handler(args),
|
||||
]);
|
||||
}
|
||||
|
||||
options(path, handler) {
|
||||
this.register(handler, path, "options");
|
||||
}
|
||||
head(path, handler) {
|
||||
this.register(handler, path, "head");
|
||||
}
|
||||
get(path, handler) {
|
||||
this.register(handler, path, "get");
|
||||
}
|
||||
post(path, handler) {
|
||||
this.register(handler, path, "post");
|
||||
}
|
||||
put(path, handler) {
|
||||
this.register(handler, path, "put");
|
||||
}
|
||||
patch(path, handler) {
|
||||
this.register(handler, path, "patch");
|
||||
}
|
||||
delete(path, handler) {
|
||||
this.register(handler, path, "delete");
|
||||
}
|
||||
|
||||
all(path, handler) {
|
||||
this.register(handler, path);
|
||||
}
|
||||
}
|
||||
|
||||
// Setting up our application:
|
||||
|
||||
const router = new Router();
|
||||
|
||||
// GET collection index
|
||||
router.get("/api/todos", () => new Response("Todos Index!"));
|
||||
|
||||
// GET item
|
||||
router.get(
|
||||
"/api/todos/:id",
|
||||
({ params }) => new Response(`Todo #${params.id}`)
|
||||
);
|
||||
|
||||
// POST to the collection (we'll use async here)
|
||||
router.post("/api/todos", async ({ request }) => {
|
||||
const content = await request.json();
|
||||
|
||||
return new Response("Creating Todo: " + JSON.stringify(content));
|
||||
});
|
||||
|
||||
// 404 for everything else
|
||||
router.all("*", () => new Response("Not Found.", { status: 404 }));
|
||||
|
||||
export default router;
|
47
src/worker.js
Normal file
47
src/worker.js
Normal file
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* 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/
|
||||
*/
|
||||
|
||||
import handleProxy from './proxy.js';
|
||||
import handleRedirect from './redirect.js';
|
||||
import apiRouter from './router.js';
|
||||
|
||||
// Export a default object containing event handlers
|
||||
export default {
|
||||
// The fetch handler is invoked when this worker receives a HTTP(S) request
|
||||
// and should return a Response (optionally wrapped in a Promise)
|
||||
async fetch(request, env, ctx) {
|
||||
// You'll find it helpful to parse the request.url string into a URL object. Learn more at https://developer.mozilla.org/en-US/docs/Web/API/URL
|
||||
const url = new URL(request.url);
|
||||
|
||||
// You can get pretty far with simple logic like if/switch-statements
|
||||
switch (url.pathname) {
|
||||
case '/redirect':
|
||||
return handleRedirect.fetch(request, env, ctx);
|
||||
|
||||
case '/proxy':
|
||||
return handleProxy.fetch(request, env, ctx);
|
||||
}
|
||||
|
||||
if (url.pathname.startsWith('/api/')) {
|
||||
// You can also use more robust routing
|
||||
return apiRouter.handle(request);
|
||||
}
|
||||
|
||||
|
||||
return new Response(
|
||||
`Try making requests to:
|
||||
<ul>
|
||||
<li><code><a href="/redirect?redirectUrl=https://example.com/">/redirect?redirectUrl=https://example.com/</a></code>,</li>
|
||||
<li><code><a href="/proxy?modify&proxyUrl=https://example.com/">/proxy?modify&proxyUrl=https://example.com/</a></code>, or</li>
|
||||
<li><code><a href="/api/todos">/api/todos</a></code></li>`,
|
||||
{ headers: { "Content-Type": "text/html" } }
|
||||
);
|
||||
},
|
||||
};
|
4
wrangler.toml
Normal file
4
wrangler.toml
Normal file
|
@ -0,0 +1,4 @@
|
|||
name = "status"
|
||||
main = "src/worker.js"
|
||||
compatibility_date = "2024-05-09"
|
||||
workers_dev = true
|
Loading…
Reference in a new issue