From fd431f2139a5ca9522cd52eadcfecd086d1784d6 Mon Sep 17 00:00:00 2001 From: SunWuyuan <1847261658@qq.com> Date: Sat, 16 Nov 2024 17:52:55 +0800 Subject: [PATCH] 1 --- .bin/www | 2 +- app.js | 15 +---- docker-compose.yml | 5 -- server/configManager.js | 106 +++++++++++++++++--------------- server/services/emailService.js | 17 +++-- views/index.ejs | 92 ++++++++------------------- 6 files changed, 97 insertions(+), 140 deletions(-) diff --git a/.bin/www b/.bin/www index 186db6f..c685ceb 100644 --- a/.bin/www +++ b/.bin/www @@ -9,7 +9,7 @@ const configManager = require("../server/configManager"); // 使用 getConfig 函数获取配置 (async () => { - await configManager.initialize(); // 初始化并加载配置 + await configManager.loadConfigsFromDB(); // 初始化并加载配置 console.log("加载网站:", await configManager.getConfig("site.name")); })(); diff --git a/app.js b/app.js index c1ea306..1d54f41 100644 --- a/app.js +++ b/app.js @@ -31,20 +31,6 @@ const sdk = new opentelemetry.NodeSDK({ }); sdk.start(); */ -/* -const express = require('express'); -const app = express(); -const configManager = require('./configManager'); - -app.use(async (req, res, next) => { - try { - await configManager.initialize(); // 确保每个请求前初始化 - next(); - } catch (error) { - console.error('Middleware initialization error:', error); - res.status(500).send('Internal Server Error'); - } -});*/ // 路由处理... @@ -235,6 +221,7 @@ app.get("/check", function (req, res, next) { code: 200, }); }); + process.on("uncaughtException", function (err) { console.log("Caught exception: " + err); }); diff --git a/docker-compose.yml b/docker-compose.yml index 822c21b..efdcc63 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,8 +8,3 @@ services: container_name: zerocat ports: - 3000:3000 -# volumes: -# - data:/data -#volumes: -# data: -# size: 5Gi diff --git a/server/configManager.js b/server/configManager.js index c5b9f44..bab376c 100644 --- a/server/configManager.js +++ b/server/configManager.js @@ -1,70 +1,74 @@ // configManager.js -const { PrismaClient } = require('@prisma/client'); +const { PrismaClient } = require("@prisma/client"); class ConfigManager { - constructor() { - this.prisma = new PrismaClient(); - this.config = {}; - } - - async loadAllConfigs() { - const configs = await this.prisma.ow_config.findMany(); - configs.forEach(({ key, value }) => { - this.config[key] = value; - }); - global.configManager = this.config - - } + constructor() { + this.prisma = new PrismaClient(); + } - initialize() { - return this.loadAllConfigs(); // 将 initialize 方法定义为加载所有配置 - } + async loadConfigsFromDB() { + // Fetch all configurations from the database + const configs = await this.prisma.ow_config.findMany(); - async getConfig(key) { - //console.log(this.config) + // Internal configurations + global.config = {}; + configs.forEach(({ key, value }) => { + global.config[key] = value; + }); - // 检查值是否已经缓存 - if (this.config[key]) { - return this.config[key]; - } - // 如果未缓存,则从数据库获取 - return await this.getConfigFromDB(key); - } + // Public configurations + global.publicconfig = {}; + configs.forEach(({ key, value, is_public }) => { + if (is_public == 1) { + global.publicconfig[key] = value; + } + }); - getConfigSync(key) { - //console.log(this.config) + // Configuration information + global.configinfo = configs; - // 检查值是否已经缓存 - if (this.config[key]) { - return this.config[key]; - } + console.log(global.configinfo); // Log the updated config info + } - // 如果未缓存,直接从数据库获取 - const config = this.prisma.ow_config.findFirst({ - where: { key: key } - }); - this.config[key] = config ? config.value : null; - return config ? config.value : null; + async getConfig(key) { + // Check if the value is already cached + if (global.config && global.config[key]) { + return global.config[key]; } - - async loadAndCacheAll() { - await this.loadAllConfigs(); + // If not cached, fetch from the database + await this.loadConfigsFromDB(); + // If not cached, fetch from the database + if (global.config && global.config[key]) { + return global.config[key]; } + throw new Error(`Config key "${key}" not found.`); + } + async getPublicConfigs(key) { + // Check if the value is already cached + if (global.publicconfig && global.publicconfig[key]) { + return global.publicconfig[key]; + } + // If not cached, fetch from the database + await this.loadConfigsFromDB(); - async getConfigFromDB(key) { - const config = await this.prisma.ow_config.findFirst({ - where: { key: key } - }); + if (global.publicconfig && global.publicconfig[key]) { + return global.publicconfig[key]; + } + throw new Error(`Config key "${key}" not found.`); + } - if (config) { - this.config[key] = config.value; // 缓存获取的值 - return config.value; - } + async getConfigFromDB(key) { + await this.loadConfigsFromDB(); - throw new Error(`Config key "${key}" not found.`); + if (global.config && global.config[key]) { + return global.config[key]; } + + throw new Error(`Config key "${key}" not found.`); + } } -// 使用单例模式 + +// Create a singleton instance of the ConfigManager class const configManagerInstance = new ConfigManager(); module.exports = configManagerInstance; diff --git a/server/services/emailService.js b/server/services/emailService.js index f980e52..0680cf4 100644 --- a/server/services/emailService.js +++ b/server/services/emailService.js @@ -1,13 +1,22 @@ const configManager = require("../configManager"); const nodemailer = require('nodemailer'); - +let service, user, pass + configManager.getConfig('mail.service').then((res) => { + service = res +}); + configManager.getConfig('mail.user').then((res) => { + user = res +}); + configManager.getConfig('mail.pass').then((res) => { + pass = res +}); const transporter = nodemailer.createTransport({ - service: configManager.getConfigSync('mail.service'), + service: service, secure: true, auth: { - user: configManager.getConfigSync('mail.user'), - pass: configManager.getConfigSync('mail.pass'), + user: user, + pass: pass, }, }); diff --git a/views/index.ejs b/views/index.ejs index e62e223..879fc6b 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,44 +1,14 @@ - +
- - + +- <%= global.configManager['site.slogan'] %> -
-