const express = require("express"); const axios = require("axios"); const cors = require("cors"); const bodyParser = require("body-parser"); require("dotenv").config(); const app = express(); const PORT = 3000; app.use(cors()); app.use(bodyParser.json()); const GITHUB_API = "https://api.github.com"; const GITHUB_FILE_PATH = "j.json"; const OWNER = process.env.GITHUB_REPO_OWNER; const REPO = process.env.GITHUB_REPO_NAME; const TOKEN = process.env.GITHUB_TOKEN; // === Fetch Notes from GitHub === async function getNotes() { const url = `${GITHUB_API}/repos/${OWNER}/${REPO}/contents/${GITHUB_FILE_PATH}`; try { const response = await axios.get(url, { headers: { Authorization: `Bearer ${TOKEN}` } }); const fileContent = Buffer.from(response.data.content, "base64").toString(); return JSON.parse(fileContent); } catch (error) { return []; } } // === Update Notes in GitHub === async function updateNotes(notes) { const url = `${GITHUB_API}/repos/${OWNER}/${REPO}/contents/${GITHUB_FILE_PATH}`; try { const fileData = await axios.get(url, { headers: { Authorization: `Bearer ${TOKEN}` } }); const sha = fileData.data.sha; const contentEncoded = Buffer.from(JSON.stringify(notes, null, 2)).toString("base64"); await axios.put(url, { message: "Update notes", content: contentEncoded, sha: sha }, { headers: { Authorization: `Bearer ${TOKEN}` } }); return true; } catch (error) { return false; } } // === Get Notes API === app.get("/show-notes", async (req, res) => { const notes = await getNotes(); res.json(notes); }); // === Create Note API === app.post("/create-note", async (req, res) => { const { title, content, image, gameId, user } = req.body; const notes = await getNotes(); let gameTitle = ""; if (gameId) { try { const gameResponse = await axios.get(`https://games.roblox.com/v1/games?universeIds=${gameId}`); gameTitle = gameResponse.data.data[0]?.name || "Unknown Game"; } catch (error) { gameTitle = "Unknown Game"; } } const newNote = { id: notes.length + 1, user, title: gameTitle || title, content, image, approved: false }; notes.push(newNote); const success = await updateNotes(notes); res.json({ success }); }); // === Approve Note API === app.post("/approve-note", async (req, res) => { const { noteId } = req.body; const notes = await getNotes(); const note = notes.find(n => n.id === noteId); if (note) note.approved = true; const success = await updateNotes(notes); res.json({ success }); }); // === Start Server === app.listen(PORT, () => console.log(`Server running on port ${PORT}`));