Added proxy headers flag to FastAPI launch logic

[docker] add new BEHIND_PROXY env variable with false as default value
[docker] added new script start.sh with frontend.sh logic plus logic to add proxy headers flag if BEHIND_PROXY is true
[docker] removed frontend.sh
[docker] created new folder docker in root and moved docker related files to it
This commit is contained in:
João Vitória Silva
2024-12-23 22:30:56 +00:00
parent ea2acea074
commit 0e43bf1a09
2 changed files with 27 additions and 10 deletions

View File

@@ -52,7 +52,8 @@ ENV TZ="UTC" \
JAEGER_PROTOCOL="http" \
JAGGER_PORT=4317 \
ENDURAIN_HOST="http://localhost:8080" \
GEOCODES_MAPS_API="changeme"
GEOCODES_MAPS_API="changeme" \
BEHIND_PROXY=false
# Set the working directory to /app/backend
WORKDIR /app/backend
@@ -71,13 +72,12 @@ COPY backend/app ./
# Copy the directory app contents to /app
COPY --from=frontend-build /tmp/frontend/dist ./frontend/dist
COPY frontend/app/.env ./frontend/.env
# Copy the entrypoint script
COPY frontend_env.sh /docker-entrypoint.d/frontend_env.sh
# Copy the entrypoint script for starting the FastAPI app
COPY docker/start.sh /docker-entrypoint.d/start.sh
# Make the entrypoint script executable
RUN chmod +x /docker-entrypoint.d/frontend_env.sh
# Make the script executable
RUN chmod +x /docker-entrypoint.d/start.sh
# Change ownership to non-root user
RUN chown -R endurain:endurain ./
@@ -88,5 +88,5 @@ USER endurain
# Make port 80 available to the world outside this container
EXPOSE 80
# Run FastAPI
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "80"]
# Run the FastAPI app
ENTRYPOINT ["/docker-entrypoint.d/start.sh"]

View File

@@ -1,11 +1,28 @@
#!/bin/sh
#!/bin/bash
set -e
# Substitute MY_APP_ENDURAIN_HOST with the value of ENDURAIN_HOST
if [ ! -z "$ENDURAIN_HOST" ]; then
echo "Substituting MY_APP_ENDURAIN_HOST with $ENDURAIN_HOST"
find /app/backend/frontend/dist -type f \( -name '*.js' -o -name '*.css' \) -exec sed -i "s|MY_APP_ENDURAIN_HOST|${ENDURAIN_HOST}|g" '{}' +
fi
# Substitute MY_APP_STRAVA_CLIENT_ID with the value of STRAVA_CLIENT_ID
if [ ! -z "$STRAVA_CLIENT_ID" ]; then
echo "Substituting MY_APP_STRAVA_CLIENT_ID with $STRAVA_CLIENT_ID"
find /app/backend/frontend/dist -type f \( -name '*.js' -o -name '*.css' \) -exec sed -i "s|MY_APP_STRAVA_CLIENT_ID|${STRAVA_CLIENT_ID}|g" '{}' +
fi
fi
echo "Starting FastAPI with BEHIND_PROXY=$BEHIND_PROXY"
# Base command as an array
CMD=("uvicorn" "main:app" "--host" "0.0.0.0" "--port" "80")
# Add --proxy-headers if BEHIND_PROXY is true
if [ "$BEHIND_PROXY" = "true" ]; then
echo "Enabling proxy headers"
CMD+=("--proxy-headers")
fi
# Execute the command
exec "${CMD[@]}"