summaryrefslogtreecommitdiff
path: root/src/index.tsx
blob: ea799029755f6b0600f6683f5e918cc43d368351 (plain) (blame)
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
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { createClient } from "@libsql/client";
import { drizzle } from "drizzle-orm/libsql";
import * as schema from "./db/schema.js";
import authRouter, { getSession, LoginForm } from "./auth.js";
import { eq } from "drizzle-orm";

export const RP_ID = "localhost"; // "uneven.0m.nu";
export const ORIGIN = `http://${RP_ID}`;

let app = new Hono();
export let db = drizzle(createClient({ url: "file:data.db" }), { schema });

app.get("/groups", async c => {
  let session = await getSession(c);
  if (!session) return c.html("Must be logged in");
  let user = await db.query.userTable.findFirst({ where: user => eq(user.id, session.user.id), with: { groups: { with: { group: true } } } });
  if (!user) return c.html("Huh?");

  return c.html(<ul>{
    user.groups.map(group => <li>{group.group.name}</li>)
  }</ul>);
});

app.get("/", c => c.html(
  <html lang="en">
    <head>
      <meta charset="UTF-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <script src="https://unpkg.com/htmx.org@2.0.6/dist/htmx.min.js" integrity="sha384-Akqfrbj/HpNVo8k11SXBb6TlBWmXXlYQrCSqEWmyKJe+hDm3Z/B2WVG4smwBkRVm" crossorigin="anonymous"></script>
      <script src="https://unpkg.com/hyperscript.org@0.9.14" integrity="sha384-NzchC8z9HmP/Ed8cheGl9XuSrFSkDNHPiDl+ujbHE0F0I7tWC4rUnwPXP+7IvVZv" crossorigin="anonymous"></script>
      <script src="https://unpkg.com/@simplewebauthn/browser/dist/bundle/index.umd.min.js" integrity="sha384-x+9k/LwnOU31Uw0BjGIuH0mJYPM4b5yBa/0GkqcR5tlgphBf9LtXYySTFNK/UtL3" crossorigin="anonymous"></script>
      <title>Uneven</title>
    </head>
    <body>
      <LoginForm />
      <button hx-get="/button" hx-swap="outerHTML">click me!</button>
      <div hx-on-load="/groups" hx-swap="outerHTML" />
    </body>
  </html>
));

let colors = ["red", "green", "blue"];
app.get("/button", async c => {
  let session = await getSession(c);
  return c.html(
    <button
      hx-get="/button"
      hx-swap="outerHTML"
      style={{ backgroundColor: colors[Math.floor(Math.random() * colors.length)] }}
    >disco button! {session?.user.name}</button>
  );
});

app.route("/auth", authRouter);

serve({
  fetch: app.fetch,
  port: 80,
}, info => console.log(`Server is running on http://localhost:${info.port}`));