修正观影室管理面板设置
This commit is contained in:
129
server.js
129
server.js
@@ -11,6 +11,82 @@ const port = parseInt(process.env.PORT || '3000', 10);
|
||||
const app = next({ dev, hostname, port });
|
||||
const handle = app.getRequestHandler();
|
||||
|
||||
// 读取观影室配置的辅助函数
|
||||
async function getWatchRoomConfig() {
|
||||
const storageType = process.env.NEXT_PUBLIC_STORAGE_TYPE || 'localstorage';
|
||||
|
||||
// 如果使用 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' };
|
||||
}
|
||||
|
||||
// 观影室服务器类
|
||||
class WatchRoomServer {
|
||||
constructor(io) {
|
||||
@@ -443,7 +519,7 @@ class WatchRoomServer {
|
||||
}
|
||||
}
|
||||
|
||||
app.prepare().then(() => {
|
||||
app.prepare().then(async () => {
|
||||
const httpServer = createServer(async (req, res) => {
|
||||
try {
|
||||
const parsedUrl = parse(req.url, true);
|
||||
@@ -455,18 +531,35 @@ app.prepare().then(() => {
|
||||
}
|
||||
});
|
||||
|
||||
// 初始化 Socket.IO
|
||||
const io = new Server(httpServer, {
|
||||
path: '/socket.io',
|
||||
cors: {
|
||||
origin: '*',
|
||||
methods: ['GET', 'POST'],
|
||||
},
|
||||
});
|
||||
// 读取观影室配置
|
||||
const watchRoomConfig = await getWatchRoomConfig();
|
||||
console.log('[WatchRoom] Config:', watchRoomConfig);
|
||||
|
||||
// 初始化观影室服务器
|
||||
const watchRoomServer = new WatchRoomServer(io);
|
||||
console.log('[WatchRoom] Socket.IO server initialized');
|
||||
let watchRoomServer = null;
|
||||
|
||||
// 只在启用观影室且使用内部服务器时初始化 Socket.IO
|
||||
if (watchRoomConfig.enabled && watchRoomConfig.serverType === 'internal') {
|
||||
console.log('[WatchRoom] Initializing Socket.IO server...');
|
||||
|
||||
// 初始化 Socket.IO
|
||||
const io = new Server(httpServer, {
|
||||
path: '/socket.io',
|
||||
cors: {
|
||||
origin: '*',
|
||||
methods: ['GET', 'POST'],
|
||||
},
|
||||
});
|
||||
|
||||
// 初始化观影室服务器
|
||||
watchRoomServer = new WatchRoomServer(io);
|
||||
console.log('[WatchRoom] Socket.IO server initialized');
|
||||
} else {
|
||||
if (!watchRoomConfig.enabled) {
|
||||
console.log('[WatchRoom] Watch room is disabled');
|
||||
} else if (watchRoomConfig.serverType === 'external') {
|
||||
console.log('[WatchRoom] Using external watch room server');
|
||||
}
|
||||
}
|
||||
|
||||
httpServer
|
||||
.once('error', (err) => {
|
||||
@@ -475,13 +568,17 @@ app.prepare().then(() => {
|
||||
})
|
||||
.listen(port, () => {
|
||||
console.log(`> Ready on http://${hostname}:${port}`);
|
||||
console.log(`> Socket.IO ready on ws://${hostname}:${port}`);
|
||||
if (watchRoomConfig.enabled && watchRoomConfig.serverType === 'internal') {
|
||||
console.log(`> Socket.IO ready on ws://${hostname}:${port}`);
|
||||
}
|
||||
});
|
||||
|
||||
// 优雅关闭
|
||||
process.on('SIGINT', () => {
|
||||
console.log('\n[Server] Shutting down...');
|
||||
watchRoomServer.destroy();
|
||||
if (watchRoomServer) {
|
||||
watchRoomServer.destroy();
|
||||
}
|
||||
httpServer.close(() => {
|
||||
console.log('[Server] Server closed');
|
||||
process.exit(0);
|
||||
@@ -490,7 +587,9 @@ app.prepare().then(() => {
|
||||
|
||||
process.on('SIGTERM', () => {
|
||||
console.log('\n[Server] Shutting down...');
|
||||
watchRoomServer.destroy();
|
||||
if (watchRoomServer) {
|
||||
watchRoomServer.destroy();
|
||||
}
|
||||
httpServer.close(() => {
|
||||
console.log('[Server] Server closed');
|
||||
process.exit(0);
|
||||
|
||||
Reference in New Issue
Block a user