замечания 3
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
const express = require('express');
|
||||
const cors = require('cors');
|
||||
const dotenv = require('dotenv');
|
||||
const connectDB = require('./config/db');
|
||||
const { runMigrations } = require('./scripts/run-migrations');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// Загрузить переменные окружения
|
||||
dotenv.config();
|
||||
@@ -15,7 +15,7 @@ if (process.env.DEV === 'true') {
|
||||
console.log('ℹ️ DEBUG MODE ENABLED - All logs are visible');
|
||||
}
|
||||
|
||||
// Импортировать маршруты
|
||||
// Импортировать маршруты - прямые пути без path.join и __dirname
|
||||
const authRoutes = require('./routes/auth');
|
||||
const companiesRoutes = require('./routes/companies');
|
||||
const messagesRoutes = require('./routes/messages');
|
||||
@@ -28,34 +28,15 @@ const buyProductsRoutes = require('./routes/buyProducts');
|
||||
const requestsRoutes = require('./routes/requests');
|
||||
const homeRoutes = require('./routes/home');
|
||||
|
||||
const connectDB = require('./config/db');
|
||||
|
||||
const app = express();
|
||||
|
||||
// Подключить MongoDB и запустить миграции при инициализации
|
||||
// Подключить MongoDB при инициализации
|
||||
let dbConnected = false;
|
||||
let migrationsCompleted = false;
|
||||
|
||||
const initializeApp = async () => {
|
||||
try {
|
||||
await connectDB().then(() => {
|
||||
dbConnected = true;
|
||||
});
|
||||
|
||||
// Запустить миграции после успешного подключения
|
||||
if (dbConnected) {
|
||||
try {
|
||||
await runMigrations(false);
|
||||
migrationsCompleted = true;
|
||||
} catch (migrationError) {
|
||||
console.error('⚠️ Migrations failed but app will continue:', migrationError.message);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('Error during app initialization:', err);
|
||||
}
|
||||
};
|
||||
|
||||
// Запустить инициализацию
|
||||
initializeApp();
|
||||
connectDB().then(() => {
|
||||
dbConnected = true;
|
||||
});
|
||||
|
||||
// Middleware
|
||||
app.use(cors());
|
||||
@@ -84,13 +65,19 @@ app.use((req, res, next) => {
|
||||
const delay = (ms = 300) => (req, res, next) => setTimeout(next, ms);
|
||||
app.use(delay());
|
||||
|
||||
// Статика для загруженных файлов
|
||||
const uploadsRoot = path.join(__dirname, '..', '..', 'remote-assets', 'uploads');
|
||||
if (!fs.existsSync(uploadsRoot)) {
|
||||
fs.mkdirSync(uploadsRoot, { recursive: true });
|
||||
}
|
||||
app.use('/uploads', express.static(uploadsRoot));
|
||||
|
||||
// Health check endpoint
|
||||
app.get('/health', (req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
api: 'running',
|
||||
database: dbConnected ? 'mongodb' : 'mock',
|
||||
migrations: migrationsCompleted ? 'completed' : 'pending',
|
||||
timestamp: new Date().toISOString()
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user