Download server.js from MineRoma/tv: direct link, hf CLI and curl.
- Browser
- Download file 4.98 kB
-
https://huggingface.co/spaces/MineRoma/tv/resolve/main/server.js
- Command line
-
hf download hf://spaces/MineRoma/tv/server.js
-
curl -L -o server.js https://huggingface.co/spaces/MineRoma/tv/resolve/main/server.js
4.98 kB
| const express = require('express'); | |
| const session = require('express-session'); | |
| const bodyParser = require('body-parser'); | |
| const cookieParser = require('cookie-parser'); | |
| const path = require('path'); | |
| const fs = require('fs'); | |
| const http = require('http'); | |
| const WebSocket = require('ws'); | |
| const authRoutes = require('./routes/auth'); | |
| const streamRoutes = require('./routes/stream'); | |
| const scheduleRoutes = require('./routes/schedule'); | |
| const liveRoutes = require('./routes/live'); | |
| const db = require('./utils/database'); | |
| const app = express(); | |
| const server = http.createServer(app); | |
| const wss = new WebSocket.Server({ server }); | |
| const PORT = process.env.PORT || 7860; | |
| // Хранилище активных зрителей по каналам | |
| const viewers = { | |
| 'TV-1': new Set(), | |
| 'TV-2': new Set(), | |
| 'TV-3': new Set(), | |
| 'TV-4': new Set() | |
| }; | |
| // WebSocket для подсчета зрителей | |
| wss.on('connection', (ws) => { | |
| let currentChannel = null; | |
| let clientId = Math.random().toString(36).substr(2, 9); | |
| ws.on('message', (message) => { | |
| try { | |
| const data = JSON.parse(message); | |
| if (data.type === 'join') { | |
| // Пользователь зашел на канал | |
| if (currentChannel) { | |
| viewers[currentChannel].delete(clientId); | |
| } | |
| currentChannel = data.channel; | |
| if (viewers[currentChannel]) { | |
| viewers[currentChannel].add(clientId); | |
| } | |
| broadcastViewers(); | |
| } else if (data.type === 'leave') { | |
| // Пользователь покинул канал | |
| if (currentChannel && viewers[currentChannel]) { | |
| viewers[currentChannel].delete(clientId); | |
| currentChannel = null; | |
| broadcastViewers(); | |
| } | |
| } | |
| } catch (error) { | |
| console.error('WebSocket message error:', error); | |
| } | |
| }); | |
| ws.on('close', () => { | |
| // Пользователь отключился | |
| if (currentChannel && viewers[currentChannel]) { | |
| viewers[currentChannel].delete(clientId); | |
| broadcastViewers(); | |
| } | |
| }); | |
| // Отправляем текущее количество зрителей при подключении | |
| ws.send(JSON.stringify({ | |
| type: 'viewers', | |
| data: getViewersCount() | |
| })); | |
| }); | |
| // Рассылка обновлений всем клиентам | |
| function broadcastViewers() { | |
| const data = JSON.stringify({ | |
| type: 'viewers', | |
| data: getViewersCount() | |
| }); | |
| wss.clients.forEach((client) => { | |
| if (client.readyState === WebSocket.OPEN) { | |
| client.send(data); | |
| } | |
| }); | |
| } | |
| // Получить количество зрителей по каналам | |
| function getViewersCount() { | |
| return { | |
| 'TV-1': viewers['TV-1'].size, | |
| 'TV-2': viewers['TV-2'].size, | |
| 'TV-3': viewers['TV-3'].size, | |
| 'TV-4': viewers['TV-4'].size, | |
| total: viewers['TV-1'].size + viewers['TV-2'].size + viewers['TV-3'].size + viewers['TV-4'].size | |
| }; | |
| } | |
| // API для получения количества зрителей | |
| app.get('/api/viewers', (req, res) => { | |
| res.json(getViewersCount()); | |
| }); | |
| // Middleware | |
| app.use(bodyParser.json()); | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| app.use(cookieParser()); | |
| app.use(session({ | |
| secret: process.env.SESSION_SECRET || 'v-otv-secret-key-change-in-production', | |
| resave: false, | |
| saveUninitialized: false, | |
| cookie: { | |
| secure: false, | |
| maxAge: 7 * 24 * 60 * 60 * 1000 // 7 days | |
| } | |
| })); | |
| // Static files | |
| app.use(express.static('public')); | |
| // Routes | |
| app.use('/api/auth', authRoutes); | |
| app.use('/api/stream', streamRoutes); | |
| app.use('/api/schedule', scheduleRoutes); | |
| app.use('/api/live', liveRoutes); | |
| // Main page | |
| app.get('/', (req, res) => { | |
| res.sendFile(path.join(__dirname, 'public', 'index.html')); | |
| }); | |
| // Initialize and start server | |
| async function startServer() { | |
| console.log('🔧 Initializing database...'); | |
| await db.initializeGist(); | |
| server.listen(PORT, () => { | |
| console.log(`🚀 V-OTV Server running on port ${PORT}`); | |
| console.log(`📺 Open http://localhost:${PORT}`); | |
| console.log(''); | |
| console.log('🔐 Encryption: ' + (process.env.ENCRYPTION_SEED ? 'ENABLED' : 'DEFAULT SEED (change in production!)')); | |
| console.log('📋 Gist: ' + (process.env.GIST_ID ? process.env.GIST_ID : 'Will be created automatically')); | |
| console.log('👁 WebSocket viewers tracking: ENABLED'); | |
| }); | |
| } | |
| startServer().catch(error => { | |
| console.error('Failed to start server:', error); | |
| process.exit(1); | |
| }); | |