Skip to content

Commit 038e320

Browse files
committed
Adding environments
1 parent 0808cc7 commit 038e320

File tree

4 files changed

+38
-8
lines changed

4 files changed

+38
-8
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "labelzoom-cf-api-proxy",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"private": true,
55
"scripts": {
66
"deploy": "wrangler deploy",

src/index.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
import { handleOptions, responseWithAllowOrigin } from "./cors";
1515

16-
const IS_TRUE = /true/i;
17-
1816
/**
1917
* Log conversion request and response data to Cloudflare R2
2018
* @param request
@@ -25,7 +23,7 @@ const IS_TRUE = /true/i;
2523
*/
2624
async function handleConversionLog(request: Request<unknown, IncomingRequestCfProperties<unknown>>, env: Env, ctx: ExecutionContext, url: URL) {
2725
const requestID = new Date().toISOString().substring(0, 19).replaceAll('-', '/').replaceAll('T', '/').replaceAll(':', '') + '--' + crypto.randomUUID();
28-
const loggingEnabled = IS_TRUE.test(env.LZ_LOG_CONVERSION_DATA);
26+
const loggingEnabled = Math.random() < env.LZ_LOG_SAMPLE_RATE;
2927

3028
// Clone and log request asynchronously
3129
if (loggingEnabled) ctx.waitUntil(env.LZ_R2_BUCKET.put(requestID + '/in', request.clone().body));
@@ -49,7 +47,7 @@ async function handleConversionLog(request: Request<unknown, IncomingRequestCfPr
4947
*/
5048
async function proxyRequestToBackend(request: Request, url: URL, env: Env, requestID = ''): Promise<Response> {
5149
const backendUrl = env.LZ_PROD_API_BASE_URL + url.pathname + url.search;
52-
const newRequest = new Request(backendUrl ?? request.url, request);
50+
const newRequest = new Request(backendUrl, request);
5351
let ip = request.headers.get("X-Forwarded-For") ?? '';
5452
if (!ip) {
5553
ip = request.headers.get("Cf-Connecting-Ip") ?? '';

worker-configuration.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated by Wrangler
22
// After adding bindings to `wrangler.toml`, regenerate this interface via `npm run cf-typegen`
33
interface Env {
4-
LZ_LOG_CONVERSION_DATA: string;
4+
LZ_LOG_SAMPLE_RATE: number;
55
LZ_PROD_API_BASE_URL: string;
66
LZ_PROD_API_SECRET_KEY: string;
77
LZ_R2_BUCKET: R2Bucket;

wrangler.toml

+34-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ compatibility_flags = ["nodejs_compat"]
66
tail_consumers = [{service = "labelzoom-cf-api-proxy-tail"}]
77

88
[limits]
9-
cpu_ms = 1000
9+
cpu_ms = 100
1010

1111
# Automatically place your workloads in an optimal location to minimize latency.
1212
# If you are running back-end logic in a Worker, running it closer to your back-end infrastructure
@@ -21,10 +21,30 @@ cpu_ms = 1000
2121
# Note: Use secrets to store sensitive data.
2222
# - https://developers.cloudflare.com/workers/configuration/secrets/
2323
[vars]
24-
LZ_LOG_CONVERSION_DATA = "false"
24+
LZ_LOG_SAMPLE_RATE = 0.0
2525
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
2626
# LZ_PROD_API_SECRET_KEY = <SECRET>
2727

28+
[env.beta.vars]
29+
LZ_LOG_SAMPLE_RATE = 1.0
30+
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
31+
32+
[env.public.vars]
33+
LZ_LOG_SAMPLE_RATE = 0.0
34+
LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
35+
36+
[env.eus1.vars]
37+
LZ_LOG_SAMPLE_RATE = 0.0
38+
LZ_PROD_API_BASE_URL = "https://prod-api-eus1.labelzoom.net"
39+
# [env.eus1.placement]
40+
# mode = "smart"
41+
42+
[env.eus2.vars]
43+
LZ_LOG_SAMPLE_RATE = 0.0
44+
LZ_PROD_API_BASE_URL = "https://prod-api-eus2.labelzoom.net"
45+
# [env.eus2.placement]
46+
# mode = "smart"
47+
2848
# Bind the Workers AI model catalog. Run machine learning models, powered by serverless GPUs, on Cloudflare’s global network
2949
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#workers-ai
3050
# [ai]
@@ -99,6 +119,18 @@ LZ_PROD_API_BASE_URL = "https://api-backend.labelzoom.net"
99119
# Docs: https://developers.cloudflare.com/workers/wrangler/configuration/#r2-buckets
100120
[[r2_buckets]]
101121
binding = "LZ_R2_BUCKET"
122+
bucket_name = "labelzoom-conversions-dev"
123+
124+
[[env.public.r2_buckets]]
125+
binding = "LZ_R2_BUCKET"
126+
bucket_name = "labelzoom-conversions-prod"
127+
128+
[[env.eus1.r2_buckets]]
129+
binding = "LZ_R2_BUCKET"
130+
bucket_name = "labelzoom-conversions-prod"
131+
132+
[[env.eus2.r2_buckets]]
133+
binding = "LZ_R2_BUCKET"
102134
bucket_name = "labelzoom-conversions-prod"
103135

104136
# Bind another Worker service. Use this binding to call another Worker without network overhead.

0 commit comments

Comments
 (0)