OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid / webui / index.js

39 lines · javascript ref:
// cthuloid-web — `bare index.js`. Web control for cthuloid light units.
//
// Finds units over mDNS (_cthuloid._tcp, via the host's avahi), serves one page
// on the LAN, and drives the units the user includes, the same way the Blender
// addon does. Starts disconnected on every launch: nothing is sent to a light
// until someone presses Connect. The controller itself is lib/app.js, shared
// with the Android app.
//
// ⛔ LAN-only, no login, by decision (2026-10-02). The pgh defaults bind
// 0.0.0.0:4050; the code default is 127.0.0.1 so a bare `bare index.js` on a
// dev machine never exposes the lights by accident.

import os from 'bare-os'
import path from 'bare-path'
import process from 'bare-process'
import { Discovery } from './lib/discovery.js'
import { createApp } from './lib/app.js'

const port = Number(process.env.CTHULOID_WEB_PORT || 4050)
const host = process.env.CTHULOID_WEB_HOST || '127.0.0.1'
const configDir = process.env.CTHULOID_WEB_CONFIG || path.join(os.homedir(), '.config', 'cthuloid-web')

const discovery = new Discovery()
discovery.on('unavailable', (reason) => console.error(JSON.stringify({ msg: 'discovery unavailable', reason })))
discovery.on('up', (u) => console.log(JSON.stringify({ msg: 'unit up', name: u.name, addr: u.addr, proto: u.proto })))
discovery.on('down', (u) => console.log(JSON.stringify({ msg: 'unit down', name: u.name })))

const app = createApp({ configDir, host, port, discovery })
app.listen().then(({ port: p }) => {
  console.log(JSON.stringify({ msg: 'cthuloid-web up', url: 'http://' + host + ':' + p, config: configDir }))
})

// Settings are written on a short debounce; flush them on the way out.
const shutdown = () => {
  app.close().then(() => process.exit(0))
}
process.on('SIGTERM', shutdown)
process.on('SIGINT', shutdown)