-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
154 lines (144 loc) Β· 3.17 KB
/
index.ts
1
2
3
4
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { file } from "bun";
import { Database } from "bun:sqlite";
import { Elysia } from "elysia";
import { html } from "@elysiajs/html";
import { cors } from "@elysiajs/cors";
/**
* 1: HTTP count ID
* 2: WebSocket count ID
*/
type ID = 1 | 2;
/** Count DB row */
type Count = {
id: ID;
value: number;
};
const db = new Database("db.sqlite");
initDb();
const app = new Elysia()
.use(html())
.use(
cors({
origin: undefined,
})
)
.ws("/api/ws-count", {
open(ws) {
ws.subscribe("count");
ws.send(`<span id="ws-counter">${get(2)}</span>`);
},
message(_ws, message) {
if (
typeof message !== "object" ||
message === null ||
!("counter_action" in message)
) {
return;
}
switch (message.counter_action) {
case "up":
app.server?.publish("count", `<span id="ws-counter">${up(2)}</span>`);
break;
case "down":
app.server?.publish(
"count",
`<span id="ws-counter">${down(2)}</span>`
);
break;
case "reset":
app.server?.publish(
"count",
`<span id="ws-counter">${reset(2)}</span>`
);
break;
default:
console.error("Unknown action in WebSocket message");
}
},
close(ws) {
ws.unsubscribe("count");
},
})
.get("/", () => {
return file("./index.html").text();
})
.get("/api/count", async () => {
return get(1);
})
.put("/api/up", async () => {
return up(1);
})
.put("/api/down", async () => {
return down(1);
})
.put("/api/reset", async () => {
return reset(1);
})
.listen(4321);
console.log(`π¦ Elysia + π΄ HTMX is running at ${app.server?.url}`);
function initDb() {
db.run(`
CREATE TABLE IF NOT EXISTS count (
id INTEGER PRIMARY KEY AUTOINCREMENT,
value INTEGER NOT NULL
);
`);
db.run(`
INSERT INTO count (id, value)
SELECT *
FROM (
SELECT 1,
0
) AS tmp
WHERE NOT EXISTS (
SELECT id
FROM count
WHERE id = 1
);
`);
db.run(`
INSERT INTO count (id, value)
SELECT *
FROM (
SELECT 2,
0
) AS tmp
WHERE NOT EXISTS (
SELECT id
FROM count
WHERE id = 2
);
`);
}
function isCountRow(row: unknown): row is Count {
return (
typeof row === "object" &&
row !== null &&
"id" in row &&
"value" in row &&
typeof row.id === "number" &&
typeof row.value === "number"
);
}
function countFromDB(id: ID): number {
const row = db.query(`select * from count where id = ${id}`).get();
if (isCountRow(row)) return row.value;
throw new Error("Count not found");
}
function get(id: ID): number {
const row = db.query(`select * from count where id = ${id}`).get();
if (isCountRow(row)) return row.value;
return 0;
}
function up(id: ID): number {
db.run(`update count set value = value + 1 where id = ${id}`);
return countFromDB(id);
}
function down(id: ID): number {
db.run(`update count set value = value - 1 where id = ${id}`);
return countFromDB(id);
}
function reset(id: ID): number {
db.run(`update count set value = 0 where ${id}`);
return countFromDB(id);
}