minecraft-web/backend/routes/api.js

114 lines
2.8 KiB
JavaScript
Raw Normal View History

2025-12-16 08:40:32 +09:00
import express from "express";
import { getTranslations, getIcons, getGamerules } from "../lib/db.js";
import { getIconUrl } from "../lib/icons.js";
import {
MOD_API_URL,
fetchModStatus,
fetchMotd,
formatStatusForClient,
fetchAllPlayers,
fetchPlayerDetail,
getCachedStatus,
getCachedPlayers,
} from "../lib/minecraft.js";
const router = express.Router();
// 서버 상태 API
router.get("/status", async (req, res) => {
const cached = getCachedStatus();
if (cached) {
res.json(cached);
} else {
const [modStatus, motdData] = await Promise.all([
fetchModStatus(),
fetchMotd(),
]);
res.json(formatStatusForClient(modStatus, motdData));
}
});
// 플레이어 목록 API
router.get("/players", async (req, res) => {
const cached = getCachedPlayers();
if (cached.length > 0) {
res.json(cached);
} else {
const players = await fetchAllPlayers();
res.json(players);
}
});
// 번역 데이터 API
router.get("/translations", (req, res) => {
res.json(getTranslations());
});
// 아이콘 캐시 API
router.get("/icons", (req, res) => {
res.json(getIcons());
});
// 온디맨드 아이콘 API
router.get("/icon/:type/:name", async (req, res) => {
const { type, name } = req.params;
if (!["block", "item", "entity"].includes(type)) {
return res.status(400).json({ error: "Invalid type" });
}
const iconUrl = await getIconUrl(name, type);
if (iconUrl) {
res.json({ icon: iconUrl });
} else {
res.status(404).json({ error: "Icon not found" });
}
});
// 게임룰 API
router.get("/gamerules", (req, res) => {
res.json(getGamerules());
});
// 플레이어 통계 API
router.get("/player/:uuid/stats", async (req, res) => {
const { uuid } = req.params;
try {
const response = await fetch(`${MOD_API_URL}/player/${uuid}/stats`);
if (response.ok) {
res.json(await response.json());
} else {
res.status(404).json({ error: "통계를 불러올 수 없습니다" });
}
} catch (error) {
console.error("[ModAPI] 통계 조회 실패:", error.message);
res.status(500).json({ error: "서버 오류" });
}
});
// 플레이어 상세 정보 API
router.get("/player/:uuid", async (req, res) => {
const { uuid } = req.params;
const player = await fetchPlayerDetail(uuid);
if (player) {
res.json(player);
} else {
res.status(404).json({ error: "플레이어 데이터를 찾을 수 없습니다" });
}
});
// 월드 정보 API
router.get("/worlds", async (req, res) => {
try {
const response = await fetch(`${MOD_API_URL}/worlds`);
if (response.ok) {
res.json(await response.json());
} else {
res.json({ worlds: [] });
}
} catch (error) {
console.error("[ModAPI] 월드 조회 실패:", error.message);
res.json({ worlds: [] });
}
});
export default router;