Felix Zieger
updates
1a45d5d
import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { createClient } from 'https://esm.sh/@supabase/[email protected]';
import * as Sentry from "https://deno.land/x/sentry/index.mjs";
Sentry.init({
dsn: "https://ca41c3f96489cc1b3e69c9a44704f7ee@o4508722276007936.ingest.de.sentry.io/4508772265558096",
defaultIntegrations: false,
// Performance Monitoring
tracesSampleRate: 1.0,
// Set sampling rate for profiling - this is relative to tracesSampleRate
profilesSampleRate: 1.0,
});
Sentry.setTag('region', Deno.env.get('SB_REGION'));
Sentry.setTag('execution_id', Deno.env.get('SB_EXECUTION_ID'));
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
};
serve(async (req) => {
if (req.method === 'OPTIONS') {
return new Response(null, { headers: corsHeaders });
}
try {
const { gameId } = await req.json();
console.log('Creating session for game:', gameId);
if (!gameId) {
throw new Error('Game ID is required');
}
// Initialize Supabase client
const supabaseUrl = Deno.env.get('SUPABASE_URL')!;
const supabaseKey = Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')!;
const supabase = createClient(supabaseUrl, supabaseKey);
// Verify game exists
const { data: game, error: gameError } = await supabase
.from('games')
.select()
.eq('id', gameId)
.single();
if (gameError || !game) {
throw new Error('Game not found');
}
// Create new session
const { data: session, error: sessionError } = await supabase
.from('sessions')
.insert({
game_id: gameId,
})
.select()
.single();
if (sessionError) {
throw sessionError;
}
console.log('Successfully created session:', session);
return new Response(
JSON.stringify(session),
{ headers: { ...corsHeaders, 'Content-Type': 'application/json' } }
);
} catch (error) {
Sentry.captureException(error)
console.error('Error in create-session:', error);
return new Response(
JSON.stringify({ error: error.message }),
{
status: 500,
headers: { ...corsHeaders, 'Content-Type': 'application/json' }
}
);
}
});