-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
97 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,8 +8,3 @@ services: | |
container_name: zerocat | ||
ports: | ||
- 3000:3000 | ||
# volumes: | ||
# - data:/data | ||
#volumes: | ||
# data: | ||
# size: 5Gi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters