-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbknd.ts
More file actions
40 lines (32 loc) · 1.14 KB
/
bknd.ts
File metadata and controls
40 lines (32 loc) · 1.14 KB
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
import { createRuntimeApp } from 'bknd/adapter'
import type { Context } from 'hono'
import { Hono } from 'hono'
import { serveStatic } from 'hono/bun'
import config from './bknd.config'
let bkndRuntimeAppInstance: Awaited<ReturnType<typeof createRuntimeApp>> | null = null
export async function getBkndApp() {
if (!bkndRuntimeAppInstance) {
bkndRuntimeAppInstance = await createRuntimeApp(config, {})
}
return bkndRuntimeAppInstance
}
export async function bkndAppFetch(context: Context) {
const app = await getBkndApp()
return app.fetch(context.req.raw)
}
export async function getApi(context: Context, opts?: { verify?: boolean }) {
const bkndApp = await getBkndApp()
const api = bkndApp.getApi({
headers: context.req.raw.headers,
})
if (opts?.verify) {
await api.verifyAuth()
}
return api
}
export const bkndApp = new Hono()
bkndApp.use('/assets/*', serveStatic({ root: './node_modules/bknd/dist/static' }))
bkndApp.use('/uploads/*', serveStatic({ root: './' }))
bkndApp.all('/api/*', async (c) => bkndAppFetch(c))
bkndApp.get('/admin', async (c) => bkndAppFetch(c))
bkndApp.get('/admin/*', async (c) => bkndAppFetch(c))