Skip to content

Commit

Permalink
Runtime config in Docker (#349)
Browse files Browse the repository at this point in the history
Co-authored-by: Matthias Mohr <[email protected]>
  • Loading branch information
constantinius and m-mohr authored Sep 6, 2023
1 parent f074ae1 commit bf87b3f
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 6 deletions.
18 changes: 12 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
FROM node:lts-alpine3.18 AS build-step

ARG catalogURL
ARG DYNAMIC_CONFIG=true

WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build -- --catalogUrl=$catalogURL
RUN \[ "${DYNAMIC_CONFIG}" == "true" \] && sed -i 's/<!-- <script defer="defer" src=".\/config.js"><\/script> -->/<script defer="defer" src=".\/config.js"><\/script>/g' public/index.html
RUN npm run build


FROM nginx:1-alpine-slim

FROM nginx:1-alpine
COPY --from=build-step /app/dist /usr/share/nginx/html
COPY ./config.schema.json /etc/nginx/conf.d/config.schema.json

# change default port to 8080
RUN sed -i 's/\s*listen\s*80;/ listen 8080;/' /etc/nginx/conf.d/default.conf
RUN apk add jq pcre-tools && \
sed -i 's/\s*listen\s*80;/ listen 8080;/' /etc/nginx/conf.d/default.conf && \
sed -i 's/\s*location \/ {/ location \/ {\n try_files $uri $uri\/ \/index.html;/' /etc/nginx/conf.d/default.conf

EXPOSE 8080

STOPSIGNAL SIGTERM

CMD ["nginx", "-g", "daemon off;"]
# override entrypoint, which calls nginx-entrypoint underneath
ADD docker/docker-entrypoint.sh /docker-entrypoint.d/40-stac-browser-entrypoint.sh
91 changes: 91 additions & 0 deletions docker/docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# echo a string, handling different types
safe_echo() {
local value="$1"
if [ -z "$value" ]; then
echo -n "null"
elif printf '%s\n' "$value" | grep -qE '\n.+\n$'; then
echo -n "\`$value\`"
else
echo -n "'$value'"
fi
}

# handle boolean
bool() {
case "$1" in
true | TRUE | yes | t | True)
echo -n true ;;
false | FALSE | no | n | False)
echo -n false ;;
*)
echo "Err: Unknown boolean value \"$1\"" >&2
exit 1 ;;
esac
}

# handle array values
array() {
local value="$1"
local arraytype="$2"
if [ -z "$value" ]; then
echo -n "[]"
else
case "$arraytype" in
string)
echo -n "['$(echo "$value" | sed "s/,/', '/g")']"
;;
*)
echo -n "[$value]"
;;
esac
fi
}

# handle object values
object() {
local value="$1"
if [ -z "$value" ]; then
echo -n "null"
else
echo -n "$value"
fi
}

config_schema=$(cat /etc/nginx/conf.d/config.schema.json)

# Iterate over environment variables with "SB_" prefix
env -0 | cut -f1 -d= | tr '\0' '\n' | grep "^SB_" | {
echo "window.STAC_BROWSER_CONFIG = {"
while IFS='=' read -r name; do
# Strip the prefix
argname="${name#SB_}"
# Read the variable's value
value="$(eval "echo \"\$$name\"")"

# Get the argument type from the schema
argtype="$(echo "$config_schema" | jq -r ".properties.$argname.type[0]")"
arraytype="$(echo "$config_schema" | jq -r ".properties.$argname.items.type[0]")"

# Encode key/value
echo -n " $argname: "
case "$argtype" in
string)
safe_echo "$value"
;;
boolean)
bool "$value"
;;
integer | number)
object "$value"
;;
array)
array "$value" "$arraytype"
;;
*)
safe_echo "$value"
;;
esac
echo ","
done
echo "}"
} > /usr/share/nginx/html/config.js

0 comments on commit bf87b3f

Please sign in to comment.