Skip to content

Commit f72c137

Browse files
committed
Enables Azure runtime config for env variables
Allows runtime overrides for Azure deployments without rebuild Adds a startup script to inject custom environment references Enhances debugging to confirm variable usage
1 parent 317230d commit f72c137

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

editor/Dockerfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ ENV VITE_OAUTH_WEB_CLIENT_ID=$VITE_OAUTH_WEB_CLIENT_ID
2424
ENV VITE_OAUTH_WEB_CLIENT_SECRET=$VITE_OAUTH_WEB_CLIENT_SECRET
2525
ENV VITE_NODE_ENV=$VITE_NODE_ENV
2626

27+
# Debug: Show what environment variables are available during build
28+
RUN echo "🔍 Build Environment Debug:" && \
29+
echo "VITE_BACKEND_BASE_URL=$VITE_BACKEND_BASE_URL" && \
30+
echo "VITE_LOGIN_BASE_URL=$VITE_LOGIN_BASE_URL" && \
31+
echo "VITE_OAUTH_WEB_CLIENT_ID=$VITE_OAUTH_WEB_CLIENT_ID" && \
32+
echo "VITE_OAUTH_WEB_CLIENT_SECRET=$(echo $VITE_OAUTH_WEB_CLIENT_SECRET | sed 's/./*/g')" && \
33+
echo "VITE_NODE_ENV=$VITE_NODE_ENV"
34+
2735
# Copy package files first for better Docker layer caching
2836
COPY package.json package-lock.json ./
2937

@@ -40,6 +48,10 @@ RUN npm run build
4048
# Production stage
4149
FROM nginx:alpine
4250

51+
# Copy startup script
52+
COPY startup.sh /startup.sh
53+
RUN chmod +x /startup.sh
54+
4355
# Install required tools for runtime environment injection
4456
RUN apk add --no-cache bash
4557

editor/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<title>TrackMan Script Editor</title>
77
<link rel="icon" type="image/x-icon" href="https://golf-portal-dev.trackmangolfdev.com/favicon.ico" />
88
<link rel="stylesheet" href="/src/index.css" />
9+
<!-- Runtime configuration for Azure App Service -->
10+
<script src="/runtime-config.js"></script>
911
</head>
1012
<body>
1113
<div id="root"></div>

editor/src/lib/env.ts

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ function stripTrailingSlash(url: string): string {
2020
}
2121

2222
const backendBase = (() => {
23+
// Try runtime configuration first (for Azure App Service)
24+
const runtimeBase = (window as any)?.runtimeConfig?.VITE_BACKEND_BASE_URL;
25+
if (runtimeBase && runtimeBase.startsWith('http')) {
26+
return stripTrailingSlash(runtimeBase);
27+
}
28+
29+
// Fall back to build-time environment
2330
const base = import.meta.env.VITE_BACKEND_BASE_URL?.trim();
2431
if (base) return stripTrailingSlash(base);
2532

@@ -33,6 +40,13 @@ const backendBase = (() => {
3340
})();
3441

3542
const loginBase = (() => {
43+
// Try runtime configuration first (for Azure App Service)
44+
const runtimeBase = (window as any)?.runtimeConfig?.VITE_LOGIN_BASE_URL;
45+
if (runtimeBase && runtimeBase.startsWith('http')) {
46+
return stripTrailingSlash(runtimeBase);
47+
}
48+
49+
// Fall back to build-time environment
3650
const base = import.meta.env.VITE_LOGIN_BASE_URL?.trim();
3751
if (base) return stripTrailingSlash(base);
3852

@@ -53,18 +67,38 @@ export const ENV_URLS = {
5367
oauthToken: loginBase ? `${loginBase}/connect/token` : import.meta.env.VITE_OAUTH_TOKEN_URL || '',
5468
};
5569

70+
// Debug environment loading
71+
console.log('🔍 Environment Debug:', {
72+
backendBase,
73+
loginBase,
74+
'import.meta.env.VITE_BACKEND_BASE_URL': import.meta.env.VITE_BACKEND_BASE_URL,
75+
'import.meta.env.VITE_LOGIN_BASE_URL': import.meta.env.VITE_LOGIN_BASE_URL,
76+
'import.meta.env.VITE_OAUTH_WEB_CLIENT_ID': import.meta.env.VITE_OAUTH_WEB_CLIENT_ID ? 'SET' : 'NOT SET',
77+
ENV_URLS
78+
});
79+
5680
// OAuth Web Client Configuration (for authorization code flow)
57-
// Function to get OAuth client ID - simplified approach
81+
// Function to get OAuth client ID - with runtime support
5882
function getOAuthClientId(): string {
59-
// Use build-time environment variable directly
60-
// This will be set during the Docker build process in Azure
83+
// Try runtime configuration first (for Azure App Service)
84+
const runtimeClientId = (window as any)?.runtimeConfig?.VITE_OAUTH_WEB_CLIENT_ID;
85+
if (runtimeClientId && runtimeClientId.length > 10) {
86+
return runtimeClientId;
87+
}
88+
89+
// Fall back to build-time environment variable
6190
return import.meta.env.VITE_OAUTH_WEB_CLIENT_ID || '';
6291
}
6392

64-
// Function to get OAuth client secret - simplified approach
93+
// Function to get OAuth client secret - with runtime support
6594
function getOAuthClientSecret(): string {
66-
// Use build-time environment variable directly
67-
// This will be set during the Docker build process in Azure
95+
// Try runtime configuration first (for Azure App Service)
96+
const runtimeClientSecret = (window as any)?.runtimeConfig?.VITE_OAUTH_WEB_CLIENT_SECRET;
97+
if (runtimeClientSecret && runtimeClientSecret.length > 10) {
98+
return runtimeClientSecret;
99+
}
100+
101+
// Fall back to build-time environment variable
68102
return import.meta.env.VITE_OAUTH_WEB_CLIENT_SECRET || '';
69103
}
70104

@@ -92,10 +126,22 @@ export const OAUTH_CONFIG = {
92126
};
93127

94128
export function assertRequiredUrls() {
129+
console.log('🔍 Asserting required URLs:', ENV_URLS);
130+
95131
if (!ENV_URLS.graphql) {
132+
console.error('❌ GraphQL endpoint is not configured:', {
133+
backendBase,
134+
'VITE_BACKEND_BASE_URL': import.meta.env.VITE_BACKEND_BASE_URL,
135+
'VITE_GRAPHQL_URL': import.meta.env.VITE_GRAPHQL_URL
136+
});
96137
throw new Error('GraphQL endpoint is not configured. Set VITE_BACKEND_BASE_URL or VITE_GRAPHQL_URL.');
97138
}
98139
if (!ENV_URLS.oauthToken) {
140+
console.error('❌ OAuth token endpoint is not configured:', {
141+
loginBase,
142+
'VITE_LOGIN_BASE_URL': import.meta.env.VITE_LOGIN_BASE_URL,
143+
'VITE_OAUTH_TOKEN_URL': import.meta.env.VITE_OAUTH_TOKEN_URL
144+
});
99145
throw new Error('OAuth token endpoint is not configured. Set VITE_LOGIN_BASE_URL or VITE_OAUTH_TOKEN_URL.');
100146
}
101147
}

editor/startup.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
# Azure App Service startup script
3+
# This script creates a runtime configuration file from environment variables
4+
5+
echo "🚀 Starting Azure App Service configuration..."
6+
7+
# Create runtime configuration file
8+
cat > /usr/share/nginx/html/runtime-config.js << EOF
9+
// Runtime configuration for Azure App Service
10+
window.runtimeConfig = {
11+
VITE_BACKEND_BASE_URL: '${VITE_BACKEND_BASE_URL}',
12+
VITE_LOGIN_BASE_URL: '${VITE_LOGIN_BASE_URL}',
13+
VITE_OAUTH_WEB_CLIENT_ID: '${VITE_OAUTH_WEB_CLIENT_ID}',
14+
VITE_OAUTH_WEB_CLIENT_SECRET: '${VITE_OAUTH_WEB_CLIENT_SECRET}',
15+
VITE_NODE_ENV: '${VITE_NODE_ENV}'
16+
};
17+
18+
console.log('🔧 Runtime configuration loaded for Azure App Service');
19+
EOF
20+
21+
echo "✅ Runtime configuration created"
22+
23+
# Start nginx
24+
echo "🌐 Starting nginx..."
25+
nginx -g "daemon off;"

0 commit comments

Comments
 (0)