Skip to content

Commit

Permalink
feat(service): add database config
Browse files Browse the repository at this point in the history
  • Loading branch information
alongw committed Sep 6, 2024
1 parent 77d468f commit b136594
Show file tree
Hide file tree
Showing 5 changed files with 590 additions and 2 deletions.
4 changes: 4 additions & 0 deletions apps/service/src/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import figlet from 'figlet'
import express from 'express'
import { logger } from '@nya-account/common'

// 显示启动信息
Expand All @@ -8,3 +9,6 @@ logger.info('-----------------------------------------------------')
logger.info('')
logger.info('Nya Account 正在启动...')
logger.info('Powered by ALONGW | https://www.alongw.cn | https://github.com/alongw/sso')

// 初始化 express
const app = express()
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@eslint/eslintrc": "^3.1.0",
"@eslint/js": "^9.9.0",
"@simplewebauthn/types": "^10.0.0",
"@types/express": "^4.17.21",
"@types/figlet": "^1.5.8",
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.4.1",
Expand All @@ -35,6 +36,7 @@
},
"dependencies": {
"@nya-account/common": "workspace:^",
"express": "^4.19.2",
"figlet": "^1.7.0",
"fs-extra": "^11.2.0",
"log4js": "^6.9.1",
Expand Down
5 changes: 4 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"name": "@nya-account/common",
"main": "./src/index.ts"
"main": "./src/index.ts",
"dependencies": {
"@nya-account/db": "workspace:^"
}
}
47 changes: 47 additions & 0 deletions packages/common/src/modules/dbConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Config } from '@nya-account/db'
import { logger } from '@nya-account/common'

const defaultConfig = {
global_encode_secret: 'f881c6163132cd554088b56a62d4a1ed',
service_base_url: '/api',
service_jwt_unless: ['/public'],
service_jwt_secret: 'f881c6163132cd554088b56a62d4a1ed',
service_listen_port: 10086,
node_application_jwt_secret: 'f881c6163132cd554088b56a62d4a1ed',
node_base_url: '/node',
node_listen_port: 10010,
node_create_user_uuid_key: 'f881c6163132cd554088b56a62d4a1ed',
web_authn_rpid: 'localhost',
web_authn_rporigin: 'https://localhost',
web_authn_rpname: 'Nya Account'
}

export const getConfig = async <T extends keyof typeof defaultConfig>(key: T) => {
// 判断配置字段是否存在 defaultConfig 中
if (!defaultConfig[key]) {
return null
}

// 从数据库中查找配置
try {
const [config, created] = await Config.findOrCreate({
where: {
key: key
},
defaults: {
key: key,
value: JSON.stringify(defaultConfig[key]),
id: undefined as unknown as number
}
})

if (created) {
logger.warn(`在数据库中未找到配置项 ${key},已自动创建`)
}

return JSON.parse(config.toJSON().value) as (typeof defaultConfig)[T]
} catch (error) {
logger.error(`获取配置项 ${key} 时出现错误:${error}`)
return null
}
}
Loading

0 comments on commit b136594

Please sign in to comment.