Skip to content

Commit

Permalink
Merge branch 'v1.1.0' into bugfix/605-solr-port
Browse files Browse the repository at this point in the history
  • Loading branch information
sashakames committed Mar 12, 2024
2 parents b5fd337 + b5dced7 commit 5f7c44b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/frontend.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,14 @@ jobs:
run: yarn install --frozen-lockfile

- name: Run Tests
env:
RELEASE: dev
ENV_FILE: .envs/.react
HTML_PATH: public/
run: |
# Replaces react-scripts substitution during build for index.html and generates runtime_env.js
docker/production/react/entrypoint
yarn test:coverage
- name: Upload Coverage Report
Expand Down
8 changes: 4 additions & 4 deletions backend/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,11 @@
# django-cors-headers
# -------------------------------------------------------------------------------
# https://github.com/adamchainz/django-cors-headers#setup
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
]
CORS_ALLOWED_ORIGINS = env.list(
"CORS_ALLOWED_ORIGINS", default=["http://localhost:5000"]
)
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_WHITELIST = env.list("CORS_ORIGIN_WHITELIST")
CORS_ORIGIN_WHITELIST = env.list("CORS_ORIGIN_WHITELIST", default=[])

SEARCH_URL = env("REACT_APP_SEARCH_URL")
WGET_URL = env("REACT_APP_WGET_API_URL")
Expand Down
10 changes: 7 additions & 3 deletions frontend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
build:
docker build $(ARGS) -t react:latest -f docker/production/react/Dockerfile .

.PHONY: build-local
build-local:
docker build $(ARGS) -t react:latest -f docker/local/Dockerfile .

.PHONY: run
run:
docker run $(ARGS) -it --rm -p 3000:3000 react:latest

.PHONY: run-shell
run-shell:
docker run $(ARGS) -it --rm --entrypoint /bin/sh react:latest
.PHONY: shell
shell:
docker run $(ARGS) -it --rm -p 3000:3000 --entrypoint /bin/sh react:latest
5 changes: 5 additions & 0 deletions frontend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ services:
dockerfile: ./docker/local/Dockerfile
image: metagrid_local_react
container_name: react
environment:
- DEBUG=true
- RELEASE=dev
- HTML_PATH=/app/public
- ENV_FILE=/app/.envs/.react
env_file:
- .envs/.react
volumes:
Expand Down
11 changes: 11 additions & 0 deletions frontend/docker/local/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,16 @@ RUN yarn install
# Add app
COPY . ./

# Determines which conf to use if app is/is not being served through subdirectory
COPY ./docker/production/react/entrypoint /entrypoint
# gettext-base is required for envsubst
RUN sed -i 's/\r$//g' /entrypoint && \
chmod +x /entrypoint && \
apt-get update && \
apt-get install -y gettext-base && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
ENTRYPOINT ["/entrypoint"]

# Start app
CMD ["yarn", "start:local"]
74 changes: 48 additions & 26 deletions frontend/docker/production/react/entrypoint
Original file line number Diff line number Diff line change
@@ -1,44 +1,66 @@
#!/bin/sh

[[ -n "${DEBUG}" ]] && set -x
[ -n "${DEBUG}" ] && set -x

runtime_path="/usr/share/nginx/html/static/js/runtime_env.js"
export RELEASE="${RELEASE:-production}"
export ENV_FILE="${ENV_FILE:-/env}"
export HTML_PATH="${HTML_PATH:-/usr/share/nginx/html}"
export STATIC_PATH=""
export STATIC_URL="${PUBLIC_URL}${STATIC_PATH}"
export RUNTIME_PATH="${HTML_PATH}${STATIC_PATH}/runtime_env.js"

rm -rf "${runtime_path}" && touch "${runtime_path}"
TMP_PATH=/tmp

echo "window.ENV = {" >> "${runtime_path}"
if [ ! -e "$(dirname ${RUNTIME_PATH})" ]
then
mkdir -p "$(dirname ${RUNTIME_PATH})"
fi

while read -r line; do
[[ -z "${line}" || "${line}" =~ ^# ]] && continue
if [ "${RELEASE}" = "production" ]
then
export STATIC_PATH="/static/js"
fi

varname=$(printf '%s\n' "${line}" | cut -d"=" -f1)
varvalue=$(printenv "${varname}")
if [ -e "${ENV_FILE}" ]; then
echo "window.ENV = {" > "${RUNTIME_PATH}"

if [[ -z "${varvalue}" ]]; then
varvalue=$(printf '%s\n' "${line}" | cut -d"=" -f2)
while read -r line; do
[ -z "$(echo ${line} | grep -vE '^# |^$')" ] && continue

export "${varname}"="${varvalue}"
fi
varname=$(printf '%s\n' "${line}" | cut -d"=" -f1)
varvalue=$(printenv "${varname}")

echo " ${varname}: \"${varvalue}\"," >> "${runtime_path}"
done < ${ENV_FILE:-/env}
if [ -z "${varvalue}" ]; then
varvalue=$(printf '%s\n' "${line}" | cut -d"=" -f2)

echo "};" >> "${runtime_path}"
export "${varname}"="${varvalue}"
fi

if [[ -z "${PUBLIC_URL}" ]]
then
envsubst '${PREVIOUS_URL}' < /nginx.conf > /etc/nginx/conf.d/default.conf
export PUBLIC_URL=""
else
envsubst '${PREVIOUS_URL},${PUBLIC_URL}' < /nginx.subdir.conf > /etc/nginx/conf.d/default.conf
echo " ${varname}: \"${varvalue}\"," >> "${RUNTIME_PATH}"
done < ${ENV_FILE}

echo "};" >> "${RUNTIME_PATH}"
fi

# prepend any "/static/... with "${PUBLIC_URL}/static/...
sed -i"" "s/\"\/static/\"\${PUBLIC_URL}\/static/g" /usr/share/nginx/html/index.html
if [ ! -e "/index.html" ]; then
cp "${HTML_PATH}/index.html" "${TMP_PATH}/index.html"
fi

if [ "${RELEASE}" = "production" ]
then
if [ -z "${PUBLIC_URL}" ]
then
envsubst '${PREVIOUS_URL}' < /nginx.conf > /etc/nginx/conf.d/default.conf
export PUBLIC_URL=""
else
envsubst '${PREVIOUS_URL},${PUBLIC_URL}' < /nginx.subdir.conf > /etc/nginx/conf.d/default.conf

# move original index.html, envsubst cannot modify in place
mv /usr/share/nginx/html/index.html /index.html
# Fixes react-scripts static path e.g. /static/ -> $PUBLIC_URL/static/
sed -i"" "s/\"\/static\//\"\$PUBLIC_URL\/static\//g" "${TMP_PATH}/index.html"
fi

fi

envsubst '${PUBLIC_URL},${$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID}' < /index.html > /usr/share/nginx/html/index.html
envsubst '$STATIC_URL,$PUBLIC_URL,$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID' < "${TMP_PATH}/index.html" > "${HTML_PATH}/index.html"

exec "$@"
2 changes: 1 addition & 1 deletion frontend/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
-->
<title>ESGF MetaGrid</title>

<script type="application/javascript" src="$PUBLIC_URL/static/js/runtime_env.js"></script>
<script type="application/javascript" src="$STATIC_URL/runtime_env.js"></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=$REACT_APP_GOOGLE_ANALYTICS_TRACKING_ID"></script>
<script>
Expand Down

0 comments on commit 5f7c44b

Please sign in to comment.