Skip to content

Commit f6acad4

Browse files
committed
improve use of worker variables
1 parent 37b7f2d commit f6acad4

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

src/index.js

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,31 @@ function routeByHosts(host) {
1414
if (host in routes) {
1515
return routes[host];
1616
}
17-
return TARGET_UPSTREAM ? TARGET_UPSTREAM : "";
17+
if (MODE == "debug") {
18+
return TARGET_UPSTREAM;
19+
}
20+
return "";
1821
}
1922

2023
async function handleRequest(request) {
2124
const url = new URL(request.url);
2225
if (url.pathname == "/v2/") {
23-
const resp = new Response({}, { status: 401 });
24-
resp.headers = new Headers();
25-
if (DEBUG) {
26-
resp.headers.set(
26+
const headers = new Headers();
27+
if (MODE == "debug") {
28+
headers.set(
2729
"Www-Authenticate",
2830
`Bearer realm="${LOCAL_ADDRESS}/v2/auth",service="cloudflare-docker-proxy"`
2931
);
3032
} else {
31-
resp.headers.set(
33+
headers.set(
3234
"Www-Authenticate",
3335
`Bearer realm="https://${url.hostname}/v2/auth",service="cloudflare-docker-proxy"`
3436
);
3537
}
36-
return resp;
38+
return new Response(JSON.stringify({ message: "UNAUTHORIZED" }), {
39+
status: 401,
40+
headers: headers,
41+
});
3742
}
3843
const upstream = routeByHosts(url.hostname);
3944
if (upstream === "") {

wrangler.toml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,12 @@ port = 8787
1616
local_protocol="http"
1717
upstream_protocol="https"
1818

19-
[env.dev]
20-
vars={DEBUG=true, LOCAL_ADDRESS="http://192.168.50.160:8787", TARGET_UPSTREAM="https://registry-1.docker.io"}
19+
[vars]
20+
MODE="production"
21+
LOCAL_ADDRESS=""
22+
TARGET_UPSTREAM=""
23+
24+
[env.dev.vars]
25+
MODE="debug"
26+
LOCAL_ADDRESS="http://192.168.50.160:8787"
27+
TARGET_UPSTREAM="https://registry-1.docker.io"

0 commit comments

Comments
 (0)