import { serve } from "bun"; import { getMatchInfo, listEvent } from "./utils"; import index from "./index.html"; import { XCXAPI } from "./services/xcxApi"; if (!process.env.KAIQIUCC_TOKEN) { console.error('env KAIQIUCC_TOKEN not found'); process.exit(1); } const xcxApi = new XCXAPI(process.env.KAIQIUCC_TOKEN ?? ''); const server = serve({ port: process.env.PORT || 3000, routes: { // Serve index.html for all unmatched routes. "/*": index, "/api/events/:clubid": { async GET(req) { const data = await listEvent(req.params.clubid); return Response.json(data); } }, "/api/match/:matchId": { async GET(req) { const data = await getMatchInfo(req.params.matchId); return Response.json(data); } }, "/api/match/:matchId/:itemId": { async GET(req) { const { matchId, itemId } = req.params; const data = await xcxApi.getMemberDetail(matchId, itemId); return Response.json(data); } }, "/api/user/:uid": { async GET(req) { const uid = req.params.uid; const profile = await xcxApi.getAdvProfile(uid); return Response.json(profile); }, }, "/api/user/:uid/games": { async GET(req) { const uid = req.params.uid; const search = new URL(req.url).searchParams; const page = Number(search.get('page')) ?? 0; const resp = await xcxApi.getGameList(uid, page); return Response.json(resp); }, }, "/api/user/:uid/tags": { async GET(req) { const uid = req.params.uid; const profile = await xcxApi.getPlayerTags(uid); return Response.json(profile); }, } }, development: process.env.NODE_ENV !== "production" && { // Enable browser hot reloading in development hmr: true, // Echo console logs from the browser to the server console: true, }, }); console.log(`🚀 Server running at ${server.url}`);