Skip to content

Commit de6bd23

Browse files
committed
Move listen option parsing to separate function
1 parent 7ee4d09 commit de6bd23

File tree

2 files changed

+47
-39
lines changed

2 files changed

+47
-39
lines changed

bin/configurable-http-proxy

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import fs from "node:fs";
1010
import { Command } from "commander";
1111

1212
import ConfigurableProxy from "../lib/configproxy.js";
13+
import { parseListenOptions } from "../lib/configproxy.js";
14+
1315
import { defaultLogger } from "../lib/log.js";
1416

1517
import { createRequire } from "node:module";
@@ -282,7 +284,7 @@ options.proxyTimeout = args.proxyTimeout;
282284
options.keepAliveTimeout = args.keepAliveTimeout;
283285

284286
// metrics options
285-
options.enableMetrics = !!args.metricsPort;
287+
options.enableMetrics = !!args.metricsPort || !!args.metricsSocket;
286288

287289
// certs need to be provided for https redirection
288290
if (!options.ssl && options.redirectPort) {
@@ -323,43 +325,7 @@ options.storageBackend = args.storageBackend;
323325

324326
var proxy = new ConfigurableProxy(options);
325327

326-
var listen = {};
327-
328-
if (args.socket) {
329-
listen.proxyTarget = [args.socket];
330-
log.warn(
331-
"Proxy will listen on UNIX domain socket, --ip and --port options will be ignored."
332-
);
333-
} else {
334-
listen.port = parseInt(args.port) || 8000;
335-
if (args.ip === "*") {
336-
// handle ip=* alias for all interfaces
337-
log.warn(
338-
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
339-
);
340-
args.ip = "";
341-
}
342-
listen.ip = args.ip;
343-
listen.proxyTarget = [listen.port, listen.ip];
344-
}
345-
346-
if (args.apiSocket) {
347-
listen.apiSocket = [args.apiSocket];
348-
log.warn(
349-
"API server will listen on UNIX domain socket, --api-ip and --api-port options will be ignored."
350-
);
351-
} else {
352-
listen.apiTarget = [args.apiPort || (listen.port ? listen.port + 1 : 8001), args.apiIp];
353-
}
354-
355-
if (args.metricsSocket) {
356-
listen.metricsSocket = [args.metricsSocket];
357-
log.warn(
358-
"Metrics server will listen on UNIX domain socket, --metrics-ip and --metrics-port options will be ignored."
359-
);
360-
} else {
361-
listen.metricsTarget = [args.metricsPort, args.metricsIp];
362-
}
328+
var listen = parseListenOptions(args);
363329

364330
proxy.proxyServer.listen(...listen.proxyTarget);
365331
proxy.apiServer.listen(...listen.apiTarget);
@@ -378,7 +344,7 @@ log.info(
378344
options.apiSsl ? "https" : "http",
379345
listen.apiTarget.join(":")
380346
);
381-
if (listen.metricsPort) {
347+
if (listen.metricsTarget) {
382348
log.info("Serve metrics at %s://%s/metrics", "http", listen.metricsTarget.join(":"));
383349
}
384350

lib/configproxy.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,48 @@ const require = createRequire(import.meta.url);
2424

2525
const __dirname = path.dirname(fileURLToPath(import.meta.url));
2626

27+
export function parseListenOptions(args) {
28+
var listen = {};
29+
30+
if (args.socket) {
31+
listen.proxyTarget = [args.socket];
32+
log.warn(
33+
"Proxy will listen on UNIX domain socket, --ip and --port options will be ignored."
34+
);
35+
} else {
36+
listen.port = parseInt(args.port) || 8000;
37+
if (args.ip === "*") {
38+
// handle ip=* alias for all interfaces
39+
log.warn(
40+
"Interpreting ip='*' as all-interfaces. Preferred usage is 0.0.0.0 for all IPv4 or '' for all-interfaces."
41+
);
42+
args.ip = "";
43+
}
44+
listen.ip = args.ip;
45+
listen.proxyTarget = [listen.port, listen.ip];
46+
}
47+
48+
if (args.apiSocket) {
49+
listen.apiSocket = [args.apiSocket];
50+
log.warn(
51+
"API server will listen on UNIX domain socket, --api-ip and --api-port options will be ignored."
52+
);
53+
} else {
54+
listen.apiPort = args.apiPort ? parseInt(args.apiPort) : (listen.port ? listen.port + 1 : 8001);
55+
listen.apiTarget = [listen.apiPort, args.apiIp];
56+
}
57+
58+
if (args.metricsSocket) {
59+
listen.metricsSocket = [args.metricsSocket];
60+
log.warn(
61+
"Metrics server will listen on UNIX domain socket, --metrics-ip and --metrics-port options will be ignored."
62+
);
63+
} else if (args.metricsPort) {
64+
listen.metricsTarget = [parseInt(args.metricsPort), args.metricsIp];
65+
}
66+
return listen;
67+
}
68+
2769
function bound(that, method) {
2870
// bind a method, to ensure `this=that` when it is called
2971
// because prototype languages are bad

0 commit comments

Comments
 (0)