Skip to content

Commit f02d21b

Browse files
authored
feat: implement runtime config loading for frontend (#52)
* fix: update AuthGate to use environment variable for Coder URL - Modified AuthGate component to retrieve Coder URL from environment variable instead of hardcoding. - Added new CODER_URL definition in env.d.ts for better configuration management. * refactor: enhance Vite configuration to load environment variables - Updated vite.config.mts to load environment variables based on the current mode, allowing for better configuration management. - Made CODER_URL available through import.meta.env for improved flexibility in accessing environment-specific settings. * fix: update AuthGate to use corrected environment variable for Coder URL - Changed the environment variable reference in AuthGate from VITE_CODER_URL to CODER_URL for consistency with the updated configuration management. * refactor: update Dockerfile and add startup script for application initialization - Modified Dockerfile to copy and execute a new startup script instead of directly running the application. - Introduced startup.sh to create a runtime configuration file with environment variables and start the application. - Updated index.html to include the new runtime configuration script. - Enhanced AuthGate component to utilize the runtime configuration for the Coder URL, providing fallback to environment variables. - Added global TypeScript definitions for the runtime configuration interface. * refactor: update runtime configuration file location and script reference - Changed the directory for the runtime configuration file from /config to /assets in startup.sh. - Updated index.html to reference the new location of the runtime configuration script.
1 parent 3793768 commit f02d21b

File tree

5 files changed

+28
-3
lines changed

5 files changed

+28
-3
lines changed

Dockerfile

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,9 @@ ENV PYTHONUNBUFFERED=1
4141
# Document the port number the container will expose
4242
EXPOSE 8000
4343

44-
# Run the application
45-
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]
44+
# Copy startup script
45+
COPY scripts/startup.sh /app/
46+
RUN chmod +x /app/startup.sh
47+
48+
# Run the startup script
49+
CMD ["/app/startup.sh"]

scripts/startup.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Create runtime config with environment variables
5+
mkdir -p /app/frontend/dist/assets
6+
cat > /app/frontend/dist/assets/runtime-config.js <<EOL
7+
window.RUNTIME_CONFIG = {
8+
CODER_URL: "${CODER_URL}"
9+
};
10+
EOL
11+
12+
# Start the application
13+
exec uvicorn main:app --host 0.0.0.0 --port 8000

src/frontend/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<title>Pad.ws</title>
1212
<link rel="icon" type="image/x-icon" href="/assets/images/favicon.png" />
13+
<script src="/assets/runtime-config.js"></script>
1314
</head>
1415

1516
<body>

src/frontend/src/AuthGate.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ export default function AuthGate({ children }: { children: React.ReactNode }) {
2121
if (isAuthenticated === true && !coderAuthDone) {
2222
const iframe = document.createElement("iframe");
2323
iframe.style.display = "none";
24-
const coderUrl = import.meta.env.CODER_URL;
24+
// Use runtime config if available, fall back to import.meta.env
25+
const coderUrl = window.RUNTIME_CONFIG?.CODER_URL || import.meta.env.CODER_URL;
2526
iframe.src = `${coderUrl}/api/v2/users/oidc/callback`;
2627
console.debug(`[pad.ws] (Silently) Priming Coder OIDC session for ${coderUrl}`);
2728

src/frontend/src/global.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
interface Window {
2+
RUNTIME_CONFIG?: {
3+
CODER_URL: string;
4+
};
5+
ExcalidrawLib: any;
6+
}

0 commit comments

Comments
 (0)