优化服务器启动逻辑,重构应用初始化流程,移除冗余参数类型定义,新增API Client动态加载功能并集成CORS中间件,提升系统可维护性与跨域支持能力

This commit is contained in:
2025-04-30 07:00:49 +00:00
parent 40b9a53610
commit f583d0b2d4
3 changed files with 82 additions and 55 deletions

View File

@@ -226,7 +226,7 @@ export type WithAuth = typeof withAuth;
// 定义模块参数接口
interface ModuleParams {
apiClient: APIClient
app: Hono<{ Variables: Variables }>
app: Hono
moduleDir: string
}

81
server/run.ts Normal file
View File

@@ -0,0 +1,81 @@
// 导入所需模块
import { Hono } from 'hono'
import { APIClient } from '@d8d-appcontainer/api'
import debug from "debug"
import { cors } from 'hono/cors'
// 初始化debug实例
const log = {
app: debug('app:server'),
auth: debug('auth:server'),
api: debug('api:server'),
debug: debug('debug:server')
}
// 启用所有日志
Object.values(log).forEach(logger => logger.enabled = true)
// 初始化 API Client
const getApiClient = async (workspaceKey: string, serverUrl?: string) => {
try {
log.api('正在初始化API Client实例')
const apiClient = await APIClient.getInstance({
scope: 'user',
config: {
serverUrl: serverUrl || Deno.env.get('SERVER_URL') || 'https://app-server.d8d.fun',
workspaceKey: workspaceKey,
type: 'http',
}
})
log.api('API Client初始化成功')
return apiClient
} catch (error) {
log.api('API Client初始化失败:', error)
throw error
}
}
// 创建Hono应用实例
const app = new Hono()
// 注册CORS中间件
app.use('/*', cors())
// 动态加载并运行模板
const runTemplate = async () => {
try {
// 创建基础app实例
const moduleApp = new Hono()
// 初始化API Client
// 注意WORKSPACE_KEY 需要在 多八多(www.d8d.fun) 平台注册并开通工作空间后获取
const workspaceKey = Deno.env.get('WORKSPACE_KEY') || ''
if (!workspaceKey) {
console.warn('未设置WORKSPACE_KEY请前往 多八多(www.d8d.fun) 注册并开通工作空间以获取密钥')
}
const apiClient = await getApiClient(workspaceKey)
// 导入模板主模块
const templateModule = await import('./app.tsx')
if (templateModule.default) {
// 传入必要参数并初始化应用
const appInstance = templateModule.default({
apiClient: apiClient,
app: moduleApp,
moduleDir: './admin-mobile-starter'
})
// 启动服务器
Deno.serve({ port: 8000 }, appInstance.fetch)
console.log('应用已启动,监听端口: 8000')
}
} catch (error) {
console.error('模板加载失败:', error)
}
}
// 执行模板
runTemplate()

View File

@@ -1,54 +0,0 @@
[测试套件] 知识库管理页面测试 (./client/admin/pages_know_info.test.tsx)
[测试用例] 应正确渲染页面元素
- 状态: 通过
- 耗时: 2.000s
[测试用例] 初始加载表格数据
- 状态: 通过
- 耗时: 0.729s
[测试用例] 搜索表单应正常工作
- 状态: 通过
- 耗时: 1.000s
[测试用例] 表格应加载并显示数据
- 状态: 通过
- 耗时: 0.038s
[测试用例] 应能打开添加文章模态框
- 状态: 通过
- 耗时: 0.298s
- 输出内容:
<div class="ant-modal-title" id=":rq:">
添加知识库文章
</div>
[测试用例] 应能完整添加一篇文章
- 状态: 通过
- 耗时: 0.483s
- 输出内容:
<input
aria-required="true"
class="ant-input css-dev-only-do-not-override-1a3rktk ant-input-outlined"
id="title"
placeholder="请输入文章标题"
type="text"
value=""
/>
<textarea
aria-required="true"
class="ant-input css-dev-only-do-not-override-1a3rktk ant-input-outlined"
id="content"
placeholder="请输入文章内容支持Markdown格式"
rows="15"
/>
<span>确 定</span>
hasNewArticle true
[测试总结]
- 总测试数: 1
- 通过数: 1
- 失败数: 0
- 总耗时: 6.000s
- 所有测试通过