Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
SunWuyuan committed Nov 16, 2024
1 parent 39e43fe commit fd431f2
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 140 deletions.
2 changes: 1 addition & 1 deletion .bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const configManager = require("../server/configManager");

// 使用 getConfig 函数获取配置
(async () => {
await configManager.initialize(); // 初始化并加载配置
await configManager.loadConfigsFromDB(); // 初始化并加载配置
console.log("加载网站:", await configManager.getConfig("site.name"));
})();

Expand Down
15 changes: 1 addition & 14 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});*/

// 路由处理...

Expand Down Expand Up @@ -235,6 +221,7 @@ app.get("/check", function (req, res, next) {
code: 200,
});
});

process.on("uncaughtException", function (err) {
console.log("Caught exception: " + err);
});
Expand Down
5 changes: 0 additions & 5 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,3 @@ services:
container_name: zerocat
ports:
- 3000:3000
# volumes:
# - data:/data
#volumes:
# data:
# size: 5Gi
106 changes: 55 additions & 51 deletions server/configManager.js
Original file line number Diff line number Diff line change
@@ -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;
17 changes: 13 additions & 4 deletions server/services/emailService.js
Original file line number Diff line number Diff line change
@@ -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,
},
});

Expand Down
92 changes: 27 additions & 65 deletions views/index.ejs
Original file line number Diff line number Diff line change
@@ -1,89 +1,51 @@
<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<html lang="zh-cmn-Hans" class="mdui-theme-dark">

<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>
<%= global.configManager['site.name'] %>
<%= global.config['site.name'] %>
</title>
<link rel="shortcut icon" href="<%= global.configManager['urls.static'] %>/img/siteicon.png" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta charset="UTF-8" />

<meta name="description" content="ZeroCat是新一代开源编程社区!坚信每一行代码都可以改变世界,我们致力于提供优质的开源编程社区,让开源编程更加容易。" />
<meta name="keyword" content="scratch编程,scratch社区,python编程,python社区,在线编程,编程社区,开源,自托管,开源编程社区,代码,阿尔法营,有道小图灵" />
<meta name="author" content="<%= global.configManager['site.name'] %>" />
<meta name="generator" content="<%= global.configManager['site.domain'] %>" />
<meta name="HandheldFriendly" content="true" />
<meta name="MobileOptimized" content="320" />
<meta name="screen-orientation" content="portrait" />
<meta name="x5-orientation" content="portrait" />
<meta name="renderer" content="webkit" />
<meta name="force-rendering" content="webkit" />
<meta name="format-detection" content="telephone=no" />
<meta name="format-detection" content="date=no" />
<meta name="format-detection" content="address=no" />
<meta content="”telephone"="no”" name="”format-detection”" />
<meta property="og:title"
content="<%= global.configManager['site.name'] %> - <%= global.configManager['site.slogan'] %>" />
<meta property="og:type" content="website" />
<meta property="og:url" content="<%= global.configManager['urls.frontend'] %>" />
<meta property="og:image" content="<%= global.configManager['urls.static'] %>/img/toutu.png" />
<meta name="referrer" content="origin-when-cross-origin" />

<meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1" />

<link rel="stylesheet" href="<%= global.configManager['urls.static'] %>/mdui2/mdui.css" id="mdui_css" />

<meta name="view-transition" content="same-origin" />

<script type="text/javascript" src="<%= global.configManager['urls.static'] %>/js/jquery.3.7.1.js"></script>
<link rel="shortcut icon" href="<%= global.config['urls.static'] %>/img/siteicon.png">
<link rel="stylesheet" href="<%= global.config['urls.static'] %>/mdui2/mdui.css" id="mdui_css">
</head>
<style>
:not(:defined) {
visibility: hidden;
}
</style>

<body class="mdui-theme-dark mdui-theme-accent-blue mdui-loaded">
<body>
<mdui-layout style="height: 100vh">
<link href="<%= global.configManager['urls.static'] %>/font1.css" rel="stylesheet" />
<link href="<%= global.config['urls.static'] %>/font1.css" rel="stylesheet" />

<mdui-layout-main>
<div class="zc-container" style="border-radius: 15px">
<link href="<%= global.configManager['urls.static'] %>/css/webpixels-1.2.6.css" rel="stylesheet" />

<div>
<div class="py-24">
<div class="container max-w-screen-xl">
<div class="row align-items-center">
<div class="col-12 col-lg-5 mb-10 mb-lg-0">
<div class="display-4 mb-5 mt-n16">🚀</div>
<div class="ls-tight font-bolder display-3 mb-5">
<%= global.configManager['site.name'] %>,<br />启动!
</div>
<p class="lead mb-10">
<%= global.configManager['site.slogan'] %>
</p>

<div class="mx-n2">
<mdui-button variant="filled" href="<%= global.configManager['urls.frontend'] %>">立刻加入</mdui-button>
<mdui-button variant="elevated" href="#">了解更多</mdui-button><mdui-list>
<mdui-list-item icon="people" description="qq群号964979747" href="https://qm.qq.com/q/wn3TkPoZ5C"
active rounded>ZeroCat现由厚浪云提供支持</mdui-list-item>
</mdui-list>
</div>
</div>
</div>
</div>
</div>

<mdui-list>
<% const config = JSON.parse(JSON.stringify(global.publicconfig)); %>
<% Object.entries(config).forEach(([key, value]) => { %>
<mdui-list-item
rounded
headline="<%= key %>"
description="<%= typeof value === 'object' ? JSON.stringify(value) : value %>"
></mdui-list-item>
<% }); %>
</mdui-list>

<mdui-button variant="filled" href="<%= global.config['urls.frontend'] %>">立刻加入</mdui-button>
<mdui-button variant="elevated" href="#">了解更多</mdui-button><mdui-list>
<mdui-list-item icon="people" description="qq群号964979747" href="https://qm.qq.com/q/wn3TkPoZ5C" active
rounded>ZeroCat现由厚浪云提供支持</mdui-list-item>
</mdui-list>

</div>
</div>
</mdui-layout-main>
</mdui-layout>
</body>
<script src="<%= global.configManager['urls.static'] %>/mdui2/mdui.global.js"></script>
<script src="<%= global.config['urls.static'] %>/mdui2/mdui.global.js"></script>
<script>
mdui.setColorScheme("#2087fd");
</script>
Expand Down

0 comments on commit fd431f2

Please sign in to comment.