initial commit
This commit is contained in:
commit
c70fc72952
143 changed files with 13594 additions and 0 deletions
44
workers/search/index.js
Normal file
44
workers/search/index.js
Normal file
|
@ -0,0 +1,44 @@
|
|||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
export default {
|
||||
async fetch(request, env) {
|
||||
const supabaseUrl = env.SUPABASE_URL;
|
||||
const supabaseKey = env.SUPABASE_KEY;
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
|
||||
const { searchParams } = new URL(request.url);
|
||||
const query = searchParams.get("q") || "";
|
||||
const rawTypes = searchParams.getAll("type") || [];
|
||||
const types = rawTypes.length > 0 ? rawTypes[0].split(",") : null;
|
||||
|
||||
const page = parseInt(searchParams.get("page") || "1", 10);
|
||||
const pageSize = parseInt(searchParams.get("pageSize") || "10", 10);
|
||||
const offset = (page - 1) * pageSize;
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase.rpc("search_optimized_index", {
|
||||
search_query: query,
|
||||
page_size: pageSize,
|
||||
page_offset: offset,
|
||||
types: types.length ? types : null,
|
||||
});
|
||||
|
||||
if (error) {
|
||||
console.error("Error fetching search data:", error);
|
||||
return new Response(JSON.stringify({ results: [], total: 0 }), {
|
||||
status: 500,
|
||||
});
|
||||
}
|
||||
|
||||
const total = data.length > 0 ? data[0].total_count : 0;
|
||||
const results = data.map(({ total_count, ...item }) => item);
|
||||
|
||||
return new Response(JSON.stringify({ results, total, page, pageSize }), {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Unexpected error:", error);
|
||||
return new Response("Internal Server Error", { status: 500 });
|
||||
}
|
||||
},
|
||||
};
|
12
workers/search/wrangler.template.toml
Normal file
12
workers/search/wrangler.template.toml
Normal file
|
@ -0,0 +1,12 @@
|
|||
name = "search-worker"
|
||||
main = "./index.js"
|
||||
compatibility_date = "2023-01-01"
|
||||
|
||||
account_id = "${CF_ACCOUNT_ID}"
|
||||
workers_dev = true
|
||||
|
||||
[env.production]
|
||||
name = "search-worker-production"
|
||||
routes = [
|
||||
{ pattern = "coryd.dev/api/search*", zone_id = "${CF_ZONE_ID}" },
|
||||
]
|
Reference in a new issue