diff --git a/frontend/Dockerfile b/frontend/Dockerfile index e38281d..a596499 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -15,5 +15,11 @@ WORKDIR /usr/share/nginx/html COPY nginx.conf /etc/nginx/conf.d/default.conf COPY --from=build /app/dist ./ +ENV API_BASE_URL="" + +COPY docker-entrypoint.sh /docker-entrypoint.sh +RUN chmod +x /docker-entrypoint.sh + EXPOSE 80 +ENTRYPOINT ["/docker-entrypoint.sh"] CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/docker-entrypoint.sh b/frontend/docker-entrypoint.sh new file mode 100644 index 0000000..2d86f2b --- /dev/null +++ b/frontend/docker-entrypoint.sh @@ -0,0 +1,11 @@ +#!/bin/sh +set -euo pipefail + +API_BASE_URL_TRIMMED="${API_BASE_URL:-}" +API_BASE_URL_TRIMMED="${API_BASE_URL_TRIMMED%%/}" + +cat < /usr/share/nginx/html/config.js +window.__PAPERCRATE_API_BASE_URL = "${API_BASE_URL_TRIMMED}"; +CONFIG + +exec "$@" diff --git a/frontend/src/index.html b/frontend/src/index.html index d5cc24a..1a26e61 100644 --- a/frontend/src/index.html +++ b/frontend/src/index.html @@ -6,6 +6,7 @@ Papercrate +
diff --git a/frontend/src/index.jsx b/frontend/src/index.jsx index 2363e34..657f33e 100644 --- a/frontend/src/index.jsx +++ b/frontend/src/index.jsx @@ -22,7 +22,12 @@ import { } from 'react-router-dom'; import './styles.css'; -const API_ROOT = (process.env.API_BASE_URL || '').replace(/\/$/, ''); +const runtimeApiBase = + typeof window !== 'undefined' && window.__PAPERCRATE_API_BASE_URL + ? window.__PAPERCRATE_API_BASE_URL + : ''; + +const API_ROOT = (runtimeApiBase || process.env.API_BASE_URL || '').replace(/\/$/, ''); const api = axios.create({ baseURL: API_ROOT ? `${API_ROOT}/api` : '/api', diff --git a/frontend/webpack.config.js b/frontend/webpack.config.js index a119093..69d0143 100644 --- a/frontend/webpack.config.js +++ b/frontend/webpack.config.js @@ -5,8 +5,10 @@ const dotenv = require('dotenv'); const env = dotenv.config({ path: path.resolve(__dirname, '.env.local') }).parsed || {}; -const API_BASE_URL = - env.API_BASE_URL || process.env.API_BASE_URL || 'http://127.0.0.1:3000'; +const DEFAULT_DEV_API = 'http://127.0.0.1:3000'; + +const API_BASE_URL = env.API_BASE_URL || process.env.API_BASE_URL || ''; +const devServerApiTarget = API_BASE_URL || DEFAULT_DEV_API; module.exports = { entry: './src/index.jsx', @@ -50,7 +52,7 @@ module.exports = { proxy: [ { context: ['/api'], - target: API_BASE_URL, + target: devServerApiTarget, changeOrigin: true, }, ],