2022-10-12 14:21:49 +00:00
#!/usr/bin/env bash
function error_handler() {
2024-04-15 18:22:34 +00:00
echo >&2 "Exited with BAD EXIT CODE '${2}' in ${0} script at line: ${1}."
2022-10-12 14:21:49 +00:00
exit "$2"
}
trap 'error_handler ${LINENO} $?' ERR
set -o errtrace -o errexit -o nounset -o pipefail
2023-02-07 22:21:54 +00:00
# sort of like https://lithic.tech/blog/2020-05/react-dynamic-config, but without golang
2023-02-08 03:06:47 +00:00
react_configs=$(env | grep -E "^SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_" || echo '')
2023-02-07 22:21:54 +00:00
if [[ -n "$react_configs" ]]; then
2024-04-15 18:22:34 +00:00
index_html_file="/usr/share/nginx/html/index.html"
2023-02-07 22:21:54 +00:00
if [[ ! -f "$index_html_file" ]]; then
2024-04-15 18:22:34 +00:00
echo >&2 "ERROR: Could not find '${index_html_file}'. Cannot use SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG values without it."
2023-02-07 22:21:54 +00:00
exit 1
fi
2024-04-15 18:22:34 +00:00
if ! command -v sed >/dev/null; then
echo >&2 "ERROR: sed command not found. Cannot use SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG values without it."
2023-02-07 22:21:54 +00:00
exit 1
fi
2024-04-15 18:22:34 +00:00
if ! command -v perl >/dev/null; then
echo >&2 "ERROR: perl command not found. Cannot use SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG values without it."
2023-02-07 22:21:54 +00:00
exit 1
fi
for react_config in $react_configs; do
react_config_without_prefix=$(sed -E 's/^SPIFFWORKFLOW_FRONTEND_RUNTIME_CONFIG_([^=]*)=(.*)/\1=\\"\2\\"/' <<<"${react_config}")
2023-02-08 13:17:02 +00:00
if [[ -z "$react_config_without_prefix" ]]; then
2024-04-15 18:22:34 +00:00
echo >&2 "ERROR: Could not parse react config line: '${react_config}'."
2023-02-08 13:17:02 +00:00
exit 1
fi
2023-04-26 17:47:27 +00:00
escaped_react_config=$(sed -E 's|/|\\/|g' <<<"${react_config_without_prefix}")
2023-02-08 13:17:02 +00:00
# actually do the search and replace to add the js config to the html page
2024-04-15 18:22:34 +00:00
perl -pi -e "s/(window.spiffworkflowFrontendJsenv *= *\{\})/\1;window.spiffworkflowFrontendJsenv.${escaped_react_config}/" "$index_html_file"
2023-02-08 13:17:02 +00:00
if ! grep -Eq "${react_config_without_prefix}" "$index_html_file"; then
2024-04-15 18:22:34 +00:00
echo >&2 "ERROR: Could not find '${react_config_without_prefix}' in '${index_html_file}' after search and replace. It is likely that the assumptions in boot_server_in_docker about the contents of the html page have changed. Fix the glitch in boot_server_in_docker."
2023-02-08 13:17:02 +00:00
exit 1
fi
2023-02-07 22:21:54 +00:00
done
fi
2024-04-15 18:22:34 +00:00
port_to_use="${PORT0:-80}"
if [[ -n "${SPIFFWORKFLOW_FRONTEND_INTERNAL_PORT:-}" ]]; then
port_to_use="$SPIFFWORKFLOW_FRONTEND_INTERNAL_PORT"
fi
perl -p -e "s/{{SPIFFWORKFLOW_FRONTEND_INTERNAL_PORT}}/${port_to_use}/" /var/tmp/nginx.conf.template >/etc/nginx/conf.d/default.conf
exec nginx -g "daemon off;"