-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
171 lines (144 loc) · 4.67 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// /** @jsxImportSource https://esm.sh/[email protected]/jsx */
import { createClient } from "npm:@libsql/[email protected]/node";
import { Client, Value } from "npm:@libsql/core/api";
import { type Context, Hono } from "jsr:@hono/hono";
import { serveStatic } from "jsr:@hono/hono/serve-static";
import {
addCharts,
createUserDataTables,
getHTMLDoc,
getSessionFromCookie,
refreshTokensIfExpired,
tileCache,
} from "./utils/index.ts";
import { load } from "https://deno.land/[email protected]/dotenv/mod.ts";
// load from local env if available
await load({ export: true });
console.log("START");
const TURSO_AUTH_TOKEN = Deno.env.get("TURSO_AUTH_TOKEN");
const TURSO_URL = Deno.env.get("TURSO_URL");
const AUTH_TURSO_AUTH_TOKEN = Deno.env.get("AUTH_TURSO_AUTH_TOKEN");
const AUTH_TURSO_URL = Deno.env.get("AUTH_TURSO_URL");
const app = new Hono();
app.use("*", async (c: Context, next: () => Promise<void>) => {
if (!!c.get("env") && !!c.get("db")) return await next();
const env = createClient({
url: AUTH_TURSO_URL || "",
authToken: AUTH_TURSO_AUTH_TOKEN,
});
const db = createClient({
url: TURSO_URL || "",
authToken: TURSO_AUTH_TOKEN,
});
// await createAuthTable(env);
c.set("env", env);
c.set("db", db);
return await next();
});
//serve static files
app.use(
"/assets/*",
serveStatic({
getContent: async (path) => await Deno.readFile(path),
root: "./",
})
);
app.patch(
"/set-user-colour",
getSessionFromCookie,
refreshTokensIfExpired,
async (c: Context) => {
const userId = c.get("userId");
const db: Client = c.get("db");
const body = await c.req.json();
const colour = body.colour;
if (!userId) throw new Error("No session cookie");
if (!db) throw new Error("Db failed to be retrieved");
if (!colour) throw new Error("No colour provided");
const res = await db.execute({
sql: "UPDATE users SET tile_colour = ? WHERE id = ?;",
args: [colour, userId],
});
return c.json(res);
}
);
app.get("/user/:userId/colour", async (c: Context) => {
const db: Client = c.get("db");
const userId = c.req.param("userId");
if (!userId) throw new Error("userId failed to be retrieved from cookie");
if (!db) throw new Error("db failed to be retrieved");
const userRows = await db.execute({
sql: "SELECT tile_colour FROM users WHERE id = ?;",
args: [userId],
});
return c.text(userRows.rows[0][0] as string);
});
app.get(
"/get-user",
getSessionFromCookie,
refreshTokensIfExpired,
async (c: Context) => {
const db: Client = c.get("db");
const userId = c.get("userId");
if (!userId) throw new Error("userId failed to be retrieved from cookie");
if (!db) throw new Error("db failed to be retrieved");
const userRows = await db.execute({
sql: "SELECT * FROM users WHERE id = ?;",
args: [userId],
});
const user: { [key: string]: Value } = {};
for (let i = 0; i < userRows.columns.length; i++) {
user[userRows.columns[i]] = userRows.rows[0][i];
}
return c.json(user);
}
);
app.get(
"/home",
getSessionFromCookie,
refreshTokensIfExpired,
async (c: Context) => {
const env: Client = c.get("env");
const db: Client = c.get("db");
const userId = c.get("userId");
if (!userId) return c.redirect("/auth/login");
try {
await createUserDataTables(c, db, env);
const doc = await getHTMLDoc("index.html");
doc.body = await addCharts(c, doc.body, env);
const docHtmlText = doc.documentElement?.outerHTML;
if (!docHtmlText) throw new Error("Failed to obtain document html");
const doctype = "<!DOCTYPE html>";
const fullHtml = doctype + docHtmlText;
return c.html(fullHtml);
} catch (error) {
console.error(error);
return c.redirect("/auth/login");
}
}
);
app.get("/", async (c: Context) => {
const { logout } = c.req.query();
if (logout) {
c.res.headers.append(
"Set-Cookie",
`session_id=null; HttpOnly; Secure; expires=Thu, 01 Jan 1970 00:00:00 UTC; SameSite=Lax;Path=/`
);
c.res.headers.append(
"Set-Cookie",
`access_token=null; HttpOnly; Secure; expires=Thu, 01 Jan 1970 00:00:00 UTC; SameSite=Lax;Path=/`
);
}
const doc = await getHTMLDoc("welcome.html");
const docHtmlText = doc.documentElement?.outerHTML;
if (!docHtmlText) throw new Error("Failed to obtain document html");
return c.html(docHtmlText);
});
import AuthApp from "./app/auth.ts";
import SubscriptionsApp from "./app/subscription.ts";
import TilesApp from "./app/tiles.ts";
app.route("/tiles", TilesApp);
app.route("/auth", AuthApp);
app.route("/subscription", SubscriptionsApp);
Deno.serve({ hostname: "0.0.0.0", port: 8000 }, app.fetch);
export default app;