import 'dotenv/config'; import express from 'express'; import { createServer } from 'node:http'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { existsSync } from 'node:fs'; import { initDb } from './db/index.js'; import { authMiddleware, ensureBootstrapAdmin } from './auth.js'; import { applySeedIfEmpty } from './sources/seed.js'; import { scheduleHealthCheck } from './streams/checker.js'; import { attachWs } from './ws.js'; import { router as authRoutes } from './routes/auth.js'; import { router as stationRoutes } from './routes/stations.js'; import { router as meRoutes } from './routes/me.js'; import { router as adminRoutes } from './routes/admin.js'; import { router as v1Routes } from './routes/v1.js'; const __dirname = dirname(fileURLToPath(import.meta.url)); const PORT = Number(process.env.PORT) || 4173; initDb(process.env.DB_PATH || './data/db/oradio.sqlite'); ensureBootstrapAdmin({ username: process.env.ADMIN_BOOTSTRAP_USER, password: process.env.ADMIN_BOOTSTRAP_PASSWORD }); const seedResult = applySeedIfEmpty(); console.log('[seed]', seedResult); const app = express(); app.use(express.json({ limit: '512kb' })); app.use(authMiddleware); app.use('/api/auth', authRoutes); app.use('/api/stations', stationRoutes); app.use('/api/me', meRoutes); app.use('/api/admin', adminRoutes); app.use('/api/v1', v1Routes); // Static assets (built by Vite). In dev these don't exist; Vite serves them on :5173. const publicDir = resolve(__dirname, 'public'); if (existsSync(publicDir)) { app.use(express.static(publicDir)); app.get('/admin', (_req, res) => res.sendFile(resolve(publicDir, 'admin/index.html'))); app.get('/docs', (_req, res) => res.sendFile(resolve(publicDir, 'docs/index.html'))); app.get('*', (_req, res) => res.sendFile(resolve(publicDir, 'index.html'))); } app.use((err, _req, res, _next) => { console.error(err); res.status(500).json({ error: String(err.message || err) }); }); const server = createServer(app); attachWs(server); scheduleHealthCheck(process.env.STREAM_CHECK_CRON); server.listen(PORT, () => { console.log(`[oradio] api+ws on http://localhost:${PORT}`); });