generated from mayneyao/eidos-publish-deno
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
70 lines (64 loc) · 1.7 KB
/
main.ts
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
import { Hono } from "hono";
import { serveStatic } from "hono/deno";
import { DataSpace } from "@eidos/core";
import { NodeServerDatabase } from "./db-server.ts";
import { handleFunctionCall } from "./rpc.ts";
const app = new Hono();
const db = new NodeServerDatabase({
path: "./data/db.sqlite3",
});
try {
const nodes = await db.selectObjects("select * from eidos__tree");
console.log(nodes[0]);
} catch (error) {
console.error('Database error:', error);
console.error('Current working directory:', Deno.cwd());
console.error('Directory contents:', Deno.readDirSync('./data'));
throw error;
}
const dataSpace = new DataSpace({
db: db,
activeUndoManager: false,
dbName: "read",
context: {
setInterval: undefined,
},
isServer: true,
});
// const nodesViaRpc = await dataSpace.listTreeNodes();
// console.log(nodesViaRpc);
app.post("/server/api", async (c) => {
try {
const body = await c.req.json();
const res = await handleFunctionCall(body.data, dataSpace);
const response = new Response(
JSON.stringify({
status: "success",
result: res,
}),
{
headers: {
"content-type": "application/json",
"Cache-Control": "max-age=3600", // Cache for 1 hour
},
}
);
return response;
} catch (error) {
console.error("Error processing request:", error);
return new Response(
JSON.stringify({
status: "error",
message: error instanceof Error ? error.message : "Unknown error",
}),
{
status: 500,
headers: {
"content-type": "application/json",
},
}
);
}
});
app.use("/*", serveStatic({ root: "./www" }));
Deno.serve(app.fetch);