2
0

run_app.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // 导入所需模块
  2. import { Hono } from 'hono'
  3. import { APIClient } from '@d8d-appcontainer/api'
  4. import { Auth } from '@d8d-appcontainer/auth';
  5. import debug from "debug"
  6. import { cors } from 'hono/cors'
  7. import { Server } from "socket.io"
  8. import httpServer from './app.tsx'
  9. import { setupSocketIO } from './router_io.ts'
  10. // 初始化debug实例
  11. const log = {
  12. app: debug('app:server'),
  13. auth: debug('auth:server'),
  14. api: debug('api:server'),
  15. debug: debug('debug:server')
  16. }
  17. // 启用所有日志
  18. Object.values(log).forEach(logger => logger.enabled = true)
  19. // 初始化 API Client
  20. const getApiClient = async (workspaceKey: string, serverUrl?: string) => {
  21. try {
  22. log.api('正在初始化API Client实例')
  23. const apiClient = await APIClient.getInstance({
  24. scope: 'user',
  25. config: {
  26. serverUrl: serverUrl || Deno.env.get('SERVER_URL') || 'https://app-server.d8d.fun',
  27. workspaceKey: workspaceKey,
  28. type: 'http',
  29. }
  30. })
  31. log.api('API Client初始化成功')
  32. return apiClient
  33. } catch (error) {
  34. log.api('API Client初始化失败:', error)
  35. throw error
  36. }
  37. }
  38. // 初始化Auth实例
  39. const initAuth = async (apiClient: APIClient) => {
  40. try {
  41. log.auth('正在初始化Auth实例')
  42. const auth = new Auth(apiClient as any, {
  43. jwtSecret: Deno.env.get("JWT_SECRET") || 'your-jwt-secret-key',
  44. initialUsers: [],
  45. storagePrefix: '',
  46. userTable: 'users',
  47. fieldNames: {
  48. id: 'id',
  49. username: 'username',
  50. password: 'password',
  51. phone: 'phone',
  52. email: 'email',
  53. is_disabled: 'is_disabled',
  54. is_deleted: 'is_deleted'
  55. },
  56. tokenExpiry: 24 * 60 * 60,
  57. refreshTokenExpiry: 7 * 24 * 60 * 60
  58. })
  59. log.auth('Auth实例初始化完成')
  60. return auth
  61. } catch (error) {
  62. log.auth('Auth初始化失败:', error)
  63. throw error
  64. }
  65. }
  66. // 初始化API Client
  67. // 注意:WORKSPACE_KEY 需要在 多八多(www.d8d.fun) 平台注册并开通工作空间后获取
  68. const workspaceKey = Deno.env.get('WORKSPACE_KEY') || ''
  69. if (!workspaceKey) {
  70. console.warn('未设置WORKSPACE_KEY,请前往 多八多(www.d8d.fun) 注册并开通工作空间以获取密钥')
  71. }
  72. const apiClient = await getApiClient(workspaceKey)
  73. const auth = await initAuth(apiClient);
  74. // 创建Hono应用实例
  75. const app = new Hono()
  76. // 注册CORS中间件
  77. app.use('/*', cors())
  78. // 创建Socket.IO实例
  79. const io = new Server({
  80. cors: {
  81. origin: "*",
  82. methods: ["GET", "POST"],
  83. credentials: true
  84. }
  85. })
  86. setupSocketIO({io, auth, apiClient});
  87. // 动态加载并运行模板
  88. const runTemplate = () => {
  89. try {
  90. // 创建基础app实例
  91. const moduleApp = new Hono()
  92. // 传入必要参数并初始化应用
  93. const appInstance = httpServer({
  94. apiClient: apiClient,
  95. app: moduleApp,
  96. moduleDir: './',
  97. auth
  98. })
  99. // 启动服务器
  100. Deno.serve({
  101. handler: io.handler(async (req) => {
  102. return await appInstance.fetch(req) || new Response(null, { status: 404 });
  103. }),
  104. port: 8080
  105. })
  106. console.log('应用已启动,监听端口: 8080')
  107. } catch (error) {
  108. console.error('模板加载失败:', error)
  109. }
  110. }
  111. // 执行模板
  112. runTemplate()