Pepguy commited on
Commit
03d1608
·
verified ·
1 Parent(s): e2c6f21

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +96 -104
app.js CHANGED
@@ -1,104 +1,96 @@
1
- import { serve } from "bun";
2
- import { createClient } from "redis";
3
-
4
- const redis = createClient(); // defaults to localhost:6379
5
- await redis.connect();
6
-
7
- serve({
8
- port: 7860,
9
- async fetch(req) {
10
- const url = new URL(req.url);
11
- const method = req.method;
12
-
13
- // HTML test UI
14
- if (url.pathname === "/" && method === "GET") {
15
- return new Response(`
16
- <!DOCTYPE html>
17
- <html>
18
- <head><title>Redis Test UI</title></head>
19
- <body>
20
- <h1>Redis Testing Interface</h1>
21
-
22
- <h2>Save Key</h2>
23
- <form id="saveForm">
24
- Key: <input name="key" /> Value: <input name="value" />
25
- <button type="submit">Save</button>
26
- </form>
27
-
28
- <h2>Get Key</h2>
29
- <form id="getForm">
30
- Key: <input name="getKey" />
31
- <button type="submit">Fetch</button>
32
- </form>
33
-
34
- <h2>Delete Key</h2>
35
- <form id="delForm">
36
- Key: <input name="delKey" />
37
- <button type="submit">Delete</button>
38
- </form>
39
-
40
- <pre id="out"></pre>
41
-
42
- <script>
43
- const out = document.getElementById("out");
44
-
45
- document.getElementById("saveForm").onsubmit = async (e) => {
46
- e.preventDefault();
47
- const key = e.target.key.value;
48
- const value = e.target.value.value;
49
- const res = await fetch("/save", {
50
- method: "POST",
51
- headers: { "Content-Type": "application/json" },
52
- body: JSON.stringify({ key, value }),
53
- });
54
- out.textContent = await res.text();
55
- };
56
-
57
- document.getElementById("getForm").onsubmit = async (e) => {
58
- e.preventDefault();
59
- const key = e.target.getKey.value;
60
- const res = await fetch("/get?key=" + key);
61
- out.textContent = await res.text();
62
- };
63
-
64
- document.getElementById("delForm").onsubmit = async (e) => {
65
- e.preventDefault();
66
- const key = e.target.delKey.value;
67
- const res = await fetch("/delete?key=" + key, { method: "DELETE" });
68
- out.textContent = await res.text();
69
- };
70
- </script>
71
- </body>
72
- </html>
73
- `, {
74
- headers: { "Content-Type": "text/html" },
75
- });
76
- }
77
-
78
- // Save key-value
79
- if (url.pathname === "/save" && method === "POST") {
80
- const { key, value } = await req.json();
81
- if (!key || !value) return new Response("Missing key or value", { status: 400 });
82
- await redis.set(key, value);
83
- return new Response("Saved: " + key);
84
- }
85
-
86
- // Get key
87
- if (url.pathname === "/get" && method === "GET") {
88
- const key = url.searchParams.get("key");
89
- if (!key) return new Response("Missing key", { status: 400 });
90
- const val = await redis.get(key);
91
- return new Response(val ?? "Key not found");
92
- }
93
-
94
- // Delete key
95
- if (url.pathname === "/delete" && method === "DELETE") {
96
- const key = url.searchParams.get("key");
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
+ })();