(app: Express)
| 75 | } |
| 76 | |
| 77 | export async function registerRoutes(app: Express): Promise<Server> { |
| 78 | app.use(validateInput); |
| 79 | |
| 80 | const getSessionId = (req: Request): string => { |
| 81 | return ensureSessionId(req); |
| 82 | }; |
| 83 | |
| 84 | app.get("/api/challenge/:id", rateLimit(60, 60000), async (req: Request, res: Response) => { |
| 85 | try { |
| 86 | const sanitizedId = sanitizeChallengeId(req.params.id); |
| 87 | if (!sanitizedId) { |
| 88 | return res.status(400).json({ error: "Định dạng ID thử thách không hợp lệ" }); |
| 89 | } |
| 90 | |
| 91 | const challenge = await storage.getChallenge(sanitizedId); |
| 92 | |
| 93 | if (!challenge) { |
| 94 | return res.status(404).json({ error: "Không tìm thấy thử thách" }); |
| 95 | } |
| 96 | |
| 97 | const { flag, ...challengeWithoutFlag } = challenge; |
| 98 | return res.json(challengeWithoutFlag); |
| 99 | } catch (error) { |
| 100 | console.error("Error fetching challenge:", error); |
| 101 | return res.status(500).json({ error: "Lỗi máy chủ nội bộ" }); |
| 102 | } |
| 103 | }); |
| 104 | |
| 105 | app.get("/api/hints/:challengeId", rateLimit(60, 60000), async (req: Request, res: Response) => { |
| 106 | try { |
| 107 | const sanitizedId = sanitizeChallengeId(req.params.challengeId); |
| 108 | if (!sanitizedId) { |
| 109 | return res.status(400).json({ error: "Định dạng ID thử thách không hợp lệ" }); |
| 110 | } |
| 111 | |
| 112 | const sessionId = getSessionId(req); |
| 113 | const allHints = await storage.getHintsByChallenge(sanitizedId); |
| 114 | const unlockedHintIds = await storage.getUnlockedHints(sessionId, sanitizedId); |
| 115 | |
| 116 | const hints = allHints.map(hint => { |
| 117 | const isUnlocked = unlockedHintIds.includes(hint.id); |
| 118 | return { |
| 119 | id: hint.id, |
| 120 | challengeId: hint.challengeId, |
| 121 | order: hint.order, |
| 122 | content: isUnlocked ? hint.content : null, |
| 123 | pointsCost: hint.pointsCost, |
| 124 | unlocked: isUnlocked, |
| 125 | }; |
| 126 | }); |
| 127 | |
| 128 | return res.json(hints); |
| 129 | } catch (error) { |
| 130 | console.error("Error fetching hints:", error); |
| 131 | return res.status(500).json({ error: "Lỗi máy chủ nội bộ" }); |
| 132 | } |
| 133 | }); |
| 134 |
no test coverage detected