-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (58 loc) · 1.78 KB
/
index.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import init, * as oxigraph from "https://esm.sh/oxigraph@0.3.16/web.js";
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";
await init();
const {
TURTLE_FILE_URL = "https://www.yamaml.org/examples/tbbt_v02.ttl",
BASE_URI = "http://purl.org/yama/examples/2022/tbbt/0.2/",
} = Deno.env.toObject();
async function fetchTurtleFile() {
const response = await fetch(TURTLE_FILE_URL);
return await response.text();
}
async function createStore() {
const turtleContent = await fetchTurtleFile();
const store = new oxigraph.Store();
store.load(turtleContent, "text/turtle", BASE_URI);
return store;
}
const store = await createStore();
async function executeQuery({ store, query }) {
const response = {
query,
results: [],
};
if (!query) {
response.error = "Empty or missing query parameter";
return response;
}
if (!query.trimStart().toUpperCase().startsWith("SELECT ")) {
response.error = "Only SELECT queries are supported";
return response;
}
try {
for (const binding of store.query(query)) {
const result = {};
binding.forEach((value, key) => {
result[key] = value ? value.value : null;
});
response.results.push(result);
}
} catch (error) {
response.error = error.message;
if (Deno.env.get("ENV") === "development") {
response.stack = error.stack;
}
}
return response;
}
async function handleRequest(request) {
const url = new URL(request.url);
const params = url.searchParams;
const query = params.get("query");
if (request.method === "GET") {
const response = await executeQuery({ store, query });
return new Response(JSON.stringify(response, null, 2));
}
return new Response("Unsupported request method", { status: 405 });
}
serve(handleRequest);