diff --git a/.dockerignore b/.dockerignore index dcf9035b..4fe36c03 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,12 +1,10 @@ .venv .env .next -node_modules out build .git .gitignore -tmp -debug -.fastembed_cache -generated_models \ No newline at end of file +servers/fastapi/tmp +servers/fastapi/debug +servers/nextjs/node_modules \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index a3fc8e44..8b3ef00b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -49,11 +49,8 @@ COPY start.js LICENSE NOTICE ./ # Copy nginx configuration COPY nginx.conf /etc/nginx/nginx.conf -# Copy start script -COPY docker-start.sh /app/docker-start.sh - # Expose the port EXPOSE 80 # Start the servers -CMD ["/bin/bash", "/app/docker-start.sh"] \ No newline at end of file +CMD ["node", "/app/start.js"] \ No newline at end of file diff --git a/Dockerfile.dev b/Dockerfile.dev index db61b4b7..e71d4e02 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -45,4 +45,4 @@ COPY nginx.conf /etc/nginx/nginx.conf EXPOSE 80 # Start the servers -CMD ["/bin/bash", "/app/docker-dev-start.sh"] \ No newline at end of file +CMD ["node", "/app/start.js", "--dev"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 81239bba..1442351d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -68,7 +68,6 @@ services: - .:/app - ./app_data:/app_data environment: - - NODE_ENV=development - CAN_CHANGE_KEYS=${CAN_CHANGE_KEYS} - LLM=${LLM} - OPENAI_API_KEY=${OPENAI_API_KEY} @@ -101,7 +100,6 @@ services: - .:/app - ./app_data:/app_data environment: - - NODE_ENV=development - CAN_CHANGE_KEYS=${CAN_CHANGE_KEYS} - LLM=${LLM} - OPENAI_API_KEY=${OPENAI_API_KEY} diff --git a/docker-dev-start.sh b/docker-dev-start.sh deleted file mode 100644 index bd6ae2ad..00000000 --- a/docker-dev-start.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/bash - -echo "Starting development server..." - -if [ -d "/node_dependencies/node_modules" ]; then - rm -rf /app/servers/nextjs/node_modules - mv /node_dependencies/node_modules /app/servers/nextjs -fi - -ollama serve & -service nginx start -service redis-server start -node /app/start.js diff --git a/docker-start.sh b/docker-start.sh deleted file mode 100644 index 22c80b91..00000000 --- a/docker-start.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -echo "Starting production server..." - -ollama serve & -service nginx start -service redis-server start -node /app/start.js diff --git a/start.js b/start.js index 6eadf2ae..df576d53 100644 --- a/start.js +++ b/start.js @@ -1,34 +1,57 @@ /* This script starts the FastAPI and Next.js servers, setting up user configuration if necessary. It reads environment variables to configure API keys and other settings, ensuring that the user configuration file is created if it doesn't exist. The script also handles the starting of both servers and keeps the Node.js process alive until one of the servers exits. */ -const path = require('path'); -const { spawn } = require('child_process'); -const fs = require('fs'); +import { join, dirname } from 'path'; +import { fileURLToPath } from 'url'; +import { spawn } from 'child_process'; +import { existsSync, mkdirSync, rmSync, cpSync, readFileSync, writeFileSync } from 'fs'; -const fastapiDir = path.join(__dirname, 'servers/fastapi'); -const nextjsDir = path.join(__dirname, 'servers/nextjs'); +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); -const isDev = process.env.NODE_ENV === 'development'; +const fastapiDir = join(__dirname, 'servers/fastapi'); +const nextjsDir = join(__dirname, 'servers/nextjs'); + +const args = process.argv.slice(2); +const hasDevArg = args.includes('--dev') || args.includes('-d'); +const isDev = hasDevArg; const canChangeKeys = process.env.CAN_CHANGE_KEYS !== 'false'; const fastapiPort = 8000; const nextjsPort = 3000; -const userConfigPath = path.join(process.env.APP_DATA_DIRECTORY, 'userConfig.json'); -const userDataDir = path.dirname(userConfigPath); +const userConfigPath = join(process.env.APP_DATA_DIRECTORY, 'userConfig.json'); +const userDataDir = dirname(userConfigPath); // Create user_data directory if it doesn't exist -if (!fs.existsSync(userDataDir)) { - fs.mkdirSync(userDataDir, { recursive: true }); +if (!existsSync(userDataDir)) { + mkdirSync(userDataDir, { recursive: true }); } +// Setup node_modules for development +const setupNodeModules = () => { + console.log('Setting up node_modules for development...'); + const nodeDependenciesPath = '/node_dependencies/node_modules'; + const nextjsNodeModulesPath = join(nextjsDir, 'node_modules'); + + if (existsSync(nodeDependenciesPath)) { + if (existsSync(nextjsNodeModulesPath)) { + rmSync(nextjsNodeModulesPath, { recursive: true, force: true }); + } + + cpSync(nodeDependenciesPath, nextjsNodeModulesPath, { recursive: true }); + console.log('Copied node_modules from /node_dependencies to Next.js server directory'); + } +}; + + process.env.USER_CONFIG_PATH = userConfigPath; //? UserConfig is only setup if API Keys can be changed const setupUserConfigFromEnv = () => { let existingConfig = {}; - if (fs.existsSync(userConfigPath)) { - existingConfig = JSON.parse(fs.readFileSync(userConfigPath, 'utf8')); + if (existsSync(userConfigPath)) { + existingConfig = JSON.parse(readFileSync(userConfigPath, 'utf8')); } if (!['ollama', 'openai', 'google'].includes(existingConfig.LLM)) { @@ -55,7 +78,7 @@ const setupUserConfigFromEnv = () => { USE_CUSTOM_URL: process.env.USE_CUSTOM_URL || existingConfig.USE_CUSTOM_URL, }; - fs.writeFileSync(userConfigPath, JSON.stringify(userConfig)); + writeFileSync(userConfigPath, JSON.stringify(userConfig)); } const startServers = async () => { @@ -87,17 +110,74 @@ const startServers = async () => { console.error("Next.js process failed to start:", err); }); + const ollamaProcess = spawn( + "ollama", + ["serve"], + { + cwd: "/", + stdio: "inherit", + env: process.env, + } + ); + + ollamaProcess.on("error", err => { + console.error("Ollama process failed to start:", err); + }); + + const redisProcess = spawn( + "redis-server", + [], + { + cwd: "/", + stdio: "inherit", + env: process.env, + } + ); + + redisProcess.on("error", err => { + console.error("Redis process failed to start:", err); + }); + // Keep the Node process alive until both servers exit const exitCode = await Promise.race([ new Promise(resolve => fastApiProcess.on("exit", resolve)), new Promise(resolve => nextjsProcess.on("exit", resolve)), + new Promise(resolve => ollamaProcess.on("exit", resolve)), + new Promise(resolve => redisProcess.on("exit", resolve)), ]); console.log(`One of the processes exited. Exit code: ${exitCode}`); process.exit(exitCode); }; +// Start nginx service +const startNginx = () => { + const nginxProcess = spawn('service', ['nginx', 'start'], { + stdio: 'inherit', + env: process.env, + }); + + nginxProcess.on('error', err => { + console.error('Nginx process failed to start:', err); + }); + + nginxProcess.on('exit', (code) => { + if (code === 0) { + console.log('Nginx started successfully'); + } else { + console.error(`Nginx failed to start with exit code: ${code}`); + } + }); +}; + +if (isDev) { + setupNodeModules(); +} + if (canChangeKeys) { setupUserConfigFromEnv(); } + startServers(); + +startNginx(); \ No newline at end of file