Skip to content

Commit 84fef18

Browse files
committed
feat: backend proxy feature
1 parent d3b9617 commit 84fef18

File tree

3 files changed

+23
-26
lines changed

3 files changed

+23
-26
lines changed

entrypoint.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#!/bin/sh
22

3-
if [ -z "$VITE_HOST_URL" ]; then
4-
echo "window.appConfig = { apiBaseUrl: \"\", proxy: false };" > /usr/share/nginx/html/config.js
3+
if [ -n "$VITE_HOST_URL" ]; then
4+
echo "window.appConfig = { apiBaseUrl: \"$VITE_HOST_URL\", rerouteApi: false };" > /usr/share/nginx/html/config.js
5+
elif [ -n "$REROUTE_API" ]; then
6+
echo "window.appConfig = { rerouteApi: \"$REROUTE_API\" };" > /usr/share/nginx/html/config.js
57
else
6-
echo "window.appConfig = { apiBaseUrl: \"$VITE_HOST_URL\", proxy: \"$PROXY\" };" > /usr/share/nginx/html/config.js
8+
echo "window.appConfig = { apiBaseUrl: \"\" };" > /usr/share/nginx/html/config.js
79
fi
810

911
cat /usr/share/nginx/html/config.js
1012

11-
# Substitute environment variables into nginx.conf.template and output to default.conf
1213
envsubst '${VITE_HOST_URL}' < /app/nginx.conf.template > /etc/nginx/conf.d/default.conf
1314

14-
# Start Nginx
1515
nginx -g "daemon off;"

src/utilities/getApiBaseUrl.utils.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
1+
/* eslint-disable no-console */
2+
import { ValidateURL } from "@src/utilities";
3+
14
export const getApiBaseUrl = (): string => {
2-
const buildUrl = (url: string, useProxy: boolean): string => {
3-
if (url.match(/^(http|https):\/\//)) {
4-
return useProxy ? "/api" : url; // If proxying, return "/api"; otherwise, use the full URL
5-
} else {
6-
const path = url.startsWith("/") ? url.substring(1) : url;
7-
return `${window.location.origin}/${path}`;
5+
if (window.appConfig?.rerouteApi) {
6+
if (window.appConfig.rerouteApi.toString()?.toLowerCase === "true") return "/api";
7+
else {
8+
console.error("Reroute API is set to false. Please set it to 'true' to reroute the API.");
89
}
9-
};
10+
}
1011

11-
// Check runtime config first (injected via config.js)
1212
if (window.appConfig?.apiBaseUrl) {
13-
const useProxy = window.appConfig.proxy === "true" || window.appConfig.proxy === true;
14-
return buildUrl(window.appConfig.apiBaseUrl, useProxy);
13+
if (ValidateURL(window.appConfig.apiBaseUrl)) return window.appConfig.apiBaseUrl;
14+
else {
15+
console.error("Invalid API base URL found. Please set a valid URL in your environment variables.");
16+
}
1517
}
1618

17-
// Fallback to Vite env var (for dev or if config.js isn’t set)
1819
const hostUrl = import.meta.env.VITE_HOST_URL;
1920
if (hostUrl) {
20-
return buildUrl(hostUrl, false); // Default to no proxy for Vite env
21+
if (ValidateURL(hostUrl)) return hostUrl;
22+
else {
23+
console.error("Invalid HOST_BASE_URL found. Please set a valid URL in your environment variables.");
24+
return "";
25+
}
2126
}
2227

23-
// Fallback logic based on hostname
2428
const { hostname } = window.location;
2529
if (hostname === "app.autokitteh.cloud") {
2630
return "https://api.autokitteh.cloud";

vite.config.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default defineConfig({
4242
"import.meta.env.VITE_NODE_ENV": JSON.stringify(process.env.VITE_NODE_ENV),
4343
"import.meta.env.VITE_DESCOPE_PROJECT_ID": JSON.stringify(process.env.VITE_DESCOPE_PROJECT_ID),
4444
"import.meta.env.GOOGLE_ANALYTICS_ID": JSON.stringify(process.env.GOOGLE_ANALYTICS_ID),
45-
// Remove VITE_HOST_URL if runtime injection is the only source
45+
"import.meta.env.VITE_HOST_URL": JSON.stringify(process.env.VITE_HOST_URL),
4646
"import.meta.env.DISPLAY_DISCORD_INTEGRATION": process.env.DISPLAY_DISCORD_INTEGRATION,
4747
"import.meta.env.DISPLAY_SLACK_SOCKET_INTEGRATION": process.env.DISPLAY_SLACK_SOCKET_INTEGRATION,
4848
"import.meta.env.SENTRY_DSN": JSON.stringify(process.env.SENTRY_DSN),
@@ -140,12 +140,5 @@ export default defineConfig({
140140
server: {
141141
port: 8000,
142142
host: true,
143-
proxy: {
144-
"/api": {
145-
target: process.env.VITE_HOST_URL || "http://localhost:9980",
146-
changeOrigin: true,
147-
rewrite: (path) => path.replace(/^\/api/, ""),
148-
},
149-
},
150143
},
151144
});

0 commit comments

Comments
 (0)