Skip to content

feat(UI-1451): add ability to run docker with custom backend url and /api backend proxy #1118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VITE_HOST_URL=http://localhost:1234
API_URL=http://localhost:1234
VITE_DESCOPE_PROJECT_ID=descopeProjectId
TESTS_JWT_AUTH_TOKEN=jwtAuthTokenToRunE2ETests
OPEN_AI_KEY=openAiKey
34 changes: 27 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
FROM node:latest AS builder
# Build stage
FROM node:21-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY package.json package-lock.json ./
RUN npm ci --ignore-scripts --include=dev
COPY . .
RUN git submodule update --init
RUN npm run build
EXPOSE 8000
CMD ["npm", "run", "dev"]
ENV NODE_ENV=production
RUN npm run build:prod
RUN ls -l /app/dist && cat /app/dist/index.html

# Production stage
FROM nginx:alpine AS production
WORKDIR /app
COPY --from=build /app/dist /usr/share/nginx/html

# Copy Nginx template (not the final config)
COPY nginx.conf.template /app/nginx.conf.template

# Copy entrypoint script
COPY docker-entrypoint.sh /app/docker-entrypoint.sh

# Make entrypoint executable
RUN chmod +x /app/docker-entrypoint.sh

EXPOSE 80

ENTRYPOINT ["/app/docker-entrypoint.sh"]

CMD ["nginx", "-g", "daemon off;"]
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ Create a `.env` file in the root of the project directory and add the necessary

`sh cp .env.example .env` Then edit .env with your environment-specific settings

`VITE_HOST_URL`
`API_URL`

- Default: A predefined default host URL mentioned in [getApiBaseUrlFile](https://github.com/autokitteh/web-platform/blob/main/src/utilities/getApiBaseUrl.utils.ts)
- Description: Defines the backend URL that the application will use as its host. If not set, the application will use a default host URL.
- Example: VITE_HOST_URL=http://localhost:1234
- Example: API_URL=http://localhost:1234

`VITE_DESCOPE_PROJECT_ID`

Expand Down
21 changes: 21 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh

# Patching index.html
API_URL=${API_URL:-"/api"}
sed -i 's|appBaseUrl=""|appBaseUrl="'"$API_URL"'"|' /usr/share/nginx/html/index.html
echo "Added appBaseUrl="$API_URL" configuration for react app"

# Patchng NGINX
if [ -n "$NGNIX_PROXY_API_URL" ]; then
if [ "${NGNIX_PROXY_API_URL: -1}" != "/" ]; then
NGNIX_PROXY_API_URL="${NGNIX_PROXY_API_URL}/"
echo "Added trailing slash to NGNIX_PROXY_API_URL: $NGNIX_PROXY_API_URL"
fi

sed -i 's|https://api.autokitteh.cloud|'"${NGNIX_PROXY_API_URL}"'|' /app/nginx.conf.template
echo "Added NGINX API routing configuration for $NGNIX_PROXY_API_URL"
fi

cp /app/nginx.conf.template /etc/nginx/conf.d/default.conf

exec "$@"
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>
<html lang="en">
<head>
<script>window.appBaseUrl="";</script>
<% if (env.VITE_GTM_ID) { %>
<!-- Google Tag Manager -->
<script>
Expand Down
20 changes: 20 additions & 0 deletions nginx.conf.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
server {
listen 80;

root /usr/share/nginx/html;
index index.html;

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

location / {
try_files $uri $uri/ /index.html;
}

location /api/ {
proxy_pass https://api.autokitteh.cloud;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
Binary file modified src/assets/templates/kittehub.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
interface AppConfig {
apiBaseUrl: string;
}

const runtimeConfig = (window as any).appConfig || {};
export const config: AppConfig = {
apiBaseUrl: runtimeConfig.apiBaseUrl || "",
};
23 changes: 21 additions & 2 deletions src/utilities/getApiBaseUrl.utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
/* eslint-disable no-console */
import { ValidateURL } from "@utilities/validateUrl.utils";

export const getApiBaseUrl = (): string => {
if (window?.appBaseUrl) {
if (ValidateURL(window.appBaseUrl) || window.appBaseUrl === "/api") return window.appBaseUrl;
else {
console.error("Invalid API base URL found. Please set a valid URL in your environment variables.");
}
}

const hostUrl = import.meta.env.API_URL;
if (hostUrl) {
if (ValidateURL(hostUrl)) return hostUrl;
else {
console.error("Invalid HOST_BASE_URL found. Please set a valid URL in your environment variables.");
return "";
}
}

const { hostname } = window.location;
if (hostname === "app.autokitteh.cloud") {
return "https://api.autokitteh.cloud";
}

if (hostname.endsWith(".autokitteh.cloud")) {
const [env, ...rest] = hostname.split(".");

return `https://${env}-api.${rest.join(".")}`;
}

return import.meta.env.VITE_HOST_URL || "http://localhost:9980";
return "http://localhost:9980";
};
2 changes: 1 addition & 1 deletion src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface ImportMetaEnv {
readonly GOOGLE_ANALYTICS_ID: string;
readonly TESTS_JWT_AUTH_TOKEN: string;
readonly SENTRY_DSN: string;
readonly VITE_HOST_URL: string;
readonly API_URL: string;
readonly DISPLAY_DISCORD_INTEGRATION: boolean;
readonly DISPLAY_SLACK_SOCKET_INTEGRATION: boolean;
readonly VITE_GTM_ID: string;
Expand Down
32 changes: 8 additions & 24 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default defineConfig({
"import.meta.env.VITE_NODE_ENV": JSON.stringify(process.env.VITE_NODE_ENV),
"import.meta.env.VITE_DESCOPE_PROJECT_ID": JSON.stringify(process.env.VITE_DESCOPE_PROJECT_ID),
"import.meta.env.GOOGLE_ANALYTICS_ID": JSON.stringify(process.env.GOOGLE_ANALYTICS_ID),
"import.meta.env.VITE_HOST_URL": JSON.stringify(process.env.VITE_HOST_URL),
"import.meta.env.API_URL": JSON.stringify(process.env.API_URL),
"import.meta.env.DISPLAY_DISCORD_INTEGRATION": process.env.DISPLAY_DISCORD_INTEGRATION,
"import.meta.env.DISPLAY_SLACK_SOCKET_INTEGRATION": process.env.DISPLAY_SLACK_SOCKET_INTEGRATION,
"import.meta.env.SENTRY_DSN": JSON.stringify(process.env.SENTRY_DSN),
Expand Down Expand Up @@ -85,13 +85,8 @@ export default defineConfig({
cleanupIDs: false,
removeUselessStrokeAndFill: false,
removeUnknownsAndDefaults: false,
convertPathData: {
floatPrecision: 2,
transformPrecision: 4,
},
cleanupNumericValues: {
floatPrecision: 2,
},
convertPathData: { floatPrecision: 2, transformPrecision: 4 },
cleanupNumericValues: { floatPrecision: 2 },
collapseGroups: true,
mergePaths: true,
convertTransform: true,
Expand All @@ -109,18 +104,9 @@ export default defineConfig({
}),
viteStaticCopy({
targets: [
{
src: "src/assets/templates/**/*",
dest: "assets/templates",
},
{
src: "src/assets/new_project_program/**/*",
dest: "assets/new_project_program",
},
{
src: "src/assets/image/pages/**/*",
dest: "assets/image/pages",
},
{ src: "src/assets/templates/**/*", dest: "assets/templates" },
{ src: "src/assets/new_project_program/**/*", dest: "assets/new_project_program" },
{ src: "src/assets/image/pages/**/*", dest: "assets/image/pages" },
],
}),
reactVirtualized(),
Expand Down Expand Up @@ -151,10 +137,8 @@ export default defineConfig({
"tailwind-config": path.resolve(__dirname, "./tailwind.config.cjs"),
},
},

server: {
host: process.env.VITE_APP_DOMAIN ? JSON.stringify(process.env.VITE_APP_DOMAIN) : true,
port: process.env.VITE_LOCAL_PORT ? Number(process.env.VITE_LOCAL_PORT) : 8000,
strictPort: true,
port: 8000,
host: true,
},
});
Loading