增加socketio 路由 支持

This commit is contained in:
yourname
2025-05-15 08:40:09 +00:00
parent b879ad4f7c
commit dea7ec5316
8 changed files with 350 additions and 58 deletions

View File

@@ -1,11 +1,12 @@
// 导入所需模块
import { Hono } from 'hono'
import { APIClient } from '@d8d-appcontainer/api'
import { Auth } from '@d8d-appcontainer/auth';
import debug from "debug"
import { cors } from 'hono/cors'
import { Server } from "socket.io"
import httpServer from './app.tsx'
import { setupSocketIO } from './routes_socketio.ts'
import { setupSocketIO } from './router_io.ts'
// 初始化debug实例
const log = {
@@ -39,6 +40,37 @@ const getApiClient = async (workspaceKey: string, serverUrl?: string) => {
throw error
}
}
// 初始化Auth实例
const initAuth = async (apiClient: APIClient) => {
try {
log.auth('正在初始化Auth实例')
const auth = new Auth(apiClient as any, {
jwtSecret: Deno.env.get("JWT_SECRET") || 'your-jwt-secret-key',
initialUsers: [],
storagePrefix: '',
userTable: 'users',
fieldNames: {
id: 'id',
username: 'username',
password: 'password',
phone: 'phone',
email: 'email',
is_disabled: 'is_disabled',
is_deleted: 'is_deleted'
},
tokenExpiry: 24 * 60 * 60,
refreshTokenExpiry: 7 * 24 * 60 * 60
})
log.auth('Auth实例初始化完成')
return auth
} catch (error) {
log.auth('Auth初始化失败:', error)
throw error
}
}
// 初始化API Client
// 注意WORKSPACE_KEY 需要在 多八多(www.d8d.fun) 平台注册并开通工作空间后获取
@@ -47,6 +79,7 @@ if (!workspaceKey) {
console.warn('未设置WORKSPACE_KEY请前往 多八多(www.d8d.fun) 注册并开通工作空间以获取密钥')
}
const apiClient = await getApiClient(workspaceKey)
const auth = await initAuth(apiClient);
// 创建Hono应用实例
const app = new Hono()
@@ -63,10 +96,10 @@ const io = new Server({
}
})
setupSocketIO(io);
setupSocketIO({io, auth, apiClient});
// 动态加载并运行模板
const runTemplate = async () => {
const runTemplate = () => {
try {
// 创建基础app实例
const moduleApp = new Hono()
@@ -75,7 +108,8 @@ const runTemplate = async () => {
const appInstance = httpServer({
apiClient: apiClient,
app: moduleApp,
moduleDir: './'
moduleDir: './',
auth
})
// 启动服务器
Deno.serve({
@@ -92,4 +126,4 @@ const runTemplate = async () => {
}
// 执行模板
runTemplate()
runTemplate()