Add Spotify playback, web search, and Mini 2 autostart

This commit is contained in:
2026-11-10 15:54:23 +03:00
parent 76a0eb86d0
commit 58d28c7683
31 changed files with 1282 additions and 64 deletions
+14 -2
View File
@@ -1,6 +1,7 @@
import { createHmac, timingSafeEqual } from 'node:crypto';
import { readFileSync } from 'node:fs';
import { validateSettings } from './store.mjs';
import { spotifyStatus, startSpotify, finishSpotify } from './spotify.mjs';
const assets = new Map([
['/', ['index.html', 'text/html; charset=utf-8']],
@@ -75,17 +76,28 @@ export async function handleAdmin(req, res, store) {
return true;
}
if (route !== '/api/admin' && route !== '/api/admin/settings' && route !== '/api/admin/history') return false;
if (route === '/api/spotify/callback' && req.method === 'GET') {
const params = new URL(req.url, 'http://localhost').searchParams;
let success = false;
try { await finishSpotify(params.get('state'), params.get('code')); success = true; } catch {}
res.writeHead(success ? 200 : 400, { ...securityHeaders, 'content-type': 'text/html; charset=utf-8' });
res.end(success ? '<!doctype html><html lang="ru"><meta charset="utf-8"><title>Spotify подключён</title><p>Spotify подключён. Вернитесь в пульт.</p></html>' : '<!doctype html><html lang="ru"><meta charset="utf-8"><title>Ошибка подключения</title><p>Не удалось подключить Spotify. Попробуйте снова из пульта.</p></html>');
return true;
}
if (route !== '/api/admin' && route !== '/api/admin/settings' && route !== '/api/admin/history' && route !== '/api/spotify/start') return false;
const auth = req.headers.authorization ?? '';
if (!auth.startsWith('tma ') || !verifyTelegram(auth.slice(4), process.env.TELEGRAM_BOT_TOKEN, process.env.TELEGRAM_OWNER_ID)) {
json(res, 401, { error: 'telegram_auth' });
return true;
}
if (route === '/api/admin' && req.method === 'GET') {
if (route === '/api/spotify/start' && req.method === 'POST') {
try { json(res, 200, { url: startSpotify() }); } catch { json(res, 503, { error: 'spotify_not_configured' }); }
} else if (route === '/api/admin' && req.method === 'GET') {
json(res, 200, {
settings: store.getSettings(),
history: store.getHistory().reverse(),
spotify: spotifyStatus(),
});
} else if (route === '/api/admin/settings' && req.method === 'PUT') {
const settings = await readSettings(req);