Spaces:
Paused
Paused
| const express = require("express"); | |
| const fs = require("fs"); | |
| const path = require("path"); | |
| const { createClient } = require("redis"); | |
| const { google } = require("googleapis"); | |
| const app = express(); | |
| const redis = createClient(); | |
| app.use(express.json()); | |
| const SCOPES = ["https://www.googleapis.com/auth/drive.file"]; | |
| const CREDENTIALS_PATH = "credentials.json"; | |
| const auth = new google.auth.GoogleAuth({ | |
| keyFile: 'credentials.json', | |
| scopes: ['https://www.googleapis.com/auth/drive.file'], | |
| }); | |
| const drive = google.drive({ version: 'v3', auth }); | |
| async function getAllRedisData() { | |
| const result = []; | |
| for await (const key of redis.get("*")) { | |
| const value = await redis.get(key); | |
| result.push({ key, value }); // Preserve both key and value | |
| } | |
| return result; | |
| } | |
| async function uploadToDrive(filename) { | |
| const res = await drive.files.create({ | |
| requestBody: { | |
| name: path.basename(filename), | |
| }, | |
| media: { | |
| mimeType: 'application/json', | |
| body: fs.createReadStream(filename), | |
| }, | |
| fields: "id", | |
| }); | |
| return res.data.id; | |
| } | |
| app.get("/", (_, res) => { | |
| res.sendFile(path.join(__dirname, "index.html")); | |
| }); | |
| app.post("/save", async (req, res) => { | |
| const { key, value } = req.body; | |
| if (!key || !value) return res.status(400).send("Missing key or value"); | |
| await redis.set(key, value); | |
| res.send("Saved"); | |
| }); | |
| app.get("/get", async (req, res) => { | |
| const key = req.query.key; | |
| if (!key) return res.status(400).send("Missing key"); | |
| const value = await redis.get(key); | |
| res.send(value ?? "Key not found"); | |
| }); | |
| app.post("/delete", async (req, res) => { | |
| const key = req.query.key; | |
| if (!key) return res.status(400).send("Missing key"); | |
| const deleted = await redis.del(key); | |
| res.send(deleted ? "Deleted" : "Key not found"); | |
| }); | |
| app.get("/all", async (req, res) => { | |
| // Ensure Redis is connected first | |
| console.log("Handling POST /all"); | |
| const data = await getAllRedisData(); | |
| console.log("all data: ", data); | |
| res.json(data); | |
| }); | |
| app.post("/backup", async (_req, res) => { | |
| try { | |
| // 1. Fetch all Redis data | |
| const data = await getAllRedisData(); | |
| // 2. Write to a temp file (no EACCES) | |
| const filename = `/tmp/redis_backup_${Date.now()}.json`; | |
| fs.writeFileSync(filename, JSON.stringify(data, null, 2), { mode: 0o600 }); | |
| console.log(`Wrote backup file: ${filename}`); | |
| // 3. Upload via your existing `uploadToDrive` (uses service account) | |
| const fileId = await uploadToDrive(filename); | |
| console.log(`Uploaded to Drive, fileId=${fileId}`); | |
| // 4. Clean up temp file | |
| fs.unlinkSync(filename); | |
| // 5. Return the Drive file ID | |
| res.send(`✅ Backup successful. File ID: ${fileId}`); | |
| } catch (err) { | |
| console.error("Backup error:", err); | |
| res.status(500).send("❌ Backup failed"); | |
| } | |
| }); | |
| (async () => { | |
| await redis.connect(); | |
| const port = process.env.PORT || 7860; | |
| app.listen(port, () => console.log("Server running on", port)); | |
| })(); |