|
|
@@ -4,83 +4,48 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
};
|
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
exports.clickhouseService = exports.redisClient = void 0;
|
|
|
-// oms/src/app.ts
|
|
|
const dotenv_1 = __importDefault(require("dotenv"));
|
|
|
-dotenv_1.default.config(); // 在读取环境变量之前加载 .env 文件
|
|
|
-const express_1 = __importDefault(require("express")); // 导入 NextFunction
|
|
|
-const redis_1 = require("redis");
|
|
|
+dotenv_1.default.config();
|
|
|
+const express_1 = __importDefault(require("express"));
|
|
|
+const http_1 = __importDefault(require("http")); // For controlling keepAliveTimeout
|
|
|
const path_1 = __importDefault(require("path"));
|
|
|
const apiRoutes_1 = __importDefault(require("./routes/apiRoutes"));
|
|
|
-const clickhouseService_1 = require("./services/clickhouseService");
|
|
|
-const database_1 = require("./database");
|
|
|
+const clients_1 = require("./services/clients"); // 导入新的初始化函数和客户端
|
|
|
+Object.defineProperty(exports, "redisClient", { enumerable: true, get: function () { return clients_1.redisClient; } });
|
|
|
+Object.defineProperty(exports, "clickhouseService", { enumerable: true, get: function () { return clients_1.clickhouseService; } });
|
|
|
const app = (0, express_1.default)();
|
|
|
const port = process.env.PORT || 3000;
|
|
|
-const mongoUri = process.env.MONGO_URI || "mongodb://oms:oms123.@localhost:27717/omsdb?authSource=admin";
|
|
|
-const redisUri = process.env.REDIS_URI || "redis://redis:6379";
|
|
|
-const clickhouseHost = process.env.CLICKHOUSE_HOST || "http://localhost:8123"; // 👈 ClickHouse 主机
|
|
|
-const clickhouseDatabase = process.env.CLICKHOUSE_DATABASE || "omsdb"; // 👈 ClickHouse 数据库名
|
|
|
-const clickhouseUser = process.env.CLICKHOUSE_USER;
|
|
|
-const clickhousePassword = process.env.CLICKHOUSE_PASSWORD;
|
|
|
-// Initialize ClickhouseService
|
|
|
-const clickhouseService = new clickhouseService_1.ClickhouseService(clickhouseHost, clickhouseDatabase, clickhouseUser, clickhousePassword);
|
|
|
-exports.clickhouseService = clickhouseService;
|
|
|
-const clickhouseTableName = "events"; // ClickHouse 日志表的名称
|
|
|
-// MongoDB connection
|
|
|
-(0, database_1.connectToDatabase)();
|
|
|
-// Redis client
|
|
|
-// Using the modern 'redis' package client setup
|
|
|
-const redisClient = (0, redis_1.createClient)({ url: redisUri }); // 👈 使用解构后的 createClient
|
|
|
-exports.redisClient = redisClient;
|
|
|
-redisClient.on("connect", () => console.log("Connected to Redis"));
|
|
|
-redisClient.on("error", (err) => console.error("Redis connection error:", err));
|
|
|
-// Connect to Redis when the application starts
|
|
|
-(async () => {
|
|
|
- try {
|
|
|
- await redisClient.connect();
|
|
|
- // 确保 ClickHouse 表在应用启动时存在
|
|
|
- await clickhouseService.ensureTable(clickhouseTableName);
|
|
|
- }
|
|
|
- catch (err) {
|
|
|
- console.error("Failed to connect to essential services:", err);
|
|
|
- process.exit(1); // 如果连接失败,退出应用
|
|
|
- }
|
|
|
-})();
|
|
|
// Middleware
|
|
|
-// 服务部署在反向代理(如 Nginx)后面,需设置此项以正确获取客户端 IP
|
|
|
app.set("trust proxy", true);
|
|
|
app.use(express_1.default.json());
|
|
|
-// 新增中间件:为每个API请求添加日志打印
|
|
|
-app.use((req, res, next) => {
|
|
|
- console.log(`[API Request] ${new Date().toISOString()} - ${req.method} ${req.originalUrl}`);
|
|
|
- next();
|
|
|
-});
|
|
|
// API routes
|
|
|
app.use("/api", apiRoutes_1.default);
|
|
|
-// 动态设置公共目录和Angular应用路径
|
|
|
+// Static and SPA routing... (Keep your existing code here)
|
|
|
let publicPath;
|
|
|
let angularAppPath;
|
|
|
if (process.env.NODE_ENV === "production") {
|
|
|
- // 如果在生产环境中,从 dist 目录往上一层找到 public
|
|
|
publicPath = path_1.default.resolve(__dirname, "..", "..", "public");
|
|
|
angularAppPath = path_1.default.join(publicPath, "app");
|
|
|
}
|
|
|
else {
|
|
|
- // 如果在开发环境中,public 目录就在当前目录的上一层(相对于src)
|
|
|
publicPath = path_1.default.resolve(__dirname, "..", "public");
|
|
|
angularAppPath = path_1.default.join(publicPath, "app");
|
|
|
}
|
|
|
console.log(process.env.NODE_ENV);
|
|
|
console.log(`publicPath: ${publicPath}`);
|
|
|
console.log(`angularAppPath: ${angularAppPath}`);
|
|
|
-// 服务 Angular 应用的静态文件
|
|
|
-// Express 会在 public 目录下寻找请求的静态文件,如 /app/main.js
|
|
|
app.use(express_1.default.static(publicPath));
|
|
|
-// 作为最终的备用,处理所有未被前面路由处理的请求
|
|
|
-// 这是最标准的单页面应用(SPA)路由处理方式
|
|
|
app.use((req, res) => {
|
|
|
res.sendFile(path_1.default.join(angularAppPath, "index.html"));
|
|
|
});
|
|
|
// Start the server
|
|
|
-app.listen(port, () => {
|
|
|
- console.log(`OMS Backend server listening on port ${port}`);
|
|
|
-});
|
|
|
+async function startServer() {
|
|
|
+ await (0, clients_1.initializeClients)(); // 先初始化所有客户端
|
|
|
+ const server = http_1.default.createServer(app);
|
|
|
+ // server.keepAliveTimeout = 61 * 1000;
|
|
|
+ // server.headersTimeout = 65 * 1000;
|
|
|
+ server.listen(port, () => {
|
|
|
+ console.log(`OMS Backend server listening on port ${port}`);
|
|
|
+ });
|
|
|
+}
|
|
|
+startServer();
|