-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
106 lines (90 loc) · 3.18 KB
/
Copy pathserver.ts
File metadata and controls
106 lines (90 loc) · 3.18 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import express from "express";
import { createServer as createViteServer } from "vite";
import path from "path";
import { fileURLToPath } from "url";
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
async function startServer() {
const app = express();
const PORT = 3000;
app.use(express.json());
// Wolfram Alpha Proxy
const wolframCache = new Map<string, { data: string, ts: number }>();
const CACHE_TTL = 1000 * 60 * 60; // 1 hour
app.get("/api/wolfram", async (req, res) => {
const query = req.query.input as string;
const appId = process.env.WOLFRAM_APP_ID;
if (!appId) {
return res.status(500).json({ error: "Wolfram AppID not configured" });
}
if (!query || query.trim() === '') {
return res.status(400).json({ error: "Query parameter is required" });
}
// Check cache
const cached = wolframCache.get(query);
if (cached && Date.now() - cached.ts < CACHE_TTL) {
console.log(`Wolfram Cache Hit: ${query}`);
return res.send(cached.data);
}
try {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // Increased to 30s
const response = await fetch(
`https://api.wolframalpha.com/v1/result?appid=${appId}&i=${encodeURIComponent(query)}`,
{ signal: controller.signal as any }
);
clearTimeout(timeoutId);
const data = await response.text();
// Store in cache
wolframCache.set(query, { data, ts: Date.now() });
res.send(data);
} catch (error) {
if ((error as any).name === 'AbortError') {
res.status(504).json({ error: "Wolfram Alpha timed out" });
} else {
console.error("Wolfram Proxy Error:", error);
res.status(500).json({ error: "Failed to fetch from Wolfram Alpha" });
}
}
});
// GitHub Proxy
app.get("/api/github/repo", async (req, res) => {
const { owner, repo, path: filePath } = req.query;
if (!owner || !repo) {
return res.status(400).json({ error: "Owner and repo are required" });
}
try {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${filePath || ""}`
);
if (!response.ok) {
return res.status(response.status).json({ error: `GitHub API error: ${response.statusText}` });
}
const data = await response.json();
res.json(data);
} catch (error) {
console.error("GitHub Proxy Error:", error);
res.status(500).json({ error: "Failed to fetch from GitHub" });
}
});
// Vite middleware for development
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
app.use(express.static(path.resolve(__dirname, "dist")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "dist", "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`Server running on http://localhost:${PORT}`);
});
}
startServer();