1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const CORS_HEADERS = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"Access-Control-Allow-Headers":
"Content-Type, Access-Control-Allow-Headers, api-key",
"Access-Control-Max-Age": "2592000", // 30 days
} as Record<string, string>;
export default async function(req: Request): Promise<Response> {
if (req.method === "OPTIONS") {
return new Response("ok", { headers: structuredClone(CORS_HEADERS) });
}
try {
const url = new URL(req.url);
const path = url.pathname;
const params = url.searchParams;
const apiKey = req.headers.get("api-key");
if (!apiKey) {
throw new Error("Missing API key");
}
const savedKey = Deno.env.get("QDRANT_API_KEY");
if (savedKey && apiKey !== savedKey) {
throw new Error("Invalid API key");
}
if (!params.has("cluster_id")) {
throw new Error("Missing cluster_id");
}
const cluster_id = params.get("cluster_id");
const qdrantUrl = `https://${cluster_id}.cloud.qdrant.io${path}`;
const response = await fetch(qdrantUrl, {
method: req.method,
headers: req.headers,
body: req.body,
});
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: {
...response.headers,
...structuredClone(CORS_HEADERS),
"Content-Type": "application/json",
},
});
} catch (error) {
const e = error as Error;
console.log(error);
return new Response(e.message, { status: 500 });
}
}