From 0b4d048e9afbb00c3b77f0e06ca6cd68e1618be3 Mon Sep 17 00:00:00 2001 From: Haileyesus Date: Mon, 2 Feb 2026 11:54:55 +0300 Subject: [PATCH] refactor(backend): update environment variable handling and replace VITE_IS_PLATFORM with IS_PLATFORM constant --- server/constants/config.js | 2 +- server/index.js | 22 ++++------------------ server/load-env.js | 24 ++++++++++++++++++++++++ server/middleware/auth.js | 5 +++-- server/routes/agent.js | 5 +++-- 5 files changed, 35 insertions(+), 23 deletions(-) create mode 100644 server/load-env.js diff --git a/server/constants/config.js b/server/constants/config.js index 6741c35..580a985 100644 --- a/server/constants/config.js +++ b/server/constants/config.js @@ -2,4 +2,4 @@ * Environment Flag: Is Platform * Indicates if the app is running in Platform mode (hosted) or OSS mode (self-hosted) */ -export const IS_PLATFORM = import.meta.env.VITE_IS_PLATFORM === 'true'; \ No newline at end of file +export const IS_PLATFORM = process.env.VITE_IS_PLATFORM === 'true'; \ No newline at end of file diff --git a/server/index.js b/server/index.js index 1c42d30..1db726d 100755 --- a/server/index.js +++ b/server/index.js @@ -1,5 +1,6 @@ #!/usr/bin/env node -// Load environment variables from .env file +// Load environment variables before other imports execute +import './load-env.js'; import fs from 'fs'; import path from 'path'; import { fileURLToPath } from 'url'; @@ -28,22 +29,6 @@ const c = { dim: (text) => `${colors.dim}${text}${colors.reset}`, }; -try { - const envPath = path.join(__dirname, '../.env'); - const envFile = fs.readFileSync(envPath, 'utf8'); - envFile.split('\n').forEach(line => { - const trimmedLine = line.trim(); - if (trimmedLine && !trimmedLine.startsWith('#')) { - const [key, ...valueParts] = trimmedLine.split('='); - if (key && valueParts.length > 0 && !process.env[key]) { - process.env[key] = valueParts.join('=').trim(); - } - } - }); -} catch (e) { - console.log('No .env file found or error reading it:', e.message); -} - console.log('PORT from env:', process.env.PORT); import express from 'express'; @@ -76,6 +61,7 @@ import userRoutes from './routes/user.js'; import codexRoutes from './routes/codex.js'; import { initializeDatabase } from './database/db.js'; import { validateApiKey, authenticateToken, authenticateWebSocket } from './middleware/auth.js'; +import { IS_PLATFORM } from './constants/config.js'; // File system watcher for projects folder let projectsWatcher = null; @@ -200,7 +186,7 @@ const wss = new WebSocketServer({ console.log('WebSocket connection attempt to:', info.req.url); // Platform mode: always allow connection - if (process.env.VITE_IS_PLATFORM === 'true') { + if (IS_PLATFORM) { const user = authenticateWebSocket(null); // Will return first user if (!user) { console.log('[WARN] Platform mode: No user found in database'); diff --git a/server/load-env.js b/server/load-env.js new file mode 100644 index 0000000..21280a4 --- /dev/null +++ b/server/load-env.js @@ -0,0 +1,24 @@ +// Load environment variables from .env before other imports execute. +import fs from 'fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +try { + const envPath = path.join(__dirname, '../.env'); + const envFile = fs.readFileSync(envPath, 'utf8'); + envFile.split('\n').forEach(line => { + const trimmedLine = line.trim(); + if (trimmedLine && !trimmedLine.startsWith('#')) { + const [key, ...valueParts] = trimmedLine.split('='); + if (key && valueParts.length > 0 && !process.env[key]) { + process.env[key] = valueParts.join('=').trim(); + } + } + }); +} catch (e) { + console.log('No .env file found or error reading it:', e.message); +} diff --git a/server/middleware/auth.js b/server/middleware/auth.js index 9231e4e..ab12e0c 100644 --- a/server/middleware/auth.js +++ b/server/middleware/auth.js @@ -1,5 +1,6 @@ import jwt from 'jsonwebtoken'; import { userDb } from '../database/db.js'; +import { IS_PLATFORM } from '../constants/config.js'; // Get JWT secret from environment or use default (for development) const JWT_SECRET = process.env.JWT_SECRET || 'claude-ui-dev-secret-change-in-production'; @@ -21,7 +22,7 @@ const validateApiKey = (req, res, next) => { // JWT authentication middleware const authenticateToken = async (req, res, next) => { // Platform mode: use single database user - if (process.env.VITE_IS_PLATFORM === 'true') { + if (IS_PLATFORM) { try { const user = userDb.getFirstUser(); if (!user) { @@ -80,7 +81,7 @@ const generateToken = (user) => { // WebSocket authentication function const authenticateWebSocket = (token) => { // Platform mode: bypass token validation, return first user - if (process.env.VITE_IS_PLATFORM === 'true') { + if (IS_PLATFORM) { try { const user = userDb.getFirstUser(); if (user) { diff --git a/server/routes/agent.js b/server/routes/agent.js index f633034..3ef2620 100644 --- a/server/routes/agent.js +++ b/server/routes/agent.js @@ -11,6 +11,7 @@ import { spawnCursor } from '../cursor-cli.js'; import { queryCodex } from '../openai-codex.js'; import { Octokit } from '@octokit/rest'; import { CLAUDE_MODELS, CURSOR_MODELS, CODEX_MODELS } from '../../shared/modelConstants.js'; +import { IS_PLATFORM } from '../constants/config.js'; const router = express.Router(); @@ -18,7 +19,7 @@ const router = express.Router(); * Middleware to authenticate agent API requests. * * Supports two authentication modes: - * 1. Platform mode (VITE_IS_PLATFORM=true): For managed/hosted deployments where + * 1. Platform mode (IS_PLATFORM=true): For managed/hosted deployments where * authentication is handled by an external proxy. Requests are trusted and * the default user context is used. * @@ -28,7 +29,7 @@ const router = express.Router(); const validateExternalApiKey = (req, res, next) => { // Platform mode: Authentication is handled externally (e.g., by a proxy layer). // Trust the request and use the default user context. - if (process.env.VITE_IS_PLATFORM === 'true') { + if (IS_PLATFORM) { try { const user = userDb.getFirstUser(); if (!user) {