Skip to content

Commit eac1632

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

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
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\" };" > /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 = {};" > /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;"

index.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<!doctype html>
22
<html lang="en">
33
<head>
4+
<% if (env.VITE_GTM_ID) { %>
45
<!-- Google Tag Manager -->
56
<script>
67
(function (w, d, s, l, i) {
@@ -15,6 +16,7 @@
1516
})(window, document, "script", "dataLayer", "<%= env.VITE_GTM_ID %>");
1617
</script>
1718
<!-- End Google Tag Manager -->
19+
<% } %>
1820

1921
<meta charset="UTF-8" />
2022
<link rel="icon" type="image/svg+xml" href="/favicon.png" />
@@ -36,7 +38,7 @@
3638
<!-- Google Tag Manager (noscript) -->
3739
<noscript
3840
><iframe
39-
src="https://www.googletagmanager.com/ns.html?id=<%= env.VITE_GTM_ID %>"
41+
src="https://www.googletagmanager.com/ns.html?id=%VITE_GTM_ID%"
4042
height="0"
4143
width="0"
4244
style="display: none; visibility: hidden"
@@ -58,4 +60,4 @@
5860
<!-- End of HubSpot Embed Code -->
5961
<% } %>
6062
</body>
61-
</html>
63+
</html>

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)