68 lines
2.7 KiB
JavaScript
68 lines
2.7 KiB
JavaScript
#!/usr/bin/env node
|
|
// create_session.js
|
|
// Usage: node scripts/create_session.js [room] [username] [--open]
|
|
// Environment overrides: BACKEND (default http://localhost:4000) or TOKEN_SERVER env
|
|
|
|
const fetch = require('node-fetch');
|
|
const child = require('child_process');
|
|
|
|
async function main() {
|
|
console.log('[create_session] starting');
|
|
const args = process.argv.slice(2).filter(a => !a.startsWith('--'));
|
|
const openFlag = process.argv.slice(2).some(a => a === '--open');
|
|
const room = args[0] || `broadcast-${Math.random().toString(36).slice(2,8)}`;
|
|
const username = args[1] || 'browser-user';
|
|
|
|
const backend = process.env.BACKEND || process.env.VITE_BACKEND_API_URL || process.env.VITE_TOKEN_SERVER_URL || 'http://localhost:4000';
|
|
const base = backend.replace(/\/$/, '');
|
|
const url = `${base}/api/session`;
|
|
|
|
console.log('[create_session] Creating session on', url);
|
|
console.log('[create_session] room=', room, 'username=', username);
|
|
|
|
try {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), 15000);
|
|
const r = await fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ room, username }), signal: controller.signal });
|
|
clearTimeout(timeout);
|
|
console.log('[create_session] response status=', r.status);
|
|
const text = await r.text().catch(() => null);
|
|
console.log('[create_session] response body (raw):', text && text.slice(0,2000));
|
|
let json = null;
|
|
try { json = text ? JSON.parse(text) : null; } catch (e) { json = null }
|
|
if (!r.ok) {
|
|
console.error('[create_session] Request failed status=', r.status);
|
|
console.error('[create_session] Body:', text);
|
|
process.exit(2);
|
|
}
|
|
const id = (json && (json.id || json.sessionId)) || null;
|
|
if (!id) {
|
|
console.error('[create_session] No session id in response:', json || text);
|
|
process.exit(3);
|
|
}
|
|
const studioHost = process.env.BROADCAST_HOST || 'https://avanzacast-broadcastpanel.bfzqqk.easypanel.host';
|
|
const sessionUrl = `${studioHost.replace(/\/$/, '')}/${encodeURIComponent(id)}`;
|
|
console.log('[create_session] Session created:', JSON.stringify(json, null, 2));
|
|
console.log('[create_session] Open Studio URL:', sessionUrl);
|
|
|
|
if (openFlag) {
|
|
// xdg-open for linux
|
|
try {
|
|
child.execSync(`xdg-open "${sessionUrl}"`);
|
|
} catch (e) {
|
|
console.warn('[create_session] Failed to open browser automatically:', String(e.message || e));
|
|
}
|
|
}
|
|
process.exit(0);
|
|
} catch (err) {
|
|
if (err.name === 'AbortError') {
|
|
console.error('[create_session] request timed out');
|
|
} else {
|
|
console.error('[create_session] Failed to create session:', String(err));
|
|
}
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|