Pepguy commited on
Commit
e430ef5
·
verified ·
1 Parent(s): 27ba337

Update app.js

Browse files
Files changed (1) hide show
  1. app.js +50 -100
app.js CHANGED
@@ -1,114 +1,64 @@
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
-
13
- const CREDENTIALS_PATH = "credentials.json";
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
- async function getAllRedisData() {
25
- const result = [];
26
- const keys = await redis.keys('*');
 
 
 
 
27
 
28
- for (const key of keys) {
 
 
 
29
  const value = await redis.get(key);
30
- result.push({ key, value });
31
  }
32
 
33
- return result;
34
- }
35
- async function uploadToDrive(filename) {
36
- const res = await drive.files.create({
37
- requestBody: {
38
- name: path.basename(filename),
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
- // 3. Upload via your existing `uploadToDrive` (uses service account)
96
- const fileId = await uploadToDrive(filename);
97
- console.log(`Uploaded to Drive, fileId=${fileId}`);
 
 
 
 
 
 
 
98
 
99
- // 4. Clean up temp file
100
- fs.unlinkSync(filename);
101
 
102
- // 5. Return the Drive file ID
103
- res.send(`✅ Backup successful. File ID: ${fileId}`);
104
- } catch (err) {
105
- console.error("Backup error:", err);
106
- res.status(500).send("❌ Backup failed");
107
- }
108
  });
109
 
110
- (async () => {
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);