From 9494599c486d557a67d1e4ff9fb0c91985bd09b3 Mon Sep 17 00:00:00 2001 From: itxaiohanglover <1531137510@qq.com> Date: Sun, 5 Jul 2026 23:07:38 +0800 Subject: [PATCH] [ISSUE #401 #405] Support runtime API base URL configuration for Docker The frontend API base URL was hardcoded at build time, causing issues when deploying via Docker (the built image always points to localhost:8082). This commit introduces a runtime configuration mechanism: - Add env-config.js loaded before React app, providing window.__ENV__ - Update remoteApi.js to check window.__ENV__.API_BASE_URL first - Add Docker entrypoint script that extracts and patches env-config.js when API_BASE_URL environment variable is set - Users can now set API_BASE_URL in docker-compose to configure the frontend at container startup without rebuilding This fixes both #401 (Docker localhost issue) and #405 (release API localhost:8082 issue). --- frontend-new/public/env-config.js | 19 +++++++++++++ frontend-new/public/index.html | 1 + frontend-new/src/api/remoteApi/remoteApi.js | 2 +- src/main/docker/Dockerfile | 5 +++- src/main/docker/env-config-entrypoint.sh | 31 +++++++++++++++++++++ 5 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 frontend-new/public/env-config.js create mode 100755 src/main/docker/env-config-entrypoint.sh diff --git a/frontend-new/public/env-config.js b/frontend-new/public/env-config.js new file mode 100644 index 00000000..f470ad04 --- /dev/null +++ b/frontend-new/public/env-config.js @@ -0,0 +1,19 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +window.__ENV__ = window.__ENV__ || {}; +window.__ENV__.API_BASE_URL = ""; diff --git a/frontend-new/public/index.html b/frontend-new/public/index.html index 2208f39b..81229f00 100644 --- a/frontend-new/public/index.html +++ b/frontend-new/public/index.html @@ -25,6 +25,7 @@ RocketMQ Dashboard + diff --git a/frontend-new/src/api/remoteApi/remoteApi.js b/frontend-new/src/api/remoteApi/remoteApi.js index e39a9c69..2be18161 100644 --- a/frontend-new/src/api/remoteApi/remoteApi.js +++ b/frontend-new/src/api/remoteApi/remoteApi.js @@ -16,7 +16,7 @@ */ const appConfig = { - apiBaseUrl: process.env.REACT_APP_API_BASE_URL || window.location.origin + apiBaseUrl: (window.__ENV__ && window.__ENV__.API_BASE_URL) || process.env.REACT_APP_API_BASE_URL || window.location.origin }; let _redirectHandler = null; diff --git a/src/main/docker/Dockerfile b/src/main/docker/Dockerfile index 2961fed9..485da525 100644 --- a/src/main/docker/Dockerfile +++ b/src/main/docker/Dockerfile @@ -20,4 +20,7 @@ VOLUME /tmp ADD rocketmq-dashboard-*.jar rocketmq-dashboard.jar RUN sh -c 'touch /rocketmq-dashboard.jar' ENV JAVA_OPTS="" -ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /rocketmq-dashboard.jar" ] +ENV API_BASE_URL="" +COPY env-config-entrypoint.sh /env-config-entrypoint.sh +RUN chmod +x /env-config-entrypoint.sh +ENTRYPOINT [ "/env-config-entrypoint.sh" ] diff --git a/src/main/docker/env-config-entrypoint.sh b/src/main/docker/env-config-entrypoint.sh new file mode 100755 index 00000000..f6837d8b --- /dev/null +++ b/src/main/docker/env-config-entrypoint.sh @@ -0,0 +1,31 @@ +#!/bin/sh +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# If API_BASE_URL is set, extract env-config.js from the jar and update it +if [ -n "$API_BASE_URL" ]; then + TMP_DIR=$(mktemp -d) + cd "$TMP_DIR" + jar xf /rocketmq-dashboard.jar BOOT-INF/classes/static/env-config.js + cat > BOOT-INF/classes/static/env-config.js << EOF +window.__ENV__ = window.__ENV__ || {}; +window.__ENV__.API_BASE_URL = "$API_BASE_URL"; +EOF + jar uf /rocketmq-dashboard.jar BOOT-INF/classes/static/env-config.js + cd / + rm -rf "$TMP_DIR" +fi + +exec java $JAVA_OPTS -jar /rocketmq-dashboard.jar