- Refactored seeder to Express API with /seed and /reset endpoints - Updated Dockerfile to run server.js - Fixed DB connection for managed postgres (stripped sslmode) - Fixed tags seeder export syntax - Preserved CLI functionality
99 lines
2.8 KiB
JavaScript
99 lines
2.8 KiB
JavaScript
import express from 'express';
|
|
import cors from 'cors';
|
|
import {
|
|
resetDatabase,
|
|
seedDatabase,
|
|
seedDatabaseLite,
|
|
seedDatabaseNoLocations
|
|
} from './index.js';
|
|
import { pool } from './db.js';
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 8080;
|
|
|
|
app.use(cors());
|
|
app.use(express.json());
|
|
|
|
// Middleware to log requests
|
|
app.use((req, res, next) => {
|
|
console.log(`[${new Date().toISOString()}] ${req.method} ${req.url}`);
|
|
next();
|
|
});
|
|
|
|
// Health check
|
|
app.get('/health', async (req, res) => {
|
|
try {
|
|
const result = await pool.query('SELECT 1');
|
|
res.json({ status: 'ok', database: 'connected', version: '1.0.0' });
|
|
} catch (error) {
|
|
console.error('Health check failed:', error);
|
|
res.status(500).json({ status: 'error', database: 'disconnected', error: error.message });
|
|
}
|
|
});
|
|
|
|
// Seed endpoint
|
|
// Options: type = 'full' | 'lite' | 'no-locations'
|
|
app.post('/seed', async (req, res) => {
|
|
if (isSeeding) {
|
|
return res.status(409).json({ error: 'Seeding already in progress' });
|
|
}
|
|
|
|
const type = req.body.type || 'full';
|
|
const password = req.body.password;
|
|
|
|
// Simple protection (optional, can be improved)
|
|
if (process.env.SEED_PASSWORD && password !== process.env.SEED_PASSWORD) {
|
|
return res.status(401).json({ error: 'Unauthorized' });
|
|
}
|
|
|
|
isSeeding = true;
|
|
res.json({ message: 'Seeding started', type }); // Respond immediately
|
|
|
|
try {
|
|
console.log(`🚀 Starting manual seed (${type})...`);
|
|
if (type === 'lite') {
|
|
await seedDatabaseLite();
|
|
} else if (type === 'no-locations') {
|
|
await seedDatabaseNoLocations();
|
|
} else {
|
|
await seedDatabase();
|
|
}
|
|
console.log('✅ Manual seed completed');
|
|
} catch (error) {
|
|
console.error('❌ Manual seed failed:', error);
|
|
} finally {
|
|
isSeeding = false;
|
|
}
|
|
});
|
|
|
|
// Reset endpoint
|
|
app.post('/reset', async (req, res) => {
|
|
if (isSeeding) {
|
|
return res.status(409).json({ error: 'Seeding/Reset in progress' });
|
|
}
|
|
|
|
const password = req.body.password;
|
|
if (process.env.SEED_PASSWORD && password !== process.env.SEED_PASSWORD) {
|
|
return res.status(401).json({ error: 'Unauthorized' });
|
|
}
|
|
|
|
isSeeding = true;
|
|
res.json({ message: 'Reset started' });
|
|
|
|
try {
|
|
console.log('🚀 Starting manual reset...');
|
|
await resetDatabase();
|
|
console.log('✅ Manual reset completed');
|
|
} catch (error) {
|
|
console.error('❌ Manual reset failed:', error);
|
|
} finally {
|
|
isSeeding = false;
|
|
}
|
|
});
|
|
|
|
let isSeeding = false;
|
|
|
|
app.listen(port, () => {
|
|
console.log(`🌱 GoHorseJobs Seeder API listening on port ${port}`);
|
|
console.log(` Health check: http://localhost:${port}/health`);
|
|
});
|