Skip to content

Commit 7424cbf

Browse files
Merge pull request #5 from dlsrks1021/main
chore: 런타임에 호출 URL을 환경변수로 설정 가능하도록 수정 good idea!
2 parents 8ce3bc8 + 59f1915 commit 7424cbf

File tree

6 files changed

+13
-4
lines changed

6 files changed

+13
-4
lines changed

public/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
77
<meta name="viewport" content="width=device-width, initial-scale=1" />
88
<meta name="theme-color" content="#000000" />
9+
<script src="/env.js"></script>
910
<!--
1011
Notice the use of %PUBLIC_URL% in the tags above.
1112
It will be replaced with the URL of the `public` folder during the build.

server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ const path = require('path');
44
const app = express();
55
const PORT = process.env.PORT || 3000;
66

7+
const env = {
8+
REACT_APP_API_BASE_URL: process.env.REACT_APP_API_BASE_URL || "https://api-brainrace.duckdns.org",
9+
REACT_APP_WS_BASE_URL: process.env.REACT_APP_WS_BASE_URL || "wss://api-brainrace.duckdns.org",
10+
};
11+
const fs = require('fs');
12+
const envFileContent = `window.__ENV__ = ${JSON.stringify(env)};`;
13+
fs.writeFileSync(path.join(__dirname, 'build', 'env.js'), envFileContent);
14+
715
// 정적 파일 제공 (build 디렉토리)
816
app.use(express.static(path.join(__dirname, 'build')));
917

src/hooks/useRoomSseClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function useRoomSseClient(onRoomEvent) {
1414
useEffect(() => {
1515
if (eventSource) return;
1616

17-
eventSource = new EventSource(`${process.env.REACT_APP_API_BASE_URL}/sse/lobby`, {withCredentials: true});
17+
eventSource = new EventSource(`${window.__ENV__?.REACT_APP_API_BASE_URL || "https://api-brainrace.duckdns.org"}/sse/lobby`, {withCredentials: true});
1818

1919
eventSource.onopen = () => {
2020
console.log('✅ SSE 연결됨');

src/hooks/useStompClient.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default function useStompClient(roomId, onMessage) {
2525
isConnecting = true;
2626

2727
stompClient = new Client({
28-
brokerURL: `${process.env.REACT_APP_WS_BASE_URL}/ws/game-room`,
28+
brokerURL: `${window.__ENV__?.REACT_APP_WS_BASE_URL || "wss://api-brainrace.duckdns.org"}/ws/game-room`,
2929
reconnectDelay: 5000,
3030
// debug: (msg) => console.log('[STOMP]', msg),
3131
onConnect: () => {

src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {RecoilRoot} from "recoil";
77
import {QueryClient, QueryClientProvider} from "react-query";
88
import axios from "axios";
99

10-
axios.defaults.baseURL = process.env.REACT_APP_API_BASE_URL || "https://api-brainrace.duckdns.org";
10+
axios.defaults.baseURL = window.__ENV__?.REACT_APP_API_BASE_URL || "https://api-brainrace.duckdns.org";
1111
// react-qeury사용을 위해 선언
1212
const queryClient = new QueryClient({
1313
defaultOptions: {

src/pages/login/Login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const Login = () => {
1111
</div>
1212
<button className={styles.loginButton}
1313
onClick={() => {
14-
window.location.href = `${process.env.REACT_APP_API_BASE_URL}/oauth2/authorization/kakao`;
14+
window.location.href = `${window.__ENV__?.REACT_APP_API_BASE_URL || "https://api-brainrace.duckdns.org"}/oauth2/authorization/kakao`;
1515
}}>
1616
뇌이싱 입장하기
1717
</button>

0 commit comments

Comments
 (0)