Spaces:
Sleeping
Sleeping
File size: 2,485 Bytes
982b5dc ca1682c 982b5dc ca1682c 982b5dc | 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 | import { NextRequest, NextResponse } from "next/server";
import { getSession } from "auth/server";
const getBackendUrl = () => {
if (process.env.BACKEND_API_URL) return process.env.BACKEND_API_URL;
const spaceId = process.env.SPACE_ID || "";
if (spaceId.startsWith("shyota/")) {
return "https://shyota-claude-code-backend.hf.space";
}
return "https://augment17-claude-code-backend.hf.space";
};
export async function GET(req: NextRequest) {
const session = await getSession();
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
const backendUrl = getBackendUrl();
const apiKey = process.env.BACKEND_API_KEY || "";
try {
const res = await fetch(`${backendUrl}/api/eternity/list`, {
headers: {
"Authorization": `Bearer ${apiKey}`,
},
cache: "no-store",
});
if (!res.ok) {
const txt = await res.text();
return NextResponse.json({ error: txt }, { status: res.status });
}
const data = await res.json();
return NextResponse.json(data);
} catch (err: any) {
return NextResponse.json({ error: err.message }, { status: 500 });
}
}
export async function POST(req: NextRequest) {
const session = await getSession();
if (!session?.user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
}
try {
const body = await req.json();
const { action, ...payload } = body;
const backendUrl = getBackendUrl();
const apiKey = process.env.BACKEND_API_KEY || "";
let targetPath = "";
if (action === "toggle") {
targetPath = "/api/eternity/toggle";
} else if (action === "set-priority") {
targetPath = "/api/eternity/set-priority";
} else if (action === "init") {
targetPath = "/api/eternity/init";
} else {
return NextResponse.json({ error: "Invalid action" }, { status: 400 });
}
const res = await fetch(`${backendUrl}${targetPath}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${apiKey}`,
},
body: JSON.stringify(payload),
});
if (!res.ok) {
const txt = await res.text();
return NextResponse.json({ error: txt }, { status: res.status });
}
const data = await res.json();
return NextResponse.json(data);
} catch (err: any) {
return NextResponse.json({ error: err.message }, { status: 400 });
}
}
|