const express = require('express'); const path = require('path'); const fs = require('fs'); const database = require('./lib/database'); const imageFetcher = require('./lib/image-fetcher'); const imagesRouter = require('./routes/images'); const app = express(); const PORT = process.env.PORT || 3001; const DOMAIN = process.env.DOMAIN || 'homebase.sketchferret.com'; // Middleware app.use(express.json()); app.use(express.static(path.join(__dirname, 'public'))); // Initialize database database.initializeDatabase().catch(err => { console.error('Failed to initialize database:', err); process.exit(1); }); // API Routes app.use('/api', imagesRouter); // Status endpoint app.get('/api/status', (req, res) => { res.json({ message: 'HomeBase is running!', domain: DOMAIN, timestamp: new Date().toISOString(), version: '1.0.0', features: { imageStorage: true, tagging: true, corruptionDetection: true } }); }); // Health check app.get('/health', (req, res) => { res.json({ status: 'healthy' }); }); // Start server const server = app.listen(PORT, '0.0.0.0', () => { console.log(`\n🏠 HomeBase Server Started`); console.log(` Port: ${PORT}`); console.log(` Domain: ${DOMAIN}`); console.log(` URL: http://${DOMAIN}\n`); // Load configuration from image-sources.json const configPath = path.join(__dirname, 'image-sources.json'); let config = { sources: [], fetchInterval: 2.5 }; try { if (fs.existsSync(configPath)) { const fileContent = fs.readFileSync(configPath, 'utf8'); config = JSON.parse(fileContent); } } catch (err) { console.warn('⚠️ Could not read image-sources.json:', err.message); } const imageSources = config.sources || []; const fetchInterval = config.fetchInterval || 2.5; // Start image fetcher if sources are configured if (imageSources.length > 0) { const enabledSources = imageSources.filter(s => s.enabled); if (enabledSources.length > 0) { imageFetcher.startFetcher(enabledSources, fetchInterval); console.log(`📸 Image fetcher: Enabled (${fetchInterval} minute interval)\n`); } else { console.log('📸 Image fetcher: Disabled (no enabled sources in config)'); console.log(' Edit image-sources.json and set "enabled": true to enable\n'); } } else { console.log('📸 Image fetcher: No sources configured'); console.log(' Edit image-sources.json to add image sources\n'); } }); // Graceful shutdown process.on('SIGTERM', () => { console.log('\n📴 Shutting down gracefully...'); imageFetcher.stopFetcher(); server.close(async () => { await database.closeDatabase(); process.exit(0); }); }); process.on('SIGINT', () => { console.log('\n📴 Shutting down gracefully...'); imageFetcher.stopFetcher(); server.close(async () => { await database.closeDatabase(); process.exit(0); }); });