|
|
import { serve } from "https://deno.land/std/http/server.ts"; |
|
|
|
|
|
const handler = async (request) => { |
|
|
const url = new URL(request.url); |
|
|
|
|
|
|
|
|
if (request.method === "OPTIONS") { |
|
|
return new Response(null, { |
|
|
headers: { |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
"Access-Control-Allow-Methods": "POST, GET, OPTIONS", |
|
|
"Access-Control-Allow-Headers": "Content-Type, Authorization", |
|
|
"Access-Control-Max-Age": "86400", |
|
|
}, |
|
|
}); |
|
|
} |
|
|
|
|
|
const TOKEN = Deno.env.get("TOKEN"); |
|
|
|
|
|
|
|
|
if (TOKEN) { |
|
|
const authHeader = request.headers.get("Authorization"); |
|
|
if (!authHeader || authHeader !== `Bearer ${TOKEN}`) { |
|
|
return new Response("Unauthorized", { |
|
|
status: 401, |
|
|
headers: { |
|
|
"Content-Type": "application/json", |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
}, |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (url.pathname === "/v1/models" && request.method === "GET") { |
|
|
const models = { |
|
|
object: "list", |
|
|
data: [ |
|
|
{ |
|
|
id: "deepseek-ai/DeepSeek-R1-0528-Turbo", |
|
|
object: "model", |
|
|
created: 1624980000, |
|
|
owned_by: "deepseek-ai", |
|
|
}, |
|
|
{ |
|
|
id: "deepseek-ai/DeepSeek-V3-0324-Turbo", |
|
|
object: "model", |
|
|
created: 1632000000, |
|
|
owned_by: "deepseek-ai", |
|
|
}, |
|
|
{ |
|
|
id: "deepseek-ai/DeepSeek-R1-Distill-Llama-70B", |
|
|
object: "model", |
|
|
created: 1640000000, |
|
|
owned_by: "deepseek-ai", |
|
|
}, |
|
|
{ |
|
|
id: "zai-org/GLM-4.6", |
|
|
object: "model", |
|
|
created: 1720000000, |
|
|
owned_by: "zai-org", |
|
|
}, |
|
|
{ |
|
|
id: "moonshotai/Kimi-K2-Instruct", |
|
|
object: "model", |
|
|
created: 1721000000, |
|
|
owned_by: "moonshotai", |
|
|
}, |
|
|
{ |
|
|
id: "deepseek-ai/DeepSeek-V3.2-Exp", |
|
|
object: "model", |
|
|
created: 1722000000, |
|
|
owned_by: "deepseek-ai", |
|
|
}, |
|
|
{ |
|
|
id: "deepseek-ai/DeepSeek-V3.1-Terminus", |
|
|
object: "model", |
|
|
created: 1723000000, |
|
|
owned_by: "deepseek-ai", |
|
|
}, |
|
|
], |
|
|
}; |
|
|
|
|
|
return new Response(JSON.stringify(models), { |
|
|
headers: { |
|
|
"Content-Type": "application/json", |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
}, |
|
|
}); |
|
|
} |
|
|
|
|
|
|
|
|
if (url.pathname === "/v1/chat/completions" && request.method === "POST") { |
|
|
try { |
|
|
const body = await request.json(); |
|
|
const headers = new Headers({ |
|
|
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0", |
|
|
"Accept": "text/event-stream", |
|
|
"Accept-Encoding": "gzip, deflate, br, zstd", |
|
|
"Content-Type": "application/json", |
|
|
"sec-ch-ua-platform": "Windows", |
|
|
"X-Deepinfra-Source": "web-page", |
|
|
"sec-ch-ua": '"Not(A:Brand";v="99", "Microsoft Edge";v="133", "Chromium";v="133"', |
|
|
"sec-ch-ua-mobile": "?0", |
|
|
"Origin": "https://deepinfra.com", |
|
|
"Sec-Fetch-Site": "same-site", |
|
|
"Sec-Fetch-Mode": "cors", |
|
|
"Sec-Fetch-Dest": "empty", |
|
|
"Referer": "https://deepinfra.com/", |
|
|
}); |
|
|
|
|
|
const response = await fetch("https://api.deepinfra.com/v1/openai/chat/completions", { |
|
|
method: "POST", |
|
|
headers: headers, |
|
|
body: JSON.stringify(body), |
|
|
}); |
|
|
|
|
|
return new Response(response.body, { |
|
|
status: response.status, |
|
|
statusText: response.statusText, |
|
|
headers: { |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
"Content-Type": response.headers.get("Content-Type") || "application/json", |
|
|
}, |
|
|
}); |
|
|
} catch (error) { |
|
|
return new Response(JSON.stringify({ error: error.message }), { |
|
|
status: 500, |
|
|
headers: { |
|
|
"Content-Type": "application/json", |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
}, |
|
|
}); |
|
|
} |
|
|
} |
|
|
|
|
|
return new Response("Not Found", { |
|
|
status: 404, |
|
|
headers: { |
|
|
"Access-Control-Allow-Origin": "*", |
|
|
} |
|
|
}); |
|
|
}; |
|
|
|
|
|
const port = Deno.env.get("PORT") ? parseInt(Deno.env.get("PORT")) : 7860; |
|
|
console.log(`HTTP server running. Access it at: http://localhost:${port}/`); |
|
|
serve(handler, { port }); |
|
|
|