%
This commit is contained in:
83
server-init.js
Normal file
83
server-init.js
Normal file
@@ -0,0 +1,83 @@
|
||||
// server-init.js
|
||||
const path = require('path');
|
||||
const fs = require('fs');
|
||||
const { setupUploadMiddleware } = require('./upload-middleware');
|
||||
const { setupTaskEndpoints } = require('./task-endpoints');
|
||||
|
||||
async function initializeServer(app) {
|
||||
console.log('🚀 Инициализация сервера...');
|
||||
|
||||
try {
|
||||
const { initializeDatabase, getDb, isInitialized } = require('./database');
|
||||
const authService = require('./auth');
|
||||
const postgresLogger = require('./postgres');
|
||||
|
||||
// 1. Инициализируем базу данных
|
||||
console.log('🔧 Инициализация базы данных...');
|
||||
await initializeDatabase();
|
||||
|
||||
// 2. Получаем объект БД
|
||||
const db = getDb();
|
||||
console.log('✅ База данных готова');
|
||||
|
||||
// 3. Настраиваем authService с БД
|
||||
authService.setDatabase(db);
|
||||
console.log('✅ Сервис аутентификации готов');
|
||||
|
||||
// 4. Настраиваем загрузку файлов
|
||||
const upload = setupUploadMiddleware();
|
||||
console.log('✅ Middleware загрузки файлов настроен');
|
||||
|
||||
// 5. Настраиваем endpoint'ы для задач
|
||||
setupTaskEndpoints(app, db, upload);
|
||||
console.log('✅ Endpoint\'ы задач настроены');
|
||||
|
||||
// 6. Загружаем админ роутер
|
||||
try {
|
||||
const adminRouter = require('./admin-server');
|
||||
console.log('Admin router loaded:', adminRouter);
|
||||
console.log('Type:', typeof adminRouter);
|
||||
|
||||
if (adminRouter && typeof adminRouter === 'function') {
|
||||
app.use(adminRouter);
|
||||
console.log('✅ Админ роутер подключен');
|
||||
} else {
|
||||
console.error('❌ Admin router is not a valid middleware function');
|
||||
// Создаем заглушку, чтобы сервер работал
|
||||
const express = require('express');
|
||||
const stubRouter = express.Router();
|
||||
stubRouter.get('*', (req, res) => {
|
||||
res.status(501).json({ error: 'Admin router not available' });
|
||||
});
|
||||
app.use(stubRouter);
|
||||
console.log('⚠️ Используется заглушка для админ роутера');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('❌ Ошибка загрузки админ роутера:', error.message);
|
||||
console.error('Stack:', error.stack);
|
||||
|
||||
// Создаем заглушку, чтобы сервер не падал
|
||||
const express = require('express');
|
||||
const stubRouter = express.Router();
|
||||
stubRouter.get('*', (req, res) => {
|
||||
res.status(503).json({
|
||||
error: 'Admin panel temporarily unavailable',
|
||||
message: error.message
|
||||
});
|
||||
});
|
||||
app.use(stubRouter);
|
||||
console.log('⚠️ Создана заглушка для админ роутера из-за ошибки');
|
||||
}
|
||||
|
||||
console.log('✅ Сервер полностью инициализирован');
|
||||
|
||||
return { db, upload };
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Ошибка инициализации сервера:', error.message);
|
||||
console.error(error.stack);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { initializeServer };
|
||||
Reference in New Issue
Block a user