观影室改为仅环境变量配置
This commit is contained in:
80
server.js
80
server.js
@@ -13,78 +13,16 @@ const handle = app.getRequestHandler();
|
||||
|
||||
// 读取观影室配置的辅助函数
|
||||
async function getWatchRoomConfig() {
|
||||
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||
// 观影室配置现在统一从环境变量读取
|
||||
const config = {
|
||||
enabled: process.env.WATCH_ROOM_ENABLED === 'true',
|
||||
serverType: (process.env.WATCH_ROOM_SERVER_TYPE || 'internal'),
|
||||
externalServerUrl: process.env.WATCH_ROOM_EXTERNAL_SERVER_URL,
|
||||
externalServerAuth: process.env.WATCH_ROOM_EXTERNAL_SERVER_AUTH,
|
||||
};
|
||||
|
||||
// 如果使用 localStorage,无法在服务器端读取,返回默认配置(不启用)
|
||||
if (storageType === 'localstorage') {
|
||||
console.log('[WatchRoom] Using localStorage storage type.');
|
||||
// 在 localStorage 模式下,可以通过环境变量控制是否启用观影室
|
||||
const enabled = process.env.WATCH_ROOM_ENABLED === 'true';
|
||||
console.log(`[WatchRoom] Watch room ${enabled ? 'enabled' : 'disabled'} via environment variable.`);
|
||||
return { enabled, serverType: 'internal' };
|
||||
}
|
||||
|
||||
try {
|
||||
if (storageType === 'redis') {
|
||||
// 检查 Redis 配置
|
||||
const redisUrl = process.env.REDIS_URL || 'redis://localhost:6379';
|
||||
console.log('[WatchRoom] Attempting to read config from Redis...');
|
||||
|
||||
const { createClient } = require('redis');
|
||||
const client = createClient({ url: redisUrl });
|
||||
await client.connect();
|
||||
|
||||
const configStr = await client.get('admin:config'); // 注意:使用冒号而不是下划线
|
||||
await client.disconnect();
|
||||
|
||||
if (configStr) {
|
||||
const config = JSON.parse(configStr);
|
||||
return config.WatchRoomConfig || { enabled: false, serverType: 'internal' };
|
||||
}
|
||||
} else if (storageType === 'upstash') {
|
||||
// 检查 Upstash 环境变量
|
||||
if (!process.env.UPSTASH_REDIS_REST_URL || !process.env.UPSTASH_REDIS_REST_TOKEN) {
|
||||
console.log('[WatchRoom] Upstash credentials not configured. Socket.IO disabled by default.');
|
||||
return { enabled: false, serverType: 'internal' };
|
||||
}
|
||||
|
||||
console.log('[WatchRoom] Attempting to read config from Upstash...');
|
||||
const { Redis } = require('@upstash/redis');
|
||||
const redis = new Redis({
|
||||
url: process.env.UPSTASH_REDIS_REST_URL,
|
||||
token: process.env.UPSTASH_REDIS_REST_TOKEN,
|
||||
});
|
||||
|
||||
const configStr = await redis.get('admin:config'); // 注意:使用冒号而不是下划线
|
||||
|
||||
if (configStr) {
|
||||
const config = typeof configStr === 'string' ? JSON.parse(configStr) : configStr;
|
||||
return config.WatchRoomConfig || { enabled: false, serverType: 'internal' };
|
||||
}
|
||||
} else if (storageType === 'kvrocks') {
|
||||
// 检查 Kvrocks 配置
|
||||
const kvrocksUrl = process.env.KVROCKS_URL || 'redis://localhost:6666';
|
||||
console.log('[WatchRoom] Attempting to read config from Kvrocks...');
|
||||
|
||||
const { createClient } = require('redis');
|
||||
const client = createClient({ url: kvrocksUrl });
|
||||
await client.connect();
|
||||
|
||||
const configStr = await client.get('admin:config'); // 注意:使用冒号而不是下划线
|
||||
await client.disconnect();
|
||||
|
||||
if (configStr) {
|
||||
const config = JSON.parse(configStr);
|
||||
return config.WatchRoomConfig || { enabled: false, serverType: 'internal' };
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('[WatchRoom] Failed to read config from storage:', error.message);
|
||||
}
|
||||
|
||||
// 默认不启用观影室
|
||||
console.log('[WatchRoom] No config found or error occurred. Socket.IO disabled by default.');
|
||||
return { enabled: false, serverType: 'internal' };
|
||||
console.log(`[WatchRoom] Watch room ${config.enabled ? 'enabled' : 'disabled'} via environment variable.`);
|
||||
return config;
|
||||
}
|
||||
|
||||
// 观影室服务器类
|
||||
|
||||
@@ -5852,204 +5852,6 @@ const LiveSourceConfig = ({
|
||||
);
|
||||
};
|
||||
|
||||
// 观影室配置组件
|
||||
const WatchRoomConfig = ({
|
||||
config,
|
||||
refreshConfig,
|
||||
}: {
|
||||
config: AdminConfig | null;
|
||||
refreshConfig: () => Promise<void>;
|
||||
}) => {
|
||||
const { alertModal, showAlert, hideAlert } = useAlertModal();
|
||||
const { isLoading, withLoading } = useLoadingState();
|
||||
const [settings, setSettings] = useState({
|
||||
enabled: false,
|
||||
serverType: 'internal' as 'internal' | 'external',
|
||||
externalServerUrl: '',
|
||||
externalServerAuth: '',
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (config?.WatchRoomConfig) {
|
||||
setSettings({
|
||||
enabled: config.WatchRoomConfig.enabled || false,
|
||||
serverType: config.WatchRoomConfig.serverType || 'internal',
|
||||
externalServerUrl: config.WatchRoomConfig.externalServerUrl || '',
|
||||
externalServerAuth: config.WatchRoomConfig.externalServerAuth || '',
|
||||
});
|
||||
}
|
||||
}, [config]);
|
||||
|
||||
const handleSave = async () => {
|
||||
await withLoading('saveWatchRoomConfig', async () => {
|
||||
try {
|
||||
// 验证外部服务器配置
|
||||
if (settings.serverType === 'external' && !settings.externalServerUrl.trim()) {
|
||||
showError('外部服务器地址不能为空', showAlert);
|
||||
return;
|
||||
}
|
||||
|
||||
const resp = await fetch('/api/admin/watch-room', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify(settings),
|
||||
});
|
||||
|
||||
if (!resp.ok) {
|
||||
const data = await resp.json().catch(() => ({}));
|
||||
throw new Error(data.error || `保存失败: ${resp.status}`);
|
||||
}
|
||||
|
||||
showSuccess('保存成功,刷新页面后生效', showAlert);
|
||||
await refreshConfig();
|
||||
} catch (err) {
|
||||
showError(err instanceof Error ? err.message : '保存失败', showAlert);
|
||||
throw err;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
if (!config) {
|
||||
return (
|
||||
<div className='text-center text-gray-500 dark:text-gray-400'>
|
||||
加载中...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className='space-y-6'>
|
||||
{/* 功能开关 */}
|
||||
<div className='flex items-center justify-between p-4 bg-blue-50 dark:bg-blue-900/20 rounded-lg border border-blue-200 dark:border-blue-800'>
|
||||
<div>
|
||||
<h4 className='text-sm font-medium text-gray-900 dark:text-gray-100'>
|
||||
启用观影室功能
|
||||
</h4>
|
||||
<p className='text-xs text-gray-600 dark:text-gray-400 mt-1'>
|
||||
开启后,用户可以创建和加入观影室,实现多人同步观看
|
||||
</p>
|
||||
</div>
|
||||
<label className='relative inline-flex items-center cursor-pointer'>
|
||||
<input
|
||||
type='checkbox'
|
||||
className='sr-only peer'
|
||||
checked={settings.enabled}
|
||||
onChange={(e) =>
|
||||
setSettings({ ...settings, enabled: e.target.checked })
|
||||
}
|
||||
/>
|
||||
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none peer-focus:ring-4 peer-focus:ring-blue-300 dark:peer-focus:ring-blue-800 rounded-full peer dark:bg-gray-700 peer-checked:after:translate-x-full rtl:peer-checked:after:-translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:start-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all dark:border-gray-600 peer-checked:bg-blue-600"></div>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
{/* 服务器类型选择 */}
|
||||
<div>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||
服务器类型
|
||||
</label>
|
||||
<select
|
||||
value={settings.serverType}
|
||||
onChange={(e) =>
|
||||
setSettings({
|
||||
...settings,
|
||||
serverType: e.target.value as 'internal' | 'external',
|
||||
})
|
||||
}
|
||||
className='w-full px-3 py-2 text-sm text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-400 focus:border-transparent'
|
||||
>
|
||||
<option value='internal'>内部服务器(与网站集成)</option>
|
||||
<option value='external'>外部服务器(独立部署)</option>
|
||||
</select>
|
||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||
{settings.serverType === 'internal'
|
||||
? '使用内置的 Socket.IO 服务器,无需额外配置'
|
||||
: '使用独立部署的 Socket.IO 服务器,适合高并发场景'}
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* 外部服务器配置 */}
|
||||
{settings.serverType === 'external' && (
|
||||
<div className='space-y-4 p-4 bg-gray-50 dark:bg-gray-800/50 rounded-lg border border-gray-200 dark:border-gray-700'>
|
||||
<div>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||
服务器地址 *
|
||||
</label>
|
||||
<input
|
||||
type='text'
|
||||
value={settings.externalServerUrl}
|
||||
onChange={(e) =>
|
||||
setSettings({ ...settings, externalServerUrl: e.target.value })
|
||||
}
|
||||
placeholder='http://your-server:3001'
|
||||
className='w-full px-3 py-2 text-sm text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-400 focus:border-transparent'
|
||||
/>
|
||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||
外部 Socket.IO 服务器的完整地址(包含协议和端口)
|
||||
</p>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label className='block text-sm font-medium text-gray-700 dark:text-gray-300 mb-2'>
|
||||
鉴权密钥
|
||||
</label>
|
||||
<input
|
||||
type='password'
|
||||
value={settings.externalServerAuth}
|
||||
onChange={(e) =>
|
||||
setSettings({ ...settings, externalServerAuth: e.target.value })
|
||||
}
|
||||
placeholder='可选:服务器鉴权密钥'
|
||||
className='w-full px-3 py-2 text-sm text-gray-900 dark:text-gray-100 bg-white dark:bg-gray-800 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 dark:focus:ring-blue-400 focus:border-transparent'
|
||||
/>
|
||||
<p className='text-xs text-gray-500 dark:text-gray-400 mt-1'>
|
||||
如果外部服务器需要鉴权,请填写密钥
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* 使用说明 */}
|
||||
<div className='p-4 bg-yellow-50 dark:bg-yellow-900/20 rounded-lg border border-yellow-200 dark:border-yellow-800'>
|
||||
<h5 className='text-sm font-medium text-yellow-800 dark:text-yellow-300 mb-2'>
|
||||
使用说明
|
||||
</h5>
|
||||
<ul className='text-xs text-yellow-700 dark:text-yellow-400 space-y-1 list-disc list-inside'>
|
||||
<li>观影室功能允许多人同步观看视频,支持播放控制同步和聊天</li>
|
||||
<li>内部服务器模式无需额外配置,适合中小型部署</li>
|
||||
<li>外部服务器模式需要单独部署 Socket.IO 服务器(参考 server/watch-room-standalone-server.js)</li>
|
||||
<li>更改配置后需要重启服务器才能生效</li>
|
||||
</ul>
|
||||
</div>
|
||||
|
||||
{/* 保存按钮 */}
|
||||
<div className='flex justify-end'>
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={isLoading('saveWatchRoomConfig')}
|
||||
className={`px-4 py-2 ${
|
||||
isLoading('saveWatchRoomConfig')
|
||||
? buttonStyles.disabled
|
||||
: buttonStyles.success
|
||||
} rounded-lg transition-colors`}
|
||||
>
|
||||
{isLoading('saveWatchRoomConfig') ? '保存中…' : '保存'}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
{/* 通用弹窗组件 */}
|
||||
<AlertModal
|
||||
isOpen={alertModal.isOpen}
|
||||
onClose={hideAlert}
|
||||
type={alertModal.type}
|
||||
title={alertModal.title}
|
||||
message={alertModal.message}
|
||||
timer={alertModal.timer}
|
||||
showConfirm={alertModal.showConfirm}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
function AdminPageClient() {
|
||||
const { alertModal, showAlert, hideAlert } = useAlertModal();
|
||||
const { isLoading, withLoading } = useLoadingState();
|
||||
@@ -6067,7 +5869,6 @@ function AdminPageClient() {
|
||||
configFile: false,
|
||||
dataMigration: false,
|
||||
customAdFilter: false,
|
||||
watchRoomConfig: false,
|
||||
});
|
||||
|
||||
// 获取管理员配置
|
||||
@@ -6256,18 +6057,6 @@ function AdminPageClient() {
|
||||
<LiveSourceConfig config={config} refreshConfig={fetchConfig} />
|
||||
</CollapsibleTab>
|
||||
|
||||
{/* 观影室配置标签 */}
|
||||
<CollapsibleTab
|
||||
title='观影室配置'
|
||||
icon={
|
||||
<Users size={20} className='text-gray-600 dark:text-gray-400' />
|
||||
}
|
||||
isExpanded={expandedTabs.watchRoomConfig}
|
||||
onToggle={() => toggleTab('watchRoomConfig')}
|
||||
>
|
||||
<WatchRoomConfig config={config} refreshConfig={fetchConfig} />
|
||||
</CollapsibleTab>
|
||||
|
||||
{/* 分类配置标签 */}
|
||||
<CollapsibleTab
|
||||
title='分类配置'
|
||||
|
||||
@@ -1,98 +0,0 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any,no-console */
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { getConfig } from '@/lib/config';
|
||||
import { db } from '@/lib/db';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||
if (storageType === 'localstorage') {
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: '不支持本地存储进行管理员配置',
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const body = await request.json();
|
||||
|
||||
const authInfo = getAuthInfoFromCookie(request);
|
||||
if (!authInfo || !authInfo.username) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
const username = authInfo.username;
|
||||
|
||||
const {
|
||||
enabled,
|
||||
serverType,
|
||||
externalServerUrl,
|
||||
externalServerAuth,
|
||||
} = body as {
|
||||
enabled: boolean;
|
||||
serverType: 'internal' | 'external';
|
||||
externalServerUrl?: string;
|
||||
externalServerAuth?: string;
|
||||
};
|
||||
|
||||
// 参数校验
|
||||
if (
|
||||
typeof enabled !== 'boolean' ||
|
||||
(serverType !== 'internal' && serverType !== 'external')
|
||||
) {
|
||||
return NextResponse.json({ error: '参数格式错误' }, { status: 400 });
|
||||
}
|
||||
|
||||
// 如果使用外部服务器,URL 必须提供
|
||||
if (serverType === 'external' && !externalServerUrl) {
|
||||
return NextResponse.json({ error: '外部服务器地址不能为空' }, { status: 400 });
|
||||
}
|
||||
|
||||
const adminConfig = await getConfig();
|
||||
|
||||
// 权限校验
|
||||
if (username !== process.env.USERNAME) {
|
||||
// 管理员
|
||||
const user = adminConfig.UserConfig.Users.find(
|
||||
(u) => u.username === username
|
||||
);
|
||||
if (!user || user.role !== 'admin' || user.banned) {
|
||||
return NextResponse.json({ error: '权限不足' }, { status: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
// 更新缓存中的观影室配置
|
||||
adminConfig.WatchRoomConfig = {
|
||||
enabled,
|
||||
serverType,
|
||||
externalServerUrl,
|
||||
externalServerAuth,
|
||||
};
|
||||
|
||||
// 写入数据库
|
||||
await db.saveAdminConfig(adminConfig);
|
||||
|
||||
return NextResponse.json(
|
||||
{ ok: true },
|
||||
{
|
||||
headers: {
|
||||
'Cache-Control': 'no-store', // 不缓存结果
|
||||
},
|
||||
}
|
||||
);
|
||||
} catch (error) {
|
||||
console.error('更新观影室配置失败:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: '更新观影室配置失败',
|
||||
details: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -12,20 +12,21 @@ export async function GET(request: NextRequest) {
|
||||
|
||||
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||
|
||||
// 观影室配置从环境变量读取
|
||||
const watchRoomConfig = {
|
||||
enabled: process.env.WATCH_ROOM_ENABLED === 'true',
|
||||
serverType: (process.env.WATCH_ROOM_SERVER_TYPE as 'internal' | 'external') || 'internal',
|
||||
externalServerUrl: process.env.WATCH_ROOM_EXTERNAL_SERVER_URL,
|
||||
externalServerAuth: process.env.WATCH_ROOM_EXTERNAL_SERVER_AUTH,
|
||||
};
|
||||
|
||||
// 如果使用 localStorage,返回默认配置
|
||||
if (storageType === 'localstorage') {
|
||||
return NextResponse.json({
|
||||
SiteName: process.env.NEXT_PUBLIC_SITE_NAME || 'MoonTV',
|
||||
StorageType: 'localstorage',
|
||||
Version: CURRENT_VERSION,
|
||||
// localStorage 模式下,返回默认观影室配置
|
||||
// 可以通过环境变量控制是否启用
|
||||
WatchRoom: {
|
||||
enabled: process.env.WATCH_ROOM_ENABLED === 'true',
|
||||
serverType: 'internal',
|
||||
externalServerUrl: undefined,
|
||||
externalServerAuth: undefined,
|
||||
},
|
||||
WatchRoom: watchRoomConfig,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -35,13 +36,7 @@ export async function GET(request: NextRequest) {
|
||||
SiteName: config.SiteConfig.SiteName,
|
||||
StorageType: storageType,
|
||||
Version: CURRENT_VERSION,
|
||||
// 添加观影室配置(供所有用户访问)
|
||||
WatchRoom: {
|
||||
enabled: config.WatchRoomConfig?.enabled ?? false,
|
||||
serverType: config.WatchRoomConfig?.serverType ?? 'internal',
|
||||
externalServerUrl: config.WatchRoomConfig?.externalServerUrl,
|
||||
externalServerAuth: config.WatchRoomConfig?.externalServerAuth,
|
||||
},
|
||||
WatchRoom: watchRoomConfig,
|
||||
};
|
||||
return NextResponse.json(result);
|
||||
}
|
||||
|
||||
@@ -63,12 +63,6 @@ export interface AdminConfig {
|
||||
channelNumber?: number;
|
||||
disabled?: boolean;
|
||||
}[];
|
||||
WatchRoomConfig?: {
|
||||
enabled: boolean;
|
||||
serverType: 'internal' | 'external';
|
||||
externalServerUrl?: string;
|
||||
externalServerAuth?: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface AdminConfigResult {
|
||||
|
||||
Reference in New Issue
Block a user