Skip to content

Commit 70d6fff

Browse files
committed
url fix
1 parent 671ad23 commit 70d6fff

3 files changed

Lines changed: 74 additions & 6 deletions

File tree

‎README.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ Web app for **wall-clock profiling**, **growth-curve fitting**, and **learning c
1616

1717
Environment variables for Django live in `time_complexity_analyzer/time_complexity_analyzer/settings.py` (e.g. `DJANGO_SECRET_KEY`). Use a real secret in production.
1818

19+
### Render.com (split static site + API)
20+
21+
The UI calls Django under **`/api/...`**. Those routes exist on the **API** service only.
22+
23+
1. On the **static site** build, set **`REACT_APP_API_URL`** to your API’s public origin, e.g. `https://your-time-complexity-api.onrender.com` (no `/api` suffix). See `time_complexity_analyzer/frontend/.env.production.example`.
24+
2. **Rebuild** the static site so Create React App inlines the variable (build-time only).
25+
3. Keep the **web/API** service running.
26+
27+
If `REACT_APP_API_URL` is missing, production builds default the API base to the **static site’s origin**; `POST /api/analyse-code/...` then hits the CDN and often returns **404**. You can also set `window.__TCA_API_BASE__` before the app bundle loads (see `frontend/src/components/Axios.js`).
28+
1929
---
2030

2131
## Stack (high level)

‎time_complexity_analyzer/frontend/src/components/Axios.js‎

Lines changed: 60 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,70 @@
11
import axios from 'axios';
22

3-
const baseURL = process.env.REACT_APP_API_URL || 'http://127.0.0.1:8000';
3+
/**
4+
* Normalize API origin: no trailing slash, strip accidental `/api` suffix
5+
* (paths already include `/api/...`).
6+
*/
7+
function normalizeApiBaseUrl(url) {
8+
if (!url || typeof url !== 'string') return '';
9+
let u = url.trim().replace(/\/+$/, '');
10+
while (u.endsWith('/api')) {
11+
u = u.slice(0, -4).replace(/\/+$/, '');
12+
}
13+
return u;
14+
}
15+
16+
/**
17+
* Resolve backend origin:
18+
* 1) `window.__TCA_API_BASE__` if set (runtime override, optional)
19+
* 2) `REACT_APP_API_URL` at build time (required for split static + API on Render, etc.)
20+
* 3) Dev: localhost Django
21+
* 4) Prod build without env: same browser origin (only works if Django serves this host too)
22+
*/
23+
function resolveApiBaseUrl() {
24+
if (typeof window !== 'undefined' && window.__TCA_API_BASE__) {
25+
return normalizeApiBaseUrl(String(window.__TCA_API_BASE__));
26+
}
27+
const env = process.env.REACT_APP_API_URL;
28+
if (env && String(env).trim()) {
29+
return normalizeApiBaseUrl(String(env).trim());
30+
}
31+
if (process.env.NODE_ENV !== 'production') {
32+
return 'http://127.0.0.1:8000';
33+
}
34+
if (typeof window !== 'undefined' && window.location?.origin) {
35+
return normalizeApiBaseUrl(window.location.origin);
36+
}
37+
return 'http://127.0.0.1:8000';
38+
}
39+
40+
const baseURL = resolveApiBaseUrl();
41+
42+
if (
43+
typeof window !== 'undefined' &&
44+
process.env.NODE_ENV === 'production' &&
45+
!process.env.REACT_APP_API_URL &&
46+
!window.__TCA_API_BASE__ &&
47+
window.location?.hostname &&
48+
!['localhost', '127.0.0.1'].includes(window.location.hostname)
49+
) {
50+
// eslint-disable-next-line no-console
51+
console.warn(
52+
'[TCA] REACT_APP_API_URL was not set at build time. Requests use this site origin; ' +
53+
'if your API is a separate service (e.g. another Render Web Service), rebuild the frontend with ' +
54+
'REACT_APP_API_URL=https://your-api-host.onrender.com (no /api suffix).'
55+
);
56+
}
57+
458
const AxiosInstance = axios.create({
5-
baseURL: baseURL,
59+
baseURL,
660
timeout: 150000,
761
headers: {
862
'Content-Type': 'application/json',
9-
'Accept': 'application/json',
63+
Accept: 'application/json',
1064
},
1165
});
1266

13-
AxiosInstance.interceptors.request.use(config => {
67+
AxiosInstance.interceptors.request.use((config) => {
1468
const token = localStorage.getItem('token');
1569
if (token) {
1670
config.headers.Authorization = `Bearer ${token}`;
@@ -19,8 +73,8 @@ AxiosInstance.interceptors.request.use(config => {
1973
});
2074

2175
AxiosInstance.interceptors.response.use(
22-
response => response,
23-
async error => {
76+
(response) => response,
77+
async (error) => {
2478
const originalRequest = error.config;
2579
if (
2680
error.response &&

‎time_complexity_analyzer/frontend/src/components/CalculatorPage.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ function CalculatorPage() {
343343
setError(
344344
"Sign in or create an account to run analysis. If you self-host the API, set TCA_ALLOW_ANONYMOUS_ANALYSIS=true (default) or DEBUG=true, then restart Django."
345345
);
346+
} else if (err.response?.status === 404) {
347+
setError(
348+
"API returned 404 (not found). On split hosting (e.g. Render static site + separate API), set REACT_APP_API_URL to your Django base URL when building the frontend (example: https://your-api.onrender.com with no trailing /api), redeploy the static site, and confirm the API service is running."
349+
);
346350
} else {
347351
setError(err.message || "Can't calculate it. Please check your code and try again.");
348352
}

0 commit comments

Comments
 (0)