feat: remote search index
This commit is contained in:
parent
e7389ce4c0
commit
193366e946
8 changed files with 186 additions and 170 deletions
50
workers/search/index.js
Normal file
50
workers/search/index.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
import { createClient } from '@supabase/supabase-js'
|
||||
|
||||
export default {
|
||||
async fetch(request, env) {
|
||||
const allowedOrigin = 'https://coryd.dev'
|
||||
const origin = request.headers.get('Origin') || ''
|
||||
const referer = request.headers.get('Referer') || ''
|
||||
|
||||
if (!origin.startsWith(allowedOrigin) && !referer.startsWith(allowedOrigin)) return new Response('Forbidden', { status: 403 })
|
||||
|
||||
const supabaseUrl = env.SUPABASE_URL || process.env.SUPABASE_URL
|
||||
const supabaseKey = env.SUPABASE_KEY || process.env.SUPABASE_KEY
|
||||
const supabase = createClient(supabaseUrl, supabaseKey)
|
||||
|
||||
const { searchParams } = new URL(request.url)
|
||||
const query = searchParams.get('q') || ''
|
||||
const types = searchParams.get('type')?.split(',') || []
|
||||
const page = parseInt(searchParams.get('page')) || 1
|
||||
const pageSize = parseInt(searchParams.get('page_size')) || 10
|
||||
const offset = (page - 1) * pageSize
|
||||
|
||||
try {
|
||||
let supabaseQuery = supabase
|
||||
.from('optimized_search_index')
|
||||
.select('*', { count: 'exact' })
|
||||
|
||||
if (types.length > 0) supabaseQuery = supabaseQuery.in('type', types)
|
||||
if (query) supabaseQuery = supabaseQuery.or(`title.ilike.%${query}%,description.ilike.%${query}%`)
|
||||
|
||||
const { data, error, count } = await supabaseQuery.range(offset, offset + pageSize - 1)
|
||||
|
||||
if (error) {
|
||||
console.error('Query error:', error)
|
||||
return new Response('Error fetching data from Supabase', { status: 500 })
|
||||
}
|
||||
|
||||
return new Response(
|
||||
JSON.stringify({ results: data, total: count, 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