Spaces:
Running
Running
Update app.js
Browse files
app.js
CHANGED
|
@@ -1,84 +1,104 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
const
|
| 4 |
-
|
| 5 |
-
<html>
|
| 6 |
-
<head>
|
| 7 |
-
<title>Timed Event Scheduler</title>
|
| 8 |
-
</head>
|
| 9 |
-
<body>
|
| 10 |
-
<h1>Schedule Timed Event</h1>
|
| 11 |
-
<form onsubmit="submitEvent(event)">
|
| 12 |
-
<label>ID: <input type="text" id="id" required /></label><br />
|
| 13 |
-
<label>Delay (sec): <input type="number" id="delay" required /></label><br />
|
| 14 |
-
<label>Message: <input type="text" id="message" /></label><br />
|
| 15 |
-
<button type="submit">Submit</button>
|
| 16 |
-
</form>
|
| 17 |
-
<script>
|
| 18 |
-
async function submitEvent(e) {
|
| 19 |
-
e.preventDefault();
|
| 20 |
-
const id = document.getElementById('id').value;
|
| 21 |
-
const delay = parseInt(document.getElementById('delay').value) * 1000;
|
| 22 |
-
const message = document.getElementById('message').value;
|
| 23 |
|
| 24 |
-
|
| 25 |
-
method: 'POST',
|
| 26 |
-
headers: { 'Content-Type': 'application/json' },
|
| 27 |
-
body: JSON.stringify({
|
| 28 |
-
id,
|
| 29 |
-
timestamp: Date.now() + delay,
|
| 30 |
-
data: { message }
|
| 31 |
-
})
|
| 32 |
-
});
|
| 33 |
-
|
| 34 |
-
alert(await res.text());
|
| 35 |
-
}
|
| 36 |
-
</script>
|
| 37 |
-
</body>
|
| 38 |
-
</html>
|
| 39 |
-
`;
|
| 40 |
-
|
| 41 |
-
Bun.serve({
|
| 42 |
port: 7860,
|
| 43 |
-
|
| 44 |
const url = new URL(req.url);
|
|
|
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
try {
|
| 52 |
-
const body = await req.json();
|
| 53 |
-
const { id, timestamp, data } = body;
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
//
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
console.log(`⏰ Event due: [${e.id}]`, e.data);
|
| 77 |
-
});
|
| 78 |
-
// Remove fired events
|
| 79 |
-
for (const e of due) {
|
| 80 |
-
const index = events.findIndex(ev => ev.id === e.id);
|
| 81 |
-
if (index !== -1) events.splice(index, 1);
|
| 82 |
}
|
|
|
|
|
|
|
| 83 |
}
|
| 84 |
-
}
|
|
|
|
| 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 |
+
});
|