Skip to content

Commit 8d506fc

Browse files
authored
Merge pull request #338 from dsgnr/default_host
Allow default host to be defined
2 parents d4b984b + 8e84bf0 commit 8d506fc

File tree

10 files changed

+29
-6
lines changed

10 files changed

+29
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The following configuration options are available. These would be set within the
8787
| Name | Required? | Default | Notes |
8888
| ---------------- | --------- | --------------- | ---------------------------------------------------------------------------------------- |
8989
| API_URL | No | http://api:8000 | The URL of the API service if changed from the default. The scheme and port is required. |
90+
| DEFAULT_HOST | No | | Allows a default host address value to be prefille in the in the UI. Defaults to external/WAN IP if not set |
9091
| DEFAULT_PORT | No | 443 | Allows a default port number to be prefilled in the UI |
9192
| GOOGLE_ANALYTICS | No | | Allows for a Google Analytics tracking code to be provided |
9293

docker-compose-dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ services:
66
dockerfile: Dockerfile.dev
77
container_name: web
88
environment:
9+
- DEFAULT_HOST= # Optional, Populates a default host address value to be populataed in the in the UI input. Defaults to external/WAN IP.
910
- DEFAULT_PORT=443 # Optional, Populates a default port value to be populataed in the in the UI input
1011
- API_URL= # Optional, the URL of the API service. The scheme and port is required. Defaults to http://api:8000 if not set.
1112
- GOOGLE_ANALYTICS= # Optional, set for Google Analytics integration

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ services:
44
image: ghcr.io/dsgnr/portcheckerio-web:latest
55
container_name: web
66
environment:
7+
- DEFAULT_HOST= # Optional, Populates a default host address value to be populataed in the in the UI input. Defaults to external/WAN IP.
78
- DEFAULT_PORT=443 # Optional, Populates a default port value to be populataed in the in the UI input
89
- API_URL= # Optional, the URL of the API service. The scheme and port is required. Defaults to http://api:8000 if not set.
910
- GOOGLE_ANALYTICS= # Optional, set for Google Analytics integration

frontend/Dockerfile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
FROM node:23-alpine3.19 AS builder
2-
ARG API_URL DEFAULT_PORT GOOGLE_ANALYTICS
2+
ARG API_URL DEFAULT_HOST DEFAULT_PORT GOOGLE_ANALYTICS
33
ENV API_URL=$API_URL
4+
ENV DEFAULT_HOST=$DEFAULT_HOST
45
ENV DEFAULT_PORT=$DEFAULT_PORT
56
ENV GOOGLE_ANALYTICS=$GOOGLE_ANALYTICS
67

@@ -10,8 +11,9 @@ RUN yarn install && yarn build
1011

1112
# production environment
1213
FROM nginx:stable-alpine-slim AS web
13-
ARG API_URL DEFAULT_PORT
14+
ARG API_URL DEFAULT_HOST DEFAULT_PORT
1415
ENV API_URL=$API_URL
16+
ENV DEFAULT_HOST=$DEFAULT_HOST
1517
ENV DEFAULT_PORT=$DEFAULT_PORT
1618
COPY --from=builder /app/dist /usr/share/nginx/html
1719
COPY ./entrypoint.sh /entrypoint.sh

frontend/Dockerfile.dev

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ ENV PATH=$PATH:$PWD/node_modules/.bin
33

44
# ALlows the user to define a default port to populate the UI.
55
# Pass a desired value in your docker run/compose environment
6-
ARG API_URL DEFAULT_PORT
6+
ARG API_URL DEFAULT_HOST DEFAULT_PORT
77
ENV API_URL=$API_URL
8+
ENV DEFAULT_HOST=$DEFAULT_HOST
89
ENV DEFAULT_PORT=$DEFAULT_PORT
910

1011
COPY web/package.json web/yarn.lock ./

frontend/entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ set -e
55
: ${API_URL:="http://api:8000"}
66
export API_URL
77

8+
# `DEFAULT_HOST` is set by Webpack at container build time if the environment variable is provided.
9+
# If this variable is not set at that time (like for production images), we must modify the rendered HTML on container up.
10+
if [ -n "$DEFAULT_HOST" ]; then
11+
if sed -i -E "s/(<input[^>]*id=\"host\"[^>]*value=\")[^\"]*\"/\\1${DEFAULT_HOST}\"/" /usr/share/nginx/html/index.html; then
12+
echo "Updated DEFAULT_HOST value to $DEFAULT_HOST."
13+
else
14+
echo "An error occurred when attempting to set the DEFAULT_HOST value."
15+
fi
16+
else
17+
echo "DEFAULT_HOST is not set. No changes made."
18+
fi
19+
820
# `DEFAULT_PORT` is set by Webpack at container build time if the environment variable is provided.
921
# If this variable is not set at that time (like for production images), we must modify the rendered HTML on container up.
1022
if [ -n "$DEFAULT_PORT" ]; then

frontend/web/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"email": "[email protected]"
1919
},
2020
"scripts": {
21-
"dev": "GOOGLE_ANALYTICS=${GOOGLE_ANALYTICS} API_URL=${API_URL:-http://api:8000} DEFAULT_PORT=${DEFAULT_PORT} NODE_ENV=development webpack serve --history-api-fallback",
22-
"build": "GOOGLE_ANALYTICS=${GOOGLE_ANALYTICS} DEFAULT_PORT=${DEFAULT_PORT} NODE_ENV=production webpack build",
21+
"dev": "GOOGLE_ANALYTICS=${GOOGLE_ANALYTICS} API_URL=${API_URL:-http://api:8000} DEFAULT_HOST=${DEFAULT_HOST} DEFAULT_PORT=${DEFAULT_PORT} NODE_ENV=development webpack serve --history-api-fallback",
22+
"build": "GOOGLE_ANALYTICS=${GOOGLE_ANALYTICS} DEFAULT_HOST=${DEFAULT_HOST} DEFAULT_PORT=${DEFAULT_PORT} NODE_ENV=production webpack build",
2323
"lint": "npx eslint src",
2424
"lint:fix": "npm run lint -- --fix",
2525
"prettier": "npx prettier src --check",

frontend/web/src/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
class="form-control py-2"
5050
id="host"
5151
placeholder="Host"
52+
value="<%= htmlWebpackPlugin.options.DEFAULT_HOST %>"
5253
pattern="^([\w-]+(\.[\w-]+)+)$"
5354
required
5455
/>

frontend/web/src/js/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
window.onload = () => {
77
initializeState();
8-
loadUserIp();
8+
9+
if (!document.getElementById("host").value) {
10+
loadUserIp();
11+
}
912

1013
const form = document.getElementById("form");
1114
form.addEventListener("submit", (event) => {

frontend/web/webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = {
2323
minify: {
2424
collapseWhitespace: true,
2525
},
26+
DEFAULT_HOST: process.env.DEFAULT_HOST,
2627
DEFAULT_PORT: process.env.DEFAULT_PORT,
2728
GOOGLE_ANALYTICS: process.env.GOOGLE_ANALYTICS,
2829
}),

0 commit comments

Comments
 (0)