-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
55 lines (47 loc) · 1.5 KB
/
index.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
import { Elysia } from "elysia";
import { runPythonWithManifest } from "./src/wasmer";
import { pluginData } from "./src/manifest";
import { cors } from '@elysiajs/cors'
import * as dotenv from "dotenv";
// Reload environment variables
dotenv.config({ override: true });
const app = new Elysia()
.use(cors())
.get("/.well-known/ai-plugin.json", async () => {
// Force reload env vars on each request
dotenv.config({ override: true });
return pluginData();
})
.get("/code", async ({ query }) => {
try {
console.log("Received request");
console.log(query);
// Force reload env vars on each request
dotenv.config({ override: true });
const { code } = query;
if (!code) {
return { success: false, error: "No code provided" };
}
const output = await runPythonWithManifest(code);
// Handle error object returned from runPythonWithManifest
if (typeof output === 'object' && 'error' in output) {
return {
success: false,
error: output.message
};
}
// Handle successful string output
return {
success: true,
output
};
} catch (error) {
console.error("Error executing code:", error);
return {
success: false,
error: error instanceof Error ? error.message : "An unknown error occurred"
};
}
})
.listen(process.env.PORT || 3000);
console.log(`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`);