From f6ae79b0b5ec493b1843538b1c578272fefdee10 Mon Sep 17 00:00:00 2001
From: Andrew Pennington <50312474+andypenno@users.noreply.github.com>
Date: Fri, 23 Aug 2024 23:52:48 +0100
Subject: [PATCH] Removed the API route in favour of server side rendering;
 Updated documentation to list environment variables (#9)

- Use server side rendering in the `getServerEnv` library function, rather than using the API route
- Updated `README.md` to list the relevant environment variables for the frontend and backend
- Updated `NETWORKING.md` to reflect the changes made in `docker-compose.yaml`
---
 README.md                       | 17 ++++++++++++++
 docs/installation/NETWORKING.md | 18 +++++++--------
 ui/app/api/env/route.ts         | 16 -------------
 ui/lib/serverEnvironment.ts     | 40 +++++++++++++--------------------
 4 files changed, 41 insertions(+), 50 deletions(-)
 delete mode 100644 ui/app/api/env/route.ts

diff --git a/README.md b/README.md
index 3c87acc..d87a90e 100644
--- a/README.md
+++ b/README.md
@@ -97,6 +97,23 @@ There are mainly 2 ways of installing Perplexica - With Docker, Without Docker.
 
 See the [installation documentation](https://github.com/ItzCrazyKns/Perplexica/tree/master/docs/installation) for more information like exposing it your network, etc.
 
+### Environment Variables
+
+You can use environment variables to override the settings provided in your `config.toml` file.
+
+##### Frontend Environment Variables
+- `BACKEND_API_URL`
+- `BACKEND_WS_URL`
+
+##### Backend Environment Variables
+- `PORT`
+- `SIMILARITY_MEASURE`
+- `OPENAI_API_KEY`
+- `GROQ_API_KEY`
+- `ANTHROPIC_API_KEY`
+- `SEARXNG_API_ENDPOINT`
+- `OLLAMA_API_ENDPOINT`
+
 ### Ollama Connection Errors
 
 If you're encountering an Ollama connection error, it is likely due to the backend being unable to connect to Ollama's API. To fix this issue you can:
diff --git a/docs/installation/NETWORKING.md b/docs/installation/NETWORKING.md
index baad296..53c526e 100644
--- a/docs/installation/NETWORKING.md
+++ b/docs/installation/NETWORKING.md
@@ -19,9 +19,9 @@ docker compose down --rmi all
 5. Replace `127.0.0.1` with the IP address of the server Perplexica is running on in these two lines:
 
 ```
-args:
-  - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
-  - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+environment:
+  - BACKEND_API_URL=http://127.0.0.1:3001/api
+  - BACKEND_WS_URL=ws://127.0.0.1:3001
 ```
 
 6. Save and close the `docker-compose.yaml` file
@@ -57,9 +57,9 @@ nano docker-compose.yaml
 5. Replace `127.0.0.1` with the server IP in these lines:
 
 ```
-args:
-  - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
-  - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+environment:
+  - BACKEND_API_URL=http://127.0.0.1:3001/api
+  - BACKEND_WS_URL=ws://127.0.0.1:3001
 ```
 
 6. Save and exit the editor
@@ -95,9 +95,9 @@ nano docker-compose.yaml
 5. Replace `127.0.0.1` with the server IP:
 
 ```
-args:
-  - NEXT_PUBLIC_API_URL=http://127.0.0.1:3001/api
-  - NEXT_PUBLIC_WS_URL=ws://127.0.0.1:3001
+environment:
+  - BACKEND_API_URL=http://127.0.0.1:3001/api
+  - BACKEND_WS_URL=ws://127.0.0.1:3001
 ```
 
 6. Save and exit the editor
diff --git a/ui/app/api/env/route.ts b/ui/app/api/env/route.ts
deleted file mode 100644
index 01cf6fb..0000000
--- a/ui/app/api/env/route.ts
+++ /dev/null
@@ -1,16 +0,0 @@
-import process from 'process';
-import { NextResponse } from 'next/server';
-
-// Enable the Runtime
-export const runtime = "edge"
-
-export async function GET(_request: Request) {
-  // Access environment variables
-  const envVars = {
-    'BACKEND_API_URL': process.env.BACKEND_API_URL ?? process.env.NEXT_PUBLIC_API_URL,
-    'BACKEND_WS_URL': process.env.BACKEND_WS_URL ?? process.env.NEXT_PUBLIC_WS_URL
-  }
-
-  // Return the environment variables as a JSON response
-  return NextResponse.json(envVars);
-}
diff --git a/ui/lib/serverEnvironment.ts b/ui/lib/serverEnvironment.ts
index 1179fd6..a9444b7 100644
--- a/ui/lib/serverEnvironment.ts
+++ b/ui/lib/serverEnvironment.ts
@@ -1,29 +1,19 @@
-async function fetchConfig() {
-  try {
-    const response = await fetch('/api/env');
-    if (response.ok) {
-      const data = await response.json();
-      sessionStorage.setItem('cachedConfig', JSON.stringify(data));
-      return data;
-    } else {
-      throw new Error('Failed to fetch config');
-    }
-  } catch (error) {
-    return null;
-  }
-}
+'use server';
+
+import process from 'process';
 
 export async function getServerEnv(envVar: string): Promise<string> {
-  const cachedConfig = JSON.parse(sessionStorage.getItem('cachedConfig') || 'null');
-
-  if (cachedConfig) {
-    return cachedConfig[envVar];
+  let result: string | undefined;
+  switch (envVar) {
+      case "BACKEND_API_URL":
+          result = process.env.BACKEND_API_URL ?? process.env.NEXT_PUBLIC_API_URL;
+          break;
+      case "BACKEND_WS_URL":
+          result = process.env.BACKEND_WS_URL ?? process.env.NEXT_PUBLIC_WS_URL;
+          break;
+      default:
+          result = process.env[envVar];
+          break;
   }
-
-  const data = await fetchConfig();
-  if (!data) {
-    return "";
-  }
-
-  return data[envVar];
+  return result ?? "";
 }