fix(docker): removes sh scripts to fix format error on windows, moves script logic to start.js

This commit is contained in:
sauravniraula 2025-08-01 13:57:15 +05:45
parent d8586f0a7a
commit 41cd7e2b61
No known key found for this signature in database
GPG key ID: 60FCC1B5A5E83326
7 changed files with 98 additions and 46 deletions

View file

@ -1,12 +1,10 @@
.venv
.env
.next
node_modules
out
build
.git
.gitignore
tmp
debug
.fastembed_cache
generated_models
servers/fastapi/tmp
servers/fastapi/debug
servers/nextjs/node_modules

View file

@ -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"]
CMD ["node", "/app/start.js"]

View file

@ -45,4 +45,4 @@ COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
# Start the servers
CMD ["/bin/bash", "/app/docker-dev-start.sh"]
CMD ["node", "/app/start.js", "--dev"]

View file

@ -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}

View file

@ -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

View file

@ -1,8 +0,0 @@
#!/bin/bash
echo "Starting production server..."
ollama serve &
service nginx start
service redis-server start
node /app/start.js

106
start.js
View file

@ -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();