Commit ba4d17c2 authored by duanjinfei's avatar duanjinfei

add function

parent 46a15467
...@@ -4,19 +4,51 @@ ...@@ -4,19 +4,51 @@
// Setup type definitions for built-in Supabase Runtime APIs // Setup type definitions for built-in Supabase Runtime APIs
import "jsr:@supabase/functions-js/edge-runtime.d.ts" import "jsr:@supabase/functions-js/edge-runtime.d.ts"
import { createClient } from 'jsr:@supabase/supabase-js@2'
console.log("Hello from Functions!")
Deno.serve(async (req) => { Deno.serve(async (req) => {
const { name } = await req.json() try {
const data = { // const { name } = await req.json()
message: `Hello ${name}!`, const supabase = createClient(
} Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
{ global: { headers: { Authorization: req.headers.get('Authorization')! } } }
)
const { data: userData, error: userError } = await supabase.from('user').select('*').order('rank', { ascending: false }).limit(100);
if (userError) {
console.error('Error fetching top 100 users:', error);
return new Response(JSON.stringify({ error: 'Failed to fetch users' }), { status: 500 });
}
// 2. 将数据 JSON 序列化
const jsonData = JSON.stringify(userData);
return new Response( // 3. 生成文件名
JSON.stringify(data), const timestamp = Math.floor(Date.now() / 3600000) * 3600; // 计算整点的时间戳
{ headers: { "Content-Type": "application/json" } }, const fileName = `user_top_100_${timestamp}.json`;
)
const bucketName = 'cache'; // 替换为你的存储桶名称
const { error: uploadError } = await supabase.storage
.from(bucketName)
.upload(fileName, new Blob([jsonData]), {
contentType: 'application/json',
upsert: true, // 如果文件已存在则覆盖
});
if (uploadError) {
console.error('Error uploading file to storage:', uploadError);
return new Response(JSON.stringify({ error: 'Failed to upload file' }), { status: 500 });
}
return new Response(JSON.stringify({ data }), {
headers: { 'Content-Type': 'application/json' },
status: 200,
})
} catch (err) {
return new Response(String(err?.message ?? err), { status: 500 })
}
}) })
/* To invoke locally: /* To invoke locally:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment