Skip to content

Commit 7aaa764

Browse files
committed
fix: Add environment-aware API URL configuration for Codespaces
- Created getApiUrl() utility that auto-detects Codespace environment - Replaces port 3000 with port 8000 for backend API when in Codespaces - Falls back to localhost:8000 for local development - Fixed hardcoded localhost URLs in all components: - api.ts (axios instance) - ComplianceDashboard.tsx - PolicyList.tsx - PolicyViolations.tsx - Added .env.example for configuration reference - Fixes API connectivity issues where frontend couldn't reach backend
1 parent 5e82ea7 commit 7aaa764

File tree

12 files changed

+50
-7
lines changed

12 files changed

+50
-7
lines changed

AIGovHub/frontend/.env.example

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# API Configuration
2+
# For Codespaces, the backend URL will be auto-detected from window.location
3+
# For local development, use localhost
4+
VITE_API_BASE_URL=http://localhost:8000

AIGovHub/frontend/src/api.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,27 @@ import type {
66
ComplianceLog,
77
} from "./types";
88

9+
// Detect if running in GitHub Codespaces and construct the API URL accordingly
10+
const getApiBaseUrl = () => {
11+
// Check if we're in a Codespace (look for the Codespace-specific URL pattern)
12+
if (
13+
typeof window !== "undefined" &&
14+
window.location.hostname.includes(".app.github.dev")
15+
) {
16+
// Replace port 3000 with 8000 for backend API
17+
const backendUrl = window.location.origin.replace(
18+
"-3000.app.github.dev",
19+
"-8000.app.github.dev",
20+
);
21+
return `${backendUrl}/api/v1`;
22+
}
23+
24+
// Fall back to environment variable or localhost for local development
25+
return `${import.meta.env.VITE_API_BASE_URL || "http://localhost:8000"}/api/v1`;
26+
};
27+
928
const api = axios.create({
10-
baseURL: "http://localhost:8000/api/v1",
29+
baseURL: getApiBaseUrl(),
1130
});
1231

1332
export const getModels = async () => {

AIGovHub/frontend/src/components/ComplianceDashboard.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from "@/components/ui/card";
99
import { Badge } from "@/components/ui/badge";
1010
import type { DashboardStats } from "../types";
11+
import { getApiUrl } from "../lib/apiUrl";
1112
import {
1213
BarChart,
1314
Bar,
@@ -43,7 +44,7 @@ export function ComplianceDashboard() {
4344
const loadStats = async () => {
4445
setLoading(true);
4546
try {
46-
const res = await fetch("http://localhost:8000/api/v1/dashboard/stats");
47+
const res = await fetch(getApiUrl("/dashboard/stats"));
4748
const data = await res.json();
4849
setStats(data);
4950
} catch (error) {

AIGovHub/frontend/src/components/PolicyList.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from "@mantine/core";
1616
import { useDisclosure } from "@mantine/hooks";
1717
import type { Policy, PolicyScope, PolicyConditionType } from "../types";
18+
import { getApiUrl } from "../lib/apiUrl";
1819

1920
const SCOPE_COLORS: Record<string, string> = {
2021
global: "blue",
@@ -43,7 +44,7 @@ export function PolicyList() {
4344
let cancelled = false;
4445
(async () => {
4546
try {
46-
const res = await fetch("http://localhost:8000/api/v1/policies/");
47+
const res = await fetch(getApiUrl("/policies/"));
4748
const data = await res.json();
4849
if (!cancelled) setPolicies(data);
4950
} catch {
@@ -57,7 +58,7 @@ export function PolicyList() {
5758

5859
const handleCreatePolicy = async () => {
5960
try {
60-
const res = await fetch("http://localhost:8000/api/v1/policies/", {
61+
const res = await fetch(getApiUrl("/policies/"), {
6162
method: "POST",
6263
headers: { "Content-Type": "application/json" },
6364
body: JSON.stringify(newPolicy),

AIGovHub/frontend/src/components/PolicyViolations.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
} from "@mantine/core";
1313
import { useNavigate } from "react-router-dom";
1414
import type { PolicyViolation } from "../types";
15+
import { getApiUrl } from "../lib/apiUrl";
1516

1617
export function PolicyViolations() {
1718
const [violations, setViolations] = useState<PolicyViolation[]>([]);
@@ -21,9 +22,7 @@ export function PolicyViolations() {
2122
let cancelled = false;
2223
(async () => {
2324
try {
24-
const res = await fetch(
25-
"http://localhost:8000/api/v1/policies/violations/",
26-
);
25+
const res = await fetch(getApiUrl("/policies/violations/"));
2726
const data = await res.json();
2827
if (!cancelled) setViolations(data);
2928
} catch {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// API URL utility for consistent backend access across all components
2+
export const getApiUrl = (endpoint: string = ''): string => {
3+
// Detect if running in GitHub Codespaces
4+
if (typeof window !== 'undefined' && window.location.hostname.includes('.app.github.dev')) {
5+
// Replace port 3000 with 8000 for backend API
6+
const backendUrl = window.location.origin.replace('-3000.app.github.dev', '-8000.app.github.dev');
7+
return `${backendUrl}/api/v1${endpoint}`;
8+
}
9+
10+
// Fall back to localhost for local development
11+
const baseUrl = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000';
12+
return `${baseUrl}/api/v1${endpoint}`;
13+
};

ApacheTacticalMesh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 18b00990f2a55ae49265264bd55ad8d7991dfb1f

ChirpDecoder

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit f462bfdc2c7e7bbfd000874e943e09340933a31f

SpaceComms

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 751015e24e7bf62797d0fc406e7a95757133a35b

SpaceComms.wiki

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit afb4c6f80534176778bc5131d30f3adf0ca5d32e

0 commit comments

Comments
 (0)