Skip to content

Commit

Permalink
Merge pull request #114 from SciPhi-AI/Nolan/EnvVars
Browse files Browse the repository at this point in the history
Allow runtime env vars
  • Loading branch information
NolanTrem authored Oct 25, 2024
2 parents afe1554 + dcff91b commit 92019d6
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 42 deletions.
13 changes: 10 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Builder Stage
FROM node:22-alpine AS builder
WORKDIR /app

Expand All @@ -16,7 +17,7 @@ COPY . .
# Build the Next.js application
RUN pnpm build

# Production stage
# Production Stage
FROM node:22-alpine AS runner
WORKDIR /app

Expand All @@ -32,8 +33,14 @@ COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

# Copy the startup script
COPY startup.sh /app/startup.sh

# Ensure the startup script is executable
RUN chmod +x /app/startup.sh

# Expose the port the app runs on
EXPOSE 3000

# Define the command to run the app
CMD ["node", "server.js"]
# Define the command to run the startup script
CMD ["/app/startup.sh"]
9 changes: 0 additions & 9 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,4 @@ module.exports = {
},
],
},
env: {
NEXT_PUBLIC_R2R_DEPLOYMENT_URL: process.env.NEXT_PUBLIC_R2R_DEPLOYMENT_URL,
R2R_DASHBOARD_DISABLE_TELEMETRY:
process.env.R2R_DASHBOARD_DISABLE_TELEMETRY,
SUPABASE_URL: process.env.SUPABASE_URL,
SUPABASE_ANON_KEY: process.env.SUPABASE_ANON_KEY,
NEXT_PUBLIC_HATCHET_DASHBOARD_URL:
process.env.NEXT_PUBLIC_HATCHET_DASHBOARD_URL,
},
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
"pnpm": "^9.5.0",
"postcss": "^8.4.39",
"posthog-js": "^1.148.0",
"r2r-js": "file:/Users/nolantremelling/R2R/js/sdk/r2r-js-0.3.10.tgz",
"r2r-js": "^0.3.11",
"radix-ui": "^1.0.1",
"react": "18.3.1",
"react-chartjs-2": "^5.2.0",
Expand Down
11 changes: 5 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions public/env-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
window.__RUNTIME_CONFIG__ = {
NEXT_PUBLIC_R2R_DEPLOYMENT_URL: '__NEXT_PUBLIC_R2R_DEPLOYMENT_URL__',
R2R_DASHBOARD_DISABLE_TELEMETRY: '__R2R_DASHBOARD_DISABLE_TELEMETRY__',
SUPABASE_URL: '__SUPABASE_URL__',
SUPABASE_ANON_KEY: '__SUPABASE_ANON_KEY__',
NEXT_PUBLIC_HATCHET_DASHBOARD_URL: '__NEXT_PUBLIC_HATCHET_DASHBOARD_URL__',
};
2 changes: 1 addition & 1 deletion src/lib/posthog-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function initializePostHog() {
autocapture: true,
});

if (process.env.R2R_DASHBOARD_DISABLE_TELEMETRY === 'true') {
if (window.__RUNTIME_CONFIG__?.R2R_DASHBOARD_DISABLE_TELEMETRY === 'true') {
posthog.opt_out_capturing();
}
}
Expand Down
27 changes: 19 additions & 8 deletions src/lib/supabase.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import { createClient } from '@supabase/supabase-js';
// lib/supabase.ts

let supabase: ReturnType<typeof createClient> | null = null;
import { createClient, SupabaseClient } from '@supabase/supabase-js';

if (process.env.SUPABASE_URL && process.env.SUPABASE_ANON_KEY) {
supabase = createClient(
process.env.SUPABASE_URL,
process.env.SUPABASE_ANON_KEY
);
}
let supabase: SupabaseClient | null = null;

export const getSupabase = (): SupabaseClient | null => {
if (typeof window !== 'undefined' && window.__RUNTIME_CONFIG__) {
if (!supabase) {
const supabaseUrl = window.__RUNTIME_CONFIG__.SUPABASE_URL;
const supabaseAnonKey = window.__RUNTIME_CONFIG__.SUPABASE_ANON_KEY;
if (supabaseUrl && supabaseAnonKey) {
supabase = createClient(supabaseUrl, supabaseAnonKey);
} else {
console.warn('Supabase URL or Anon Key is missing in runtime config.');
}
}
return supabase;
}
return null;
};

export { supabase };
17 changes: 15 additions & 2 deletions src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,10 @@ function MyAppContent({ Component, pageProps }: AppProps) {
}

if (isSuperUser()) {
// Superusers can access all routes
return;
}

if (!isUserRoute(currentPath)) {
// Non-superusers are redirected to /documents if they try to access any other page
router.replace('/documents');
}
}, [isAuthenticated, isSuperUser, authState.userRole, router]);
Expand All @@ -53,6 +51,21 @@ function MyAppContent({ Component, pageProps }: AppProps) {
}

function MyApp(props: AppProps) {
// Move the runtime config check into useEffect
useEffect(() => {
// Load the env-config.js script dynamically
const script = document.createElement('script');
script.src = '/env-config.js';
script.onload = () => {
if (typeof window !== 'undefined' && window.__RUNTIME_CONFIG__) {
console.log('Runtime Config:', window.__RUNTIME_CONFIG__);
} else {
console.warn('Runtime Config not found!');
}
};
document.body.appendChild(script);
}, []);

return (
<ThemeProvider
attribute="class"
Expand Down
35 changes: 26 additions & 9 deletions src/pages/auth/login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import { useUserContext } from '@/context/UserContext';
import debounce from '@/lib/debounce';
import { supabase } from '@/lib/supabase';

const DEFAULT_DEPLOYMENT_URL = 'http://localhost:7272';

const LoginPage: React.FC = () => {
const [email, setEmail] = useState('[email protected]');
const [password, setPassword] = useState('change_me_immediately');
Expand All @@ -26,10 +28,14 @@ const LoginPage: React.FC = () => {
const [sanitizedDeploymentUrl, setSanitizedDeploymentUrl] = useState('');

useEffect(() => {
const deploymentUrl =
process.env.NEXT_PUBLIC_R2R_DEPLOYMENT_URL || 'http://localhost:7272';
setRawDeploymentUrl(deploymentUrl);
setSanitizedDeploymentUrl(deploymentUrl);
if (typeof window !== 'undefined') {
const deploymentUrl =
window.__RUNTIME_CONFIG__?.NEXT_PUBLIC_R2R_DEPLOYMENT_URL;
if (deploymentUrl) {
setRawDeploymentUrl(deploymentUrl);
setSanitizedDeploymentUrl(deploymentUrl);
}
}
}, []);

const handleSubmit = async (e: React.FormEvent) => {
Expand Down Expand Up @@ -125,14 +131,25 @@ const LoginPage: React.FC = () => {
};

const sanitizeUrl = (url: string): string => {
const defaultUrl =
process.env.NEXT_PUBLIC_R2R_DEPLOYMENT_URL || 'http://localhost:7272';
let sanitized = url.trim();
if (
typeof window !== 'undefined' &&
window.__RUNTIME_CONFIG__?.NEXT_PUBLIC_R2R_DEPLOYMENT_URL
) {
const configUrl =
window.__RUNTIME_CONFIG__.NEXT_PUBLIC_R2R_DEPLOYMENT_URL;

// If the URL is empty or just a protocol, return the config URL
if (!url || url === 'http://' || url === 'https://') {
return configUrl;
}
}

if (!sanitized || sanitized === 'http://' || sanitized === 'https://') {
return defaultUrl;
// If no config URL is available, use the default
if (!url || url === 'http://' || url === 'https://') {
return DEFAULT_DEPLOYMENT_URL;
}

let sanitized = url.trim();
sanitized = sanitized.replace(/\/+$/, '');

if (!/^https?:\/\//i.test(sanitized)) {
Expand Down
13 changes: 11 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,19 @@ const HomePage = () => {
className="rounded-md py-1 px-3"
color="light"
disabled={
!process.env.NEXT_PUBLIC_HATCHET_DASHBOARD_URL
!window.__RUNTIME_CONFIG__
?.NEXT_PUBLIC_HATCHET_DASHBOARD_URL
}
onClick={() =>
window.open(
window.__RUNTIME_CONFIG__
.NEXT_PUBLIC_HATCHET_DASHBOARD_URL,
'_blank'
)
}
tooltip={
!process.env.NEXT_PUBLIC_HATCHET_DASHBOARD_URL ? (
!window.__RUNTIME_CONFIG__
?.NEXT_PUBLIC_HATCHET_DASHBOARD_URL ? (
<div>
Hatchet Dashboard Deployment URL unavailable.
<br />
Expand Down
17 changes: 17 additions & 0 deletions startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# Function to replace placeholders with environment variables
replace_env_vars() {
echo "Replacing environment variables in env-config.js..."
sed -i "s|__NEXT_PUBLIC_R2R_DEPLOYMENT_URL__|${NEXT_PUBLIC_R2R_DEPLOYMENT_URL}|g" /app/public/env-config.js
sed -i "s|__R2R_DASHBOARD_DISABLE_TELEMETRY__|${R2R_DASHBOARD_DISABLE_TELEMETRY}|g" /app/public/env-config.js
sed -i "s|__SUPABASE_URL__|${SUPABASE_URL}|g" /app/public/env-config.js
sed -i "s|__SUPABASE_ANON_KEY__|${SUPABASE_ANON_KEY}|g" /app/public/env-config.js
sed -i "s|__NEXT_PUBLIC_HATCHET_DASHBOARD_URL__|${NEXT_PUBLIC_HATCHET_DASHBOARD_URL}|g" /app/public/env-config.js
}

# Replace environment variables
replace_env_vars

# Start the Next.js server
exec node server.js
9 changes: 8 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
}
]
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"typeRoots": ["./node_modules/@types", "./types"],
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
".next/types/**/*.ts",
"types/**/*.d.ts"
],
"exclude": ["node_modules", "cloud-docs"]
}
14 changes: 14 additions & 0 deletions types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// types/globals.d.ts
declare global {
interface Window {
__RUNTIME_CONFIG__: {
NEXT_PUBLIC_R2R_DEPLOYMENT_URL: string;
R2R_DASHBOARD_DISABLE_TELEMETRY: string;
SUPABASE_URL: string;
SUPABASE_ANON_KEY: string;
NEXT_PUBLIC_HATCHET_DASHBOARD_URL: string;
};
}
}

export {};

0 comments on commit 92019d6

Please sign in to comment.