60 lines
1.5 KiB
JavaScript
60 lines
1.5 KiB
JavaScript
import express from 'express';
|
|
import { exportMergedJson } from './bqexport.js';
|
|
import dotenv from 'dotenv';
|
|
|
|
const app = express();
|
|
|
|
// Middleware to parse JSON bodies
|
|
app.use(express.json());
|
|
|
|
dotenv.config({ path: '.env' });
|
|
|
|
// Health check endpoint
|
|
app.get('/', (req, res) => {
|
|
res.json({
|
|
status: 'OK',
|
|
message: 'BigQuery Export Service is running',
|
|
endpoints: {
|
|
export: 'POST /export - Export BigQuery data to GCS'
|
|
}
|
|
});
|
|
});
|
|
|
|
app.post('/export', async (req, res) => {
|
|
try {
|
|
const { companyId, applicationId } = req.body;
|
|
|
|
console.log(`Received export request:`);
|
|
console.log(`Company ID: ${companyId}`);
|
|
console.log(`Application ID: ${applicationId}`);
|
|
|
|
// Call the BigQuery export function
|
|
const result = await exportMergedJson(req, res);
|
|
|
|
// Send success response
|
|
res.status(200).json({
|
|
success: true,
|
|
signedUrl: result.signedUrl,
|
|
message: result.message,
|
|
companyId: result.companyId,
|
|
applicationId: result.applicationId,
|
|
destination: result.destination
|
|
});
|
|
} catch (error) {
|
|
console.error('Export endpoint error:', error);
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
const PORT = process.env.PORT || 8080;
|
|
app.listen(PORT, () => {
|
|
console.log(`Server is running on port ${PORT}`);
|
|
});
|
|
|
|
// For serverless functions (like Google Cloud Functions)
|
|
export default app;
|
|
|