-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
111 lines (96 loc) · 3.66 KB
/
server.ts
File metadata and controls
111 lines (96 loc) · 3.66 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
107
108
109
110
111
import express from "express";
import path from "path";
import { createServer as createViteServer } from "vite";
import { GoogleGenAI } from "@google/genai";
import dotenv from "dotenv";
dotenv.config();
const PORT = 3000;
// Lazy initialization of Gemini client to prevent crash if key is missing
let aiClient: GoogleGenAI | null = null;
function getGeminiClient(): GoogleGenAI | null {
const key = process.env.GEMINI_API_KEY;
if (!key) {
return null;
}
if (!aiClient) {
aiClient = new GoogleGenAI({
apiKey: key,
httpOptions: {
headers: {
'User-Agent': 'aistudio-build',
}
}
});
}
return aiClient;
}
async function startServer() {
const app = express();
app.use(express.json());
// API endpoints go here FIRST
app.get("/api/health", (req, res) => {
res.json({
status: "ok",
current_time: new Date().toISOString(),
gemini_available: !!process.env.GEMINI_API_KEY
});
});
// Code generation using server-side Gemini client
app.post("/api/gemini/generate", async (req, res) => {
try {
const { promptText, contextType, customPrompt } = req.body;
const client = getGeminiClient();
if (!client) {
return res.status(503).json({
error: "GEMINI_API_KEY is not configured in the environment. Please configure it in Settings > Secrets."
});
}
let systemInstruction = `You are a Principal Network Systems Architect and Lead Compiler Engineer specializing in mobile networking, low-level proxies, network routing protocols (SOCKS5, TCP/UDP, HTTP), and proxy-bypassing mechanisms (used to bypass carrier mobile hotspot caps safely).
Your target is to help design a high-performance, ultra-stable, open-source alternative to PDANet+ or TetherFuseNet, commonly called "AetherTunnel".
Provide highly professional, direct, production-grade source code, configuration scripts, and expert architectural designs.
Always enclose key scripts and source files within clean markdown code blocks with correct syntax identifiers (e.g. bash, python, go, java, properties). Keep explanations precise, technical, yet extremely clear and free of fluffy text or marketing hyperbole. Ensure code is fully documented, robust, and directly usable.`;
let contents = "";
if (customPrompt) {
contents = customPrompt;
} else {
contents = `Please generate code/instructions for: ${promptText}.
Focus on technology category: ${contextType || 'general'}.
Make sure to explain security configurations, bypass strategy, performance optimization, and how to execute it in detail.`;
}
const response = await client.models.generateContent({
model: "gemini-3.5-flash",
contents,
config: {
systemInstruction,
temperature: 0.2
}
});
res.json({
text: response.text || "No response generated by the model."
});
} catch (err: any) {
console.error("Gemini Generation Error:", err);
res.status(500).json({
error: err.message || "An error occurred during code generation."
});
}
});
// Vite middleware setup
if (process.env.NODE_ENV !== "production") {
const vite = await createViteServer({
server: { middlewareMode: true },
appType: "spa",
});
app.use(vite.middlewares);
} else {
const distPath = path.join(process.cwd(), "dist");
app.use(express.static(distPath));
app.get("*", (req, res) => {
res.sendFile(path.join(distPath, "index.html"));
});
}
app.listen(PORT, "0.0.0.0", () => {
console.log(`[AetherTunnel Dev Server] Server running on http://0.0.0.0:${PORT}`);
});
}
startServer();