feat(app): wire up Express app factory and production entry point (Task 13)

Initializes DB, creates default admin user, connects brokers/PG sources,
starts query timers, and serves frontend/dist as SPA.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-06 12:56:43 +02:00
co-authored by Claude Sonnet 4.6
parent 44f4b6d010
commit 9e96ecc231
2 changed files with 88 additions and 12 deletions
+43 -12
View File
@@ -1,23 +1,54 @@
import express from 'express';
import cors from 'cors';
import authRouter from './api/routes/auth';
import bcrypt from 'bcryptjs';
import { initDb } from './db/index';
import { createApp } from './api/index';
import { connectAllEnabledBrokers } from './modules/mqtt/broker-manager';
import { connectAllEnabledSources } from './modules/postgres/source-manager';
import { startAllQueryTimers } from './modules/postgres/query-scheduler';
const app = express();
const PORT = process.env.PORT ? parseInt(process.env.PORT) : 4000;
const DB_PATH = process.env.DB_PATH ?? './display.db';
app.use(cors());
app.use(express.json());
async function main(): Promise<void> {
// Initialize database
const db = initDb(DB_PATH);
app.get('/health', (_req, res) => {
res.json({ status: 'ok', timestamp: new Date().toISOString() });
});
// Create default admin user if no users exist
const userCount = db
.prepare('SELECT COUNT(*) as count FROM users')
.get() as { count: number };
app.use('/api/auth', authRouter);
if (userCount.count === 0) {
const hash = bcrypt.hashSync('admin', 10);
db.run(
`INSERT INTO users (username, password_hash) VALUES (?, ?)`,
['admin', hash]
);
console.log('Created default admin user (admin/admin)');
}
// Connect all enabled MQTT brokers
await connectAllEnabledBrokers();
// Connect all enabled PostgreSQL sources and start query timers
await connectAllEnabledSources();
startAllQueryTimers();
// Create and start Express app
const app = createApp();
if (require.main === module) {
app.listen(PORT, () => {
console.log(`Middleware server running on port ${PORT}`);
});
}
export default app;
// Only run when executed directly (not during tests)
if (require.main === module) {
main().catch((err) => {
console.error('Fatal startup error:', err);
process.exit(1);
});
}
// Export createApp for tests that import from index
export { createApp };
export default createApp();