Files
HomeBase/server.js
Spencer 9c72b00b1b
Some checks failed
Deploy to BeePC / deploy (push) Has been cancelled
feat: Implement image fetching and storage system
- Add image-fetcher module for downloading and saving images from various sources.
- Create storage module for managing image files, including downloading, verifying integrity, and cleaning up orphaned files.
- Develop gallery HTML page for displaying images with sorting and filtering options.
- Set up RESTful API routes for image management, including fetching, adding tags, and deleting images.
- Introduce setup script for initializing the database and configuring image sources.
- Implement a batch process for verifying image integrity and cleaning up old images.
- Add setup batch script for easy installation and configuration of the image storage system.
2026-02-12 13:13:36 -05:00

104 lines
2.9 KiB
JavaScript

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);
});
});