Skip to content

Commit

Permalink
feat: support proxy common openai api
Browse files Browse the repository at this point in the history
  • Loading branch information
C-Dao committed Apr 1, 2023
1 parent de7e2e6 commit a8c9afb
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
> This Project use Cloudflare KV as backend database.
## Deploy

>Before you begin, you need to have a Cloudflare account and be able to use Cloudflare Worker. Have a joy!
### 1. Git clone the repo and enter repo
```sh
cd ./opencatd_worker
Expand Down Expand Up @@ -41,7 +41,7 @@

### 7. Use wrangler deploy
```sh
yarn deploy
yarn deploy
```


Expand Down
48 changes: 27 additions & 21 deletions src/controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Handler, MiddlewareHandler } from "hono";
import { Bindings, DBConfig, Key, KeyK, User } from "./type";
import { Bindings, DBConfig, Key, User } from "./type";
import { uuid } from "@cfworker/uuid";
import { StatusCode } from "hono/utils/http-status";

export const users: Record<string, Handler<{ Bindings: Bindings }>> = {
async init(ctx) {
Expand All @@ -12,7 +13,7 @@ export const users: Record<string, Handler<{ Bindings: Bindings }>> = {
{
error: "super user already exists, please input token",
},
200
403
);
} else {
const user: User = {
Expand Down Expand Up @@ -47,7 +48,7 @@ export const users: Record<string, Handler<{ Bindings: Bindings }>> = {
await ctx.env.OPENCAT_DB.getWithMetadata<DBConfig>("db::config");

if (!dbConfig) {
return ctx.json({ error: "db config not initialized" });
return ctx.json({ error: "db config not initialized" }, 403);
}

const user: User = {
Expand All @@ -74,7 +75,7 @@ export const users: Record<string, Handler<{ Bindings: Bindings }>> = {
const id = ctx.req.param("id");

if (id == undefined || isNaN(Number(id))) {
return ctx.json({ error: "id is not a number" });
return ctx.json({ error: "id is not a number" }, 403);
}

await ctx.env.OPENCAT_DB.delete(`user::id::${Number(id)}`);
Expand All @@ -88,7 +89,7 @@ export const users: Record<string, Handler<{ Bindings: Bindings }>> = {
);

if (!user) {
return ctx.json({ error: "user not found" });
return ctx.json({ error: "user not found" }, 404);
}

user = { ...user, token: uuid() };
Expand Down Expand Up @@ -116,7 +117,7 @@ export const keys: Record<string, Handler<{ Bindings: Bindings }>> = {
await ctx.env.OPENCAT_DB.getWithMetadata<DBConfig>("db::config");

if (!dbConfig) {
return ctx.json({ error: "db metadata not initialized" });
return ctx.json({ error: "db metadata not initialized" }, 403);
}

const item: Key = {
Expand Down Expand Up @@ -144,7 +145,7 @@ export const keys: Record<string, Handler<{ Bindings: Bindings }>> = {
const id = ctx.req.param("id");

if (id == undefined || isNaN(Number(id))) {
return ctx.json({ error: "id is not a number" });
return ctx.json({ error: "id is not a number" }, 403);
}

await ctx.env.OPENCAT_DB.delete(`key::id::${Number(id)}`);
Expand All @@ -159,27 +160,37 @@ export const root: Record<string, Handler<{ Bindings: Bindings }>> = {
if (user) {
return ctx.json(user.metadata);
} else {
return ctx.json({ error: "not found root user, please init service" });
return ctx.json(
{ error: "not found root user, please init service" },
404
);
}
},
};

export const openai: Record<string, Handler<{ Bindings: Bindings }>> = {
export const openai: Record<
string,
MiddlewareHandler<{ Bindings: Bindings }>
> = {
async proxy(ctx) {
const keys = await ctx.env.OPENCAT_DB.list<Key>({ prefix: "key::id::" });
const randomIndex = Math.floor(Math.random() * keys.keys.length);

const openaiToken = keys.keys[randomIndex].metadata?.key;

let req_headers = new Headers(ctx.req.headers);
const req_headers = new Headers(ctx.req.headers);
const req_querys = new URLSearchParams(ctx.req.query()).toString();

req_headers.set("Authorization", "Bearer " + openaiToken);

const request = new Request(ctx.env.OPENAI_DOMAIN + ctx.req.path, {
body: ctx.req.body,
method: ctx.req.method,
headers: req_headers,
});
const request = new Request(
`${ctx.env.OPENAI_DOMAIN}${ctx.req.path}?${req_querys}`,
{
method: ctx.req.method,
headers: req_headers,
body: ctx.req.body,
}
);

const response = await fetch(request);

Expand All @@ -190,11 +201,7 @@ export const openai: Record<string, Handler<{ Bindings: Bindings }>> = {
ctx.header("access-control-allow-origin", "*");
ctx.header("access-control-allow-credentials", "true");

if (response.ok) {
return ctx.body(response.body, 200);
} else {
return ctx.json({ error: JSON.stringify(await response.json()) }, 200);
}
return ctx.body(response.body, (response.status as StatusCode) || 200);
},
};

Expand Down Expand Up @@ -227,7 +234,6 @@ export const auth: Record<string, MiddlewareHandler<{ Bindings: Bindings }>> = {

async openai(ctx, next) {
const auth = ctx.req.header("Authorization");

if (!auth || !auth.startsWith("Bearer")) {
return ctx.json({ error: "Unauthorized" }, 401);
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ keys.delete("/:id", controller.keys.delete);

root.get("/", controller.root.whoami);

openai.post("/*", controller.openai.proxy);
openai.use("/*", controller.openai.proxy);

middleware.root.use("*", controller.auth.root);
middleware.openai.use("*", controller.auth.openai);
Expand Down

0 comments on commit a8c9afb

Please sign in to comment.