Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,104 +1,96 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
const
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
if (!key) return new Response("Missing key", { status: 400 });
|
| 98 |
-
const deleted = await redis.del(key);
|
| 99 |
-
return new Response(deleted ? "Deleted" : "Key not found");
|
| 100 |
-
}
|
| 101 |
-
|
| 102 |
-
return new Response("404 Not Found", { status: 404 });
|
| 103 |
-
}
|
| 104 |
-
});
|
|
|
|
| 1 |
+
const express = require("express");
|
| 2 |
+
const fs = require("fs");
|
| 3 |
+
const path = require("path");
|
| 4 |
+
const { createClient } = require("redis");
|
| 5 |
+
const { google } = require("googleapis");
|
| 6 |
+
|
| 7 |
+
const app = express();
|
| 8 |
+
const redis = createClient();
|
| 9 |
+
app.use(express.json());
|
| 10 |
+
|
| 11 |
+
const SCOPES = ["https://www.googleapis.com/auth/drive.file"];
|
| 12 |
+
const TOKEN_PATH = "token.json";
|
| 13 |
+
const CREDENTIALS_PATH = "credentials.json";
|
| 14 |
+
|
| 15 |
+
async function getAuthClient() {
|
| 16 |
+
const credentials = JSON.parse(fs.readFileSync(CREDENTIALS_PATH));
|
| 17 |
+
const token = JSON.parse(fs.readFileSync(TOKEN_PATH));
|
| 18 |
+
|
| 19 |
+
const auth = new google.auth.OAuth2(
|
| 20 |
+
credentials.installed.client_id,
|
| 21 |
+
credentials.installed.client_secret,
|
| 22 |
+
credentials.installed.redirect_uris[0]
|
| 23 |
+
);
|
| 24 |
+
auth.setCredentials(token);
|
| 25 |
+
return auth;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
async function getAllRedisData() {
|
| 29 |
+
const keys = await redis.keys("*");
|
| 30 |
+
const result = {};
|
| 31 |
+
for (const key of keys) result[key] = await redis.get(key);
|
| 32 |
+
return result;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
async function uploadToDrive(filename) {
|
| 36 |
+
const auth = await getAuthClient();
|
| 37 |
+
const drive = google.drive({ version: "v3", auth });
|
| 38 |
+
|
| 39 |
+
const fileMetadata = { name: path.basename(filename) };
|
| 40 |
+
const media = {
|
| 41 |
+
mimeType: "application/json",
|
| 42 |
+
body: fs.createReadStream(filename),
|
| 43 |
+
};
|
| 44 |
+
|
| 45 |
+
const res = await drive.files.create({
|
| 46 |
+
resource: fileMetadata,
|
| 47 |
+
media,
|
| 48 |
+
fields: "id",
|
| 49 |
+
});
|
| 50 |
+
|
| 51 |
+
return res.data.id;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
app.get("/", (_, res) => {
|
| 55 |
+
res.sendFile(path.join(__dirname, "index.html"));
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
app.post("/save", async (req, res) => {
|
| 59 |
+
const { key, value } = req.body;
|
| 60 |
+
if (!key || !value) return res.status(400).send("Missing key or value");
|
| 61 |
+
await redis.set(key, value);
|
| 62 |
+
res.send("Saved");
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
app.get("/get", async (req, res) => {
|
| 66 |
+
const key = req.query.key;
|
| 67 |
+
if (!key) return res.status(400).send("Missing key");
|
| 68 |
+
const value = await redis.get(key);
|
| 69 |
+
res.send(value ?? "Key not found");
|
| 70 |
+
});
|
| 71 |
+
|
| 72 |
+
app.delete("/delete", async (req, res) => {
|
| 73 |
+
const key = req.query.key;
|
| 74 |
+
if (!key) return res.status(400).send("Missing key");
|
| 75 |
+
const deleted = await redis.del(key);
|
| 76 |
+
res.send(deleted ? "Deleted" : "Key not found");
|
| 77 |
+
});
|
| 78 |
+
|
| 79 |
+
app.get("/all", async (_, res) => {
|
| 80 |
+
const data = await getAllRedisData();
|
| 81 |
+
res.json(data);
|
| 82 |
+
});
|
| 83 |
+
|
| 84 |
+
app.get("/backup", async (_, res) => {
|
| 85 |
+
const data = await getAllRedisData();
|
| 86 |
+
const filename = `redis_backup_${Date.now()}.json`;
|
| 87 |
+
fs.writeFileSync(filename, JSON.stringify(data, null, 2));
|
| 88 |
+
const fileId = await uploadToDrive(filename);
|
| 89 |
+
res.send("Uploaded. File ID: " + fileId);
|
| 90 |
+
});
|
| 91 |
+
|
| 92 |
+
(async () => {
|
| 93 |
+
await redis.connect();
|
| 94 |
+
const port = process.env.PORT || 7860;
|
| 95 |
+
app.listen(port, () => console.log("Server running on", port));
|
| 96 |
+
})();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|