import { Hono } from 'hono'; import type { ProviderConfig, ProxyConfig, ModelGroup } from '../../config.js'; import { HomePage } from '../views/home.js'; import { getCurrentUser } from '../middleware/auth.js'; import { loadFullConfig } from '../../config.js'; export function createHomeRoute(config: ProxyConfig | (() => ProxyConfig), configPath?: string) { const app = new Hono(); // 根路径重定向到 /user/main app.get('/', (c) => { return c.redirect('/user/main'); }); // 用户主页/配置指南 app.get('/user/main', (c) => { // 检查是否启用了认证 let isAuthEnabled = false; if (configPath) { const fullConfig = loadFullConfig(configPath); isAuthEnabled = !!(fullConfig.userApiKeys && fullConfig.userApiKeys.length > 0); } // 未启用认证时,直接显示页面(无需登录) if (!isAuthEnabled) { const currentConfig = typeof config === 'function' ? config() : config; const fullConfig = configPath ? loadFullConfig(configPath) : undefined; return c.html(); } // 已启用认证,需要登录 const currentUser = getCurrentUser(c, configPath); if (!currentUser) { return c.redirect('/user/login'); } const currentConfig = typeof config === 'function' ? config() : config; const fullConfig = configPath ? loadFullConfig(configPath) : undefined; let visibleModels = currentConfig.models; if (currentUser?.allowedModels?.length) { visibleModels = currentConfig.models.filter( m => currentUser.allowedModels!.includes(m.customModel) ); } return c.html(); }); return app; }