Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,114 +1,64 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
|
| 7 |
-
const app = express();
|
| 8 |
const redis = createClient();
|
| 9 |
-
|
| 10 |
|
| 11 |
-
const
|
| 12 |
-
|
| 13 |
-
const
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
const auth = new google.auth.GoogleAuth({
|
| 17 |
-
keyFile: 'credentials.json',
|
| 18 |
-
scopes: ['https://www.googleapis.com/auth/drive.file'],
|
| 19 |
-
});
|
| 20 |
-
|
| 21 |
-
const drive = google.drive({ version: 'v3', auth });
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
| 29 |
const value = await redis.get(key);
|
| 30 |
-
|
| 31 |
}
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
},
|
| 41 |
-
media: {
|
| 42 |
-
mimeType: 'application/json',
|
| 43 |
-
body: fs.createReadStream(filename),
|
| 44 |
-
},
|
| 45 |
-
fields: "id",
|
| 46 |
-
});
|
| 47 |
-
|
| 48 |
-
return res.data.id;
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
app.get("/", (_, res) => {
|
| 52 |
-
res.sendFile(path.join(__dirname, "index.html"));
|
| 53 |
-
});
|
| 54 |
-
|
| 55 |
-
app.post("/save", async (req, res) => {
|
| 56 |
-
const { key, value } = req.body;
|
| 57 |
-
if (!key || !value) return res.status(400).send("Missing key or value");
|
| 58 |
-
await redis.set(key, value);
|
| 59 |
-
res.send("Saved");
|
| 60 |
-
});
|
| 61 |
-
|
| 62 |
-
app.get("/get", async (req, res) => {
|
| 63 |
-
const key = req.query.key;
|
| 64 |
-
if (!key) return res.status(400).send("Missing key");
|
| 65 |
-
const value = await redis.get(key);
|
| 66 |
-
res.send(value ?? "Key not found");
|
| 67 |
-
});
|
| 68 |
-
|
| 69 |
-
app.post("/delete", async (req, res) => {
|
| 70 |
-
const key = req.query.key;
|
| 71 |
-
if (!key) return res.status(400).send("Missing key");
|
| 72 |
-
const deleted = await redis.del(key);
|
| 73 |
-
res.send(deleted ? "Deleted" : "Key not found");
|
| 74 |
-
});
|
| 75 |
-
|
| 76 |
-
app.get("/all", async (req, res) => {
|
| 77 |
-
// Ensure Redis is connected first
|
| 78 |
-
console.log("Handling POST /all");
|
| 79 |
-
const data = await getAllRedisData();
|
| 80 |
-
console.log("all data: ", data);
|
| 81 |
-
res.json(data);
|
| 82 |
-
});
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
app.post("/backup", async (_req, res) => {
|
| 86 |
-
try {
|
| 87 |
-
// 1. Fetch all Redis data
|
| 88 |
-
const data = await getAllRedisData();
|
| 89 |
-
|
| 90 |
-
// 2. Write to a temp file (no EACCES)
|
| 91 |
-
const filename = `/tmp/redis_backup_${Date.now()}.json`;
|
| 92 |
-
fs.writeFileSync(filename, JSON.stringify(data, null, 2), { mode: 0o600 });
|
| 93 |
-
console.log(`Wrote backup file: ${filename}`);
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
console.error("Backup error:", err);
|
| 106 |
-
res.status(500).send("❌ Backup failed");
|
| 107 |
-
}
|
| 108 |
});
|
| 109 |
|
| 110 |
-
(
|
| 111 |
-
await redis.connect();
|
| 112 |
-
const port = process.env.PORT || 7860;
|
| 113 |
-
app.listen(port, () => console.log("Server running on", port));
|
| 114 |
-
})();
|
|
|
|
| 1 |
+
// server.js
|
| 2 |
+
import { serve } from "bun";
|
| 3 |
+
import { createClient } from "redis";
|
| 4 |
+
import fs from "fs";
|
| 5 |
+
import path from "path";
|
| 6 |
|
|
|
|
| 7 |
const redis = createClient();
|
| 8 |
+
await redis.connect();
|
| 9 |
|
| 10 |
+
const handler = async (req) => {
|
| 11 |
+
const url = new URL(req.url);
|
| 12 |
+
const { pathname, searchParams } = url;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
// serve index.html
|
| 15 |
+
if (pathname === "/" && req.method === "GET") {
|
| 16 |
+
return new Response(await Bun.file("./index.html").text(), {
|
| 17 |
+
headers: { "Content-Type": "text/html" },
|
| 18 |
+
});
|
| 19 |
+
}
|
| 20 |
|
| 21 |
+
// save key/value
|
| 22 |
+
if (pathname === "/save" && req.method === "POST") {
|
| 23 |
+
const { key, value } = await req.json();
|
| 24 |
+
if (!key || !value) return new Response("Missing key or value", { status: 400 });
|
| 25 |
+
await redis.set(key, value);
|
| 26 |
+
return new Response("Saved");
|
| 27 |
+
}
|
| 28 |
|
| 29 |
+
// get value by key
|
| 30 |
+
if (pathname === "/get" && req.method === "GET") {
|
| 31 |
+
const key = searchParams.get("key");
|
| 32 |
+
if (!key) return new Response("Missing key", { status: 400 });
|
| 33 |
const value = await redis.get(key);
|
| 34 |
+
return new Response(value ?? "Key not found");
|
| 35 |
}
|
| 36 |
|
| 37 |
+
// delete key
|
| 38 |
+
if (pathname === "/delete" && req.method === "POST") {
|
| 39 |
+
const key = searchParams.get("key");
|
| 40 |
+
if (!key) return new Response("Missing key", { status: 400 });
|
| 41 |
+
const deleted = await redis.del(key);
|
| 42 |
+
return new Response(deleted ? "Deleted" : "Key not found");
|
| 43 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
// list all
|
| 46 |
+
if (pathname === "/all" && req.method === "GET") {
|
| 47 |
+
const keys = await redis.keys("*");
|
| 48 |
+
const data = await Promise.all(
|
| 49 |
+
keys.map(async (k) => ({ key: k, value: await redis.get(k) }))
|
| 50 |
+
);
|
| 51 |
+
return new Response(JSON.stringify(data), {
|
| 52 |
+
headers: { "Content-Type": "application/json" },
|
| 53 |
+
});
|
| 54 |
+
}
|
| 55 |
|
| 56 |
+
return new Response("Not found", { status: 404 });
|
| 57 |
+
};
|
| 58 |
|
| 59 |
+
serve({
|
| 60 |
+
fetch: handler,
|
| 61 |
+
port: process.env.PORT ? Number(process.env.PORT) : 7860,
|
|
|
|
|
|
|
|
|
|
| 62 |
});
|
| 63 |
|
| 64 |
+
console.log("Bun server running on port", process.env.PORT || 7860);
|
|
|
|
|
|
|
|
|
|
|
|