From 4435a8611dde4cf7bc5ef76cb75e1e104e21f538 Mon Sep 17 00:00:00 2001 From: Andrei Jiroh Halili Date: Fri, 22 Dec 2023 16:58:58 +0800 Subject: [PATCH] feat(api): :sparkles: add sources for Garden API Signed-off-by: Andrei Jiroh Halili --- api/deps.ts | 2 ++ api/index.ts | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 api/deps.ts create mode 100644 api/index.ts diff --git a/api/deps.ts b/api/deps.ts new file mode 100644 index 0000000..c9a22db --- /dev/null +++ b/api/deps.ts @@ -0,0 +1,2 @@ +// default deps here +export type { IncomingRequestCf } from 'https://raw.githubusercontent.com/skymethod/denoflare/v0.5.12/common/cloudflare_workers_types.d.ts'; \ No newline at end of file diff --git a/api/index.ts b/api/index.ts new file mode 100644 index 0000000..e88f948 --- /dev/null +++ b/api/index.ts @@ -0,0 +1,35 @@ +import { IncomingRequestCf } from './deps.ts'; + +export default { + fetch(request: IncomingRequestCf, env: any) { + const urlParser = new URL(request.url) + if (urlParser.pathname == "/api") { + return new Response("Hello, world!", { status: 200 }) + } else if (urlParser.pathname == "/api/debug") { + const data = JSON.stringify({ + ok: true, + cdnCallback: { + botManagement: request.cf.botManagement, + colocation: request.cf.colo + }, + requestData: { + ip: request.headers.get("CF-Connecting-IP"), + tz: `${'timezone' in request.cf ? request.cf.timezone : "UTC"}`, + asn: request.cf.asn, + asOrg: request.cf.asOrganization, + city: `${'city' in request.cf ? request.cf.city : null }`, + ua: request.headers.get("User-Agent") + } + }) + return new Response(data, { + status: 200, + headers: { 'Content-Type': 'application/json'} + }) + } + const default404Reply = JSON.stringify({ + ok: false, + error: "API endpoint not found" + }) + return new Response(default404Reply, { status: 404}) + } +} -- 2.51.2