Skip to content

Commit

Permalink
virtual key
Browse files Browse the repository at this point in the history
Signed-off-by: Mengxin Liu <[email protected]>
  • Loading branch information
oilbeater committed Sep 12, 2024
1 parent 27cdfda commit 1b874ae
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ async function handleChat(c: Context) {
const queryParams = new URLSearchParams(c.req.query()).toString()
const urlWithQueryParams = `${azureEndpoint}?${queryParams}`

const apiKey = c.req.header('api-key')
const realKey = await c.env.MALACCA_USER.get(apiKey)
if (!realKey) {
return c.text('Unauthorized', 401)
}


async function generateCacheKey(urlWithQueryParams: string, body: any): Promise<string> {
const cacheKey = await crypto.subtle.digest(
Expand All @@ -54,10 +60,13 @@ async function handleChat(c: Context) {
return new Response(responseFromCache)
}

const headers = new Headers(c.req.header())
headers.set('api-key', realKey)

const response = await fetch(urlWithQueryParams, {
method: c.req.method,
body: JSON.stringify(body),
headers: c.req.header()
headers: headers
})

const { readable, writable } = new TransformStream()
Expand Down

0 comments on commit 1b874ae

Please sign in to comment.