init
This commit is contained in:
163
backend/dist/index.js
vendored
Normal file
163
backend/dist/index.js
vendored
Normal file
@@ -0,0 +1,163 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const node_server_1 = require("@hono/node-server");
|
||||
const hono_1 = require("hono");
|
||||
const cors_1 = require("hono/cors");
|
||||
const logger_1 = require("hono/logger");
|
||||
const config_1 = __importDefault(require("./config"));
|
||||
const auth_1 = __importDefault(require("./routes/auth"));
|
||||
const analytics_1 = __importDefault(require("./routes/analytics"));
|
||||
const community_1 = __importDefault(require("./routes/community"));
|
||||
const posts_1 = __importDefault(require("./routes/posts"));
|
||||
const projectComments_1 = __importDefault(require("./routes/projectComments"));
|
||||
const comments_1 = __importDefault(require("./routes/comments"));
|
||||
const influencers_1 = __importDefault(require("./routes/influencers"));
|
||||
const redis_1 = require("./utils/redis");
|
||||
const clickhouse_1 = require("./utils/clickhouse");
|
||||
const queue_1 = require("./utils/queue");
|
||||
const initDatabase_1 = require("./utils/initDatabase");
|
||||
const swagger_1 = require("./swagger");
|
||||
// Create Hono app
|
||||
const app = new hono_1.Hono();
|
||||
// Middleware
|
||||
app.use('*', (0, logger_1.logger)());
|
||||
app.use('*', (0, cors_1.cors)({
|
||||
origin: '*',
|
||||
allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
||||
allowHeaders: ['Content-Type', 'Authorization'],
|
||||
exposeHeaders: ['Content-Length'],
|
||||
maxAge: 86400,
|
||||
}));
|
||||
// Health check route
|
||||
app.get('/', (c) => {
|
||||
return c.json({
|
||||
status: 'ok',
|
||||
message: 'Promote API is running',
|
||||
version: '1.0.0',
|
||||
});
|
||||
});
|
||||
// 数据库初始化路由
|
||||
app.post('/api/admin/init-db', async (c) => {
|
||||
try {
|
||||
const result = await (0, initDatabase_1.initDatabase)();
|
||||
return c.json({
|
||||
success: result,
|
||||
message: result ? 'Database initialized successfully' : 'Database initialization failed'
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error initializing database:', error);
|
||||
return c.json({
|
||||
success: false,
|
||||
message: 'Error initializing database',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
// 创建测试数据路由
|
||||
app.post('/api/admin/create-sample-data', async (c) => {
|
||||
try {
|
||||
const result = await (0, initDatabase_1.createSampleData)();
|
||||
return c.json({
|
||||
success: result,
|
||||
message: result ? 'Sample data created successfully' : 'Sample data creation failed'
|
||||
});
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Error creating sample data:', error);
|
||||
return c.json({
|
||||
success: false,
|
||||
message: 'Error creating sample data',
|
||||
error: error instanceof Error ? error.message : String(error)
|
||||
}, 500);
|
||||
}
|
||||
});
|
||||
// Routes
|
||||
app.route('/api/auth', auth_1.default);
|
||||
app.route('/api/analytics', analytics_1.default);
|
||||
app.route('/api/community', community_1.default);
|
||||
app.route('/api/posts', posts_1.default);
|
||||
app.route('/api/project-comments', projectComments_1.default);
|
||||
app.route('/api/comments', comments_1.default);
|
||||
app.route('/api/influencers', influencers_1.default);
|
||||
// Swagger UI
|
||||
const swaggerApp = (0, swagger_1.createSwaggerUI)();
|
||||
app.route('', swaggerApp);
|
||||
// Initialize services and start server
|
||||
const startServer = async () => {
|
||||
try {
|
||||
// Connect to Redis
|
||||
try {
|
||||
await (0, redis_1.connectRedis)();
|
||||
console.log('Connected to Redis');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to connect to Redis:', error);
|
||||
console.log('Continuing with mock Redis client...');
|
||||
}
|
||||
// Initialize ClickHouse
|
||||
try {
|
||||
await (0, clickhouse_1.initClickHouse)();
|
||||
console.log('ClickHouse initialized');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to initialize ClickHouse:', error);
|
||||
console.log('Continuing with limited analytics functionality...');
|
||||
}
|
||||
// 检查数据库连接,但不自动初始化或修改数据库
|
||||
try {
|
||||
await (0, initDatabase_1.checkDatabaseConnection)();
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Database connection check failed:', error);
|
||||
console.log('Some features may not work correctly if database is not properly set up');
|
||||
}
|
||||
console.log('NOTICE: Database will NOT be automatically initialized on startup');
|
||||
console.log('Use /api/admin/init-db endpoint to manually initialize the database if needed');
|
||||
// Initialize BullMQ workers
|
||||
let workers;
|
||||
try {
|
||||
workers = (0, queue_1.initWorkers)();
|
||||
console.log('BullMQ workers initialized');
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to initialize BullMQ workers:', error);
|
||||
console.log('Background processing will not be available...');
|
||||
workers = { analyticsWorker: null, notificationsWorker: null };
|
||||
}
|
||||
// Start server
|
||||
const port = Number(config_1.default.port);
|
||||
console.log(`Server starting on port ${port}...`);
|
||||
(0, node_server_1.serve)({
|
||||
fetch: app.fetch,
|
||||
port,
|
||||
});
|
||||
console.log(`Server running at http://localhost:${port}`);
|
||||
console.log(`Swagger UI available at http://localhost:${port}/swagger`);
|
||||
console.log(`Initialize database at http://localhost:${port}/api/admin/init-db (POST)`);
|
||||
console.log(`Create sample data at http://localhost:${port}/api/admin/create-sample-data (POST)`);
|
||||
// Handle graceful shutdown
|
||||
const shutdown = async () => {
|
||||
console.log('Shutting down server...');
|
||||
// Close workers if they exist
|
||||
if (workers.analyticsWorker) {
|
||||
await workers.analyticsWorker.close();
|
||||
}
|
||||
if (workers.notificationsWorker) {
|
||||
await workers.notificationsWorker.close();
|
||||
}
|
||||
process.exit(0);
|
||||
};
|
||||
process.on('SIGINT', shutdown);
|
||||
process.on('SIGTERM', shutdown);
|
||||
}
|
||||
catch (error) {
|
||||
console.error('Failed to start server:', error);
|
||||
process.exit(1);
|
||||
}
|
||||
};
|
||||
// Start the server
|
||||
startServer();
|
||||
Reference in New Issue
Block a user