mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-20 11:31:11 -05:00
Consolidate CORS handling by introducing a shared supabase/functions/_shared/cors.ts and migrate edge functions to import from it. Remove inline cors.ts usage across functions, standardize headers (including traceparent and x-request-id), and prepare for environment-aware origins.
71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
import { serve } from 'https://deno.land/std@0.168.0/http/server.ts';
|
|
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2.57.4';
|
|
import { corsHeaders } from '../_shared/cors.ts';
|
|
import { edgeLogger } from '../_shared/logger.ts';
|
|
import { formatEdgeError } from '../_shared/errorFormatter.ts';
|
|
|
|
serve(async (req: Request) => {
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, { headers: corsHeaders });
|
|
}
|
|
|
|
const requestId = crypto.randomUUID();
|
|
|
|
try {
|
|
edgeLogger.info('Starting scheduled maintenance', { requestId });
|
|
|
|
const supabase = createClient(
|
|
Deno.env.get('SUPABASE_URL')!,
|
|
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!
|
|
);
|
|
|
|
// Run system maintenance (orphaned image cleanup)
|
|
const { data: maintenanceData, error: maintenanceError } = await supabase.rpc('run_system_maintenance');
|
|
|
|
if (maintenanceError) {
|
|
edgeLogger.error('Maintenance failed', { requestId, error: maintenanceError.message });
|
|
} else {
|
|
edgeLogger.info('Maintenance completed', { requestId, result: maintenanceData });
|
|
}
|
|
|
|
// Run pipeline monitoring checks
|
|
const { data: monitoringData, error: monitoringError } = await supabase.rpc('run_pipeline_monitoring');
|
|
|
|
if (monitoringError) {
|
|
edgeLogger.error('Pipeline monitoring failed', { requestId, error: monitoringError.message });
|
|
} else {
|
|
edgeLogger.info('Pipeline monitoring completed', { requestId, result: monitoringData });
|
|
}
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
maintenance: maintenanceData,
|
|
monitoring: monitoringData,
|
|
requestId
|
|
}),
|
|
{
|
|
status: 200,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
|
}
|
|
);
|
|
} catch (error) {
|
|
edgeLogger.error('Maintenance exception', {
|
|
requestId,
|
|
error: formatEdgeError(error)
|
|
});
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: false,
|
|
error: 'Internal server error',
|
|
requestId
|
|
}),
|
|
{
|
|
status: 500,
|
|
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
|
|
}
|
|
);
|
|
}
|
|
});
|