Monorepo for wisp.place. A static site hosting service built on top of the AT Protocol.
wisp.place
1import { Elysia } from 'elysia'
2import { cors } from '@elysiajs/cors'
3import { staticPlugin } from '@elysiajs/static'
4import { openapi, fromTypes } from '@elysiajs/openapi'
5
6import type { Config } from './lib/types'
7import { BASE_HOST } from './lib/constants'
8import {
9 createClientMetadata,
10 getOAuthClient,
11 getCurrentKeys
12} from './lib/oauth-client'
13
14const config: Config = {
15 domain: (Bun.env.DOMAIN ?? `https://${BASE_HOST}`) as `https://${string}`,
16 clientName: Bun.env.CLIENT_NAME ?? 'PDS-View'
17}
18
19const client = await getOAuthClient(config)
20
21export const app = new Elysia()
22 .use(
23 openapi({
24 references: fromTypes()
25 })
26 )
27 .use(
28 await staticPlugin({
29 prefix: '/'
30 })
31 )
32 .post('/api/auth/signin', async (c) => {
33 try {
34 const { handle } = await c.request.json()
35 const state = crypto.randomUUID()
36 const url = await client.authorize(handle, { state })
37 return { url: url.toString() }
38 } catch (err) {
39 console.error('Signin error', err)
40 return { error: 'Authentication failed' }
41 }
42 })
43 .get('/api/auth/callback', async (c) => {
44 const params = new URLSearchParams(c.query)
45 const { session } = await client.callback(params)
46 if (!session) return { error: 'Authentication failed' }
47
48 const cookieSession = c.cookie
49 cookieSession.did.value = session.did
50
51 return c.redirect('/')
52 })
53 .get('/client-metadata.json', (c) => {
54 return createClientMetadata(config)
55 })
56 .get('/jwks.json', (c) => {
57 const keys = getCurrentKeys()
58 if (!keys.length) return { keys: [] }
59
60 return {
61 keys: keys.map((k) => {
62 const jwk = k.publicJwk ?? k
63 const { ...pub } = jwk
64 return pub
65 })
66 }
67 })
68 .use(cors())
69 .listen(8000)
70
71console.log(
72 `🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
73)