iper / src /server.ts
GeminiBot
Prevent server crash on unhandled exceptions
6aa271e
raw
history blame
4.52 kB
import { createServer, IncomingMessage, ServerResponse } from "node:http";
import { OpenAIService } from "./openai-service";
const openAIService = new OpenAIService();
const log = (msg: string) => {
const time = new Date().toISOString().split('T')[1].split('.')[0];
console.log(`[${time}] ${msg}`);
};
const HTML_FRONTEND = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Research Playground</title>
<style>
body { font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: #0b0e14; color: #e6edf3; display: flex; align-items: center; justify-content: center; height: 100vh; margin: 0; }
.container { text-align: center; border: 1px solid #30363d; padding: 40px; border-radius: 12px; background: #161b22; max-width: 500px; }
h1 { color: #58a6ff; margin-bottom: 10px; }
p { color: #8b949e; line-height: 1.6; }
.status { display: inline-block; padding: 4px 12px; border-radius: 20px; background: #238636; color: white; font-size: 14px; margin-top: 20px; }
</style>
</head>
<body>
<div class="container">
<h1>🔬 Research API</h1>
<p>This Space hosts a distributed language model inference engine for academic research and integration testing.</p>
<div class="status">System Online</div>
<p style="font-size: 12px; margin-top: 30px;">Authorized access only via encrypted endpoints.</p>
</div>
</body>
</html>
`;
const API_KEY = process.env.API_KEY || "MySecretKey_12345";
const PORT = Number(process.env.PORT) || 7860;
const server = createServer(async (req: IncomingMessage, res: ServerResponse) => {
try {
const url = new URL(req.url || "/", `http://${req.headers.host || "localhost"}`);
const method = req.method;
// CORS Headers
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Authorization",
};
// Helper to send JSON response
const sendJSON = (statusCode: number, data: any) => {
res.writeHead(statusCode, { "Content-Type": "application/json", ...corsHeaders });
res.end(JSON.stringify(data));
};
// Helper to send HTML
const sendHTML = (html: string) => {
res.writeHead(200, { "Content-Type": "text/html", ...corsHeaders });
res.end(html);
};
// Masking Frontend
if (url.pathname === "/" && method === "GET") {
return sendHTML(HTML_FRONTEND);
}
// Handle Preflight
if (method === "OPTIONS") {
res.writeHead(204, corsHeaders);
res.end();
return;
}
// Auth Check
const authHeader = req.headers["authorization"];
if (url.pathname.startsWith("/v1/") && authHeader !== `Bearer ${API_KEY}`) {
return sendJSON(401, { error: "Unauthorized" });
}
// Health Check
if (url.pathname === "/health") {
return sendJSON(200, { status: "online", model: "distributed-v1" });
}
// Chat Completions
if (url.pathname === "/v1/chat/completions" && method === "POST") {
let body = "";
req.on("data", (chunk) => { body += chunk; });
req.on("end", async () => {
try {
// Log full request for debugging
log(`INCOMING REQUEST:\n${body}`);
const jsonBody = JSON.parse(body);
const completion = await openAIService.createChatCompletion(jsonBody);
// Log success summary
log(`SUCCESS: Sent response for ${jsonBody.model}`);
sendJSON(200, completion);
} catch (error: any) {
log(`ERROR: ${error.message}\nSTACK: ${error.stack}`);
sendJSON(500, { error: error.message, details: error.stack });
}
});
return;
}
// Not Found
res.writeHead(404, corsHeaders);
res.end();
} catch (error: any) {
log(`CRITICAL ERR: ${error.message}`);
res.writeHead(500, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Internal Server Error" }));
}
});
// Prevent server from crashing
process.on('uncaughtException', (err) => {
log(`CRITICAL: Uncaught Exception: ${err.message}\n${err.stack}`);
});
process.on('unhandledRejection', (reason, promise) => {
log(`CRITICAL: Unhandled Rejection: ${reason}`);
});
server.listen(PORT, () => {
log(`API Gateway started on port ${PORT}`);
});