This commit is contained in:
ondal 2025-02-15 15:06:01 +09:00
parent 22981e4535
commit 455af41d0d
4 changed files with 25 additions and 8 deletions

View File

@ -1,9 +1,6 @@
# Build stage
FROM node:20-slim AS builder
ARG PROJECT_FOLDER
ARG REACT_APP_MEMBER_URL
ARG REACT_APP_MYSUB_URL
ARG REACT_APP_RECOMMEND_URL
ENV NODE_ENV=production
@ -20,13 +17,11 @@ RUN npm run build
# Run stage
FROM nginx:stable-alpine
# 시스템 환경변수 셋팅: api.js에서 사용하는 시스템 환경변수를 build시 셋팅해야 생성됨
ENV REACT_APP_MEMBER_URL=${REACT_APP_MEMBER_URL}
ENV REACT_APP_MYSUB_URL=${REACT_APP_MYSUB_URL}
ENV REACT_APP_RECOMMEND_URL=${REACT_APP_RECOMMEND_URL}
ARG BUILD_FOLDER
ARG EXPORT_PORT
ARG REACT_APP_MEMBER_URL
ARG REACT_APP_MYSUB_URL
ARG REACT_APP_RECOMMEND_URL
# Create nginx user if it doesn't exist
RUN adduser -S nginx || true
@ -34,6 +29,17 @@ RUN adduser -S nginx || true
# Copy build files
COPY --from=builder /app/build /usr/share/nginx/html
# Create runtime config
/*
index.html의 헤더에서 이 값을 읽어 환경변수를 생성함
api.js에서 이 환경변수를 이용함: 예) window.__runtime_config__.MEMBER_URL
*/
RUN echo "window.__runtime_config__ = { \
MEMBER_URL: '${REACT_APP_MEMBER_URL}', \
MYSUB_URL: '${REACT_APP_MYSUB_URL}', \
RECOMMEND_URL: '${REACT_APP_RECOMMEND_URL}' \
}" > /usr/share/nginx/html/runtime-env.js
# Copy and process nginx configuration
COPY ${BUILD_FOLDER}/nginx.conf /etc/nginx/templates/default.conf.template

View File

@ -9,6 +9,7 @@
<meta name="description" content="생활 구독 관리 서비스" />
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<script src="%PUBLIC_URL%/runtime-env.js"></script>
<title>마이구독</title>
</head>
<body>

5
public/runtime-env.js Normal file
View File

@ -0,0 +1,5 @@
window.__runtime_config__ = {
MEMBER_URL: 'http://localhost:8081',
MYSUB_URL: 'http://localhost:8082',
RECOMMEND_URL: 'http://localhost:8083'
};

View File

@ -2,9 +2,14 @@
import axios from 'axios';
// 서비스별 base URL
/*
const MEMBER_URL = process.env.REACT_APP_MEMBER_URL || 'http://localhost:8081';
const MYSUB_URL = process.env.REACT_APP_MYSUB_URL || 'http://localhost:8082';
const RECOMMEND_URL = process.env.REACT_APP_RECOMMEND_URL || 'http://localhost:8083';
*/
const MEMBER_URL = window.__runtime_config__.MEMBER_URL || 'http://localhost:8081';
const MYSUB_URL = window.__runtime_config__.MYSUB_URL || 'http://localhost:8082';
const RECOMMEND_URL = window.__runtime_config__.RECOMMEND_URL || 'http://localhost:8083';
// 서비스별 axios 인스턴스 생성
const createAxiosInstance = (baseURL) => {