restructure backend

This commit is contained in:
2026-01-16 18:07:23 +01:00
parent ebcc33b1f7
commit cd9d2e9900
21 changed files with 4323 additions and 69 deletions

18
src/api.ts Normal file
View File

@@ -0,0 +1,18 @@
export async function getImage(req: Bun.BunRequest<"/api/cards/:id">) {
const { id } = req.params;
let file = Bun.file(`images/cards/${id}.png`);
let type = "png";
if (!(await file.exists())) {
file = Bun.file(`images/cards/${id}.jpg`);
type = "jpg";
if (!(await file.exists())) {
file = Bun.file("images/cards/placeholder.png");
type = "png";
console.error(`File for image ${id} does not exist, serving placeholder`);
}
}
return new Response(file, { headers: { "Content-Type": `image/${type}` }, status: 200 });
}

View File

@@ -1,21 +0,0 @@
import type { Card } from "./types";
// Placeholder cards array
let cards: Card[] = [
{ id: "swsh1-1", name: "Celebi V", set: "Swsh1", number: "1", imageUrl: "https://assets.tcgdex.net/de/swsh/swsh1/1/high.png" },
{ id: "swsh12-001", name: "Bluzuk", set: "Swsh12", number: "001", imageUrl: "https://assets.tcgdex.net/de/swsh/swsh12/001/high.png" },
];
export function loadCards() {
// Placeholder: you can later load embeddings.npy + FAISS
console.log("Cards module loaded (currently empty)");
}
export function queryCardById(id: string): Card | null {
return cards.find(c => c.id === id) || null;
}
// Example placeholder: return top N matches
export function queryCardByEmbedding(/* embedding */): Card[] {
return cards.slice(0, 5); // dummy top 5
}

View File

@@ -1,6 +1,5 @@
import { serve, spawn } from 'bun';
import { queryCardByEmbedding, queryCardById } from './embeddings';
import type { Card } from './types';
import { getImage } from './api';
const PYTHON_SERVICE = "http://localhost:5001/query";
@@ -27,29 +26,18 @@ const server = serve({
}
},
},
"/api/cards/:id": async (req) => {
const { id } = req.params;
const card = queryCardById(id);
if (!card) return new Response(JSON.stringify({ error: "Card not found" }), { status: 404, headers: { "Content-Type": "application/json" } });
return new Response(JSON.stringify(card), { headers: { "Content-Type": "application/json" } });
"/api/cards/image/:id": {
async GET(req) {
return getImage(req);
/* const { id } = req.params;
const card = queryCardById(id);
if (!card) return new Response(JSON.stringify({ error: "Card not found" }), { status: 404, headers: { "Content-Type": "application/json" } });
return new Response(JSON.stringify(card), { headers: { "Content-Type": "application/json" } }); */
}
},
"/*": async () => {
return new Response("<h1>Pokemon Card Backend</h1>", { headers: { "Content-Type": "text/html" } });
},
/* "/api/cards/query": {
async POST(req) {
try {
const { embedding } = await req.json() as { embedding: number[] };
if (!embedding) return new Response(JSON.stringify({ error: "Missing embedding" }), { status: 400, headers: { "Content-Type": "application/json" } });
const results: Card[] = queryCardByEmbedding(embedding);
return new Response(JSON.stringify(results), { headers: { "Content-Type": "application/json" } });
} catch (err) {
console.error("Error querying card:", err);
return new Response(JSON.stringify({ error: "Failed to query card" }), { status: 500, headers: { "Content-Type": "application/json" } });
}
},
}, */
},
development: process.env.NODE_ENV !== 'production' && {
@@ -61,4 +49,4 @@ const server = serve({
},
});
console.log(`🚀 Server running at ${server.url}`);
console.log(`Server running at ${server.url}`);

View File

@@ -1,10 +0,0 @@
export interface Card {
id: string;
name: string;
set: string;
number: string;
rarity?: string;
variant?: string;
foil?: boolean;
imageUrl: string;
}