Skip to content

Commit 4229627

Browse files
vsilentCopilot
andcommitted
web: default API/WS endpoints to APP_PORT or 5555
- Inject APP_PORT and REACT_APP_API_PORT into webpack env - API service default: http://localhost:${port}/api (port from env, fallback 5555) - WS service default: ws://localhost:${port}/ws (port from env, fallback 5555) Fixes frontend trying to connect to hardcoded port 5000. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 2cf69c4 commit 4229627

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

web/src/services/api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { SecurityStatus, Threat, ThreatStatistics } from '../types/security';
33
import { Alert, AlertStats, AlertFilter } from '../types/alerts';
44
import { Container, QuarantineRequest } from '../types/containers';
55

6-
const API_BASE_URL = process.env.REACT_APP_API_URL || 'http://localhost:5000/api';
6+
type EnvLike = {
7+
REACT_APP_API_URL?: string;
8+
APP_PORT?: string;
9+
REACT_APP_API_PORT?: string;
10+
};
11+
12+
const env = ((globalThis as unknown as { __STACKDOG_ENV__?: EnvLike }).__STACKDOG_ENV__ ??
13+
{}) as EnvLike;
14+
const apiPort = env.REACT_APP_API_PORT || env.APP_PORT || '5555';
15+
const API_BASE_URL = env.REACT_APP_API_URL || `http://localhost:${apiPort}/api`;
716

817
class ApiService {
918
public api: AxiosInstance;

web/src/services/websocket.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ type WebSocketEvent =
66
| 'stats:updated';
77

88
type EventHandler = (data: any) => void;
9+
type EnvLike = {
10+
REACT_APP_WS_URL?: string;
11+
APP_PORT?: string;
12+
REACT_APP_API_PORT?: string;
13+
};
14+
15+
declare global {
16+
interface Window {
17+
__STACKDOG_ENV__?: EnvLike;
18+
}
19+
}
920

1021
export class WebSocketService {
1122
private ws: WebSocket | null = null;
@@ -17,7 +28,10 @@ export class WebSocketService {
1728
private shouldReconnect = true;
1829

1930
constructor(url?: string) {
20-
this.url = url || process.env.REACT_APP_WS_URL || 'ws://localhost:5000/ws';
31+
const env = ((globalThis as { __STACKDOG_ENV__?: EnvLike }).__STACKDOG_ENV__ ??
32+
{}) as EnvLike;
33+
const apiPort = env.REACT_APP_API_PORT || env.APP_PORT || '5555';
34+
this.url = url || env.REACT_APP_WS_URL || `ws://localhost:${apiPort}/ws`;
2135
}
2236

2337
connect(): Promise<void> {

web/webpack.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const HtmlWebpackPlugin = require('html-webpack-plugin');
33
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
4+
const webpack = require('webpack');
45

56
module.exports = {
67
entry: './src/index.tsx',
@@ -27,6 +28,14 @@ module.exports = {
2728
},
2829
plugins: [
2930
new CleanWebpackPlugin(),
31+
new webpack.DefinePlugin({
32+
__STACKDOG_ENV__: JSON.stringify({
33+
REACT_APP_API_URL: process.env.REACT_APP_API_URL || '',
34+
REACT_APP_WS_URL: process.env.REACT_APP_WS_URL || '',
35+
APP_PORT: process.env.APP_PORT || '',
36+
REACT_APP_API_PORT: process.env.REACT_APP_API_PORT || '',
37+
}),
38+
}),
3039
new HtmlWebpackPlugin({
3140
templateContent:
3241
'<!doctype html><html><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1"/><title>Stackdog</title></head><body><div id="root"></div></body></html>',

0 commit comments

Comments
 (0)