-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
100 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OPENAI_DOMAIN="https://api.openai.com" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,26 +6,21 @@ | |
</a> | ||
</p> | ||
|
||
This is a open source implementation of [OpenCat for Team](https://opencat.app/) Backend for edge platforms. | ||
|
||
Supported platforms: | ||
|
||
- Cloudflare Workers | ||
- Deno | ||
- Deno Deploy *(requires kv beta access)* | ||
|
||
This project uses Cloudflare KV or Deno KV as backend database. | ||
This is a open source implementation of [OpenCat for Team](https://opencat.app/) backend for edge platforms. | ||
|
||
## Run locally with Deno | ||
Supported platforms: | ||
|
||
You need to have Deno >= 1.32 installed. | ||
- [Cloudflare Workers](#deploy-to-cloudflare-workers) | ||
- [Deno](#run-locally-with-Deno) | ||
- [Deno Deploy]() *(requires kv beta access)* | ||
|
||
```sh | ||
deno run -A --unstable deno/index.ts | ||
``` | ||
This project uses Cloudflare KV or Deno KV as backend database. | ||
|
||
## Deploy to Cloudflare Workers | ||
>Before you begin, you need to have a Cloudflare account and be able to use Cloudflare Worker. Have a joy! | ||
## Deploy to Cloudflare Workers With Wrangler | ||
>Before you begin, you need to have a [Cloudflare](https://www.cloudflare.com/) account and be able to use [Cloudflare Workers](https://www.cloudflare.com/zh-cn/products/workers/). Have a joy! | ||
### 1. Git clone the repo and enter repo | ||
```sh | ||
cd ./opencatd_worker | ||
|
@@ -34,9 +29,9 @@ You need to have Deno >= 1.32 installed. | |
```sh | ||
yarn | ||
``` | ||
### 3. Copy `wrangler.bak.toml` to `wrangler.toml` | ||
### 3. Copy `wrangler.toml.bak` to `wrangler.toml` | ||
```sh | ||
cp wrangler.bak.toml wrangler.toml | ||
cp wrangler.toml.bak wrangler.toml | ||
``` | ||
### 4. Create Cloudflare KV Namespace | ||
```sh | ||
|
@@ -58,7 +53,26 @@ You need to have Deno >= 1.32 installed. | |
yarn deploy | ||
``` | ||
|
||
## Run locally with Deno | ||
>You need to have Deno >= 1.32 installed. | ||
### 1. Install Deno | ||
MacOS user can use under command line to install deno. [Read the official document to learn more](https://deno.land/[email protected]/getting_started/installation#download-and-install) | ||
```sh | ||
brew install deno | ||
``` | ||
### 2. Run with Deno | ||
```sh | ||
deno run -A --unstable src/server-deno.ts | ||
``` | ||
|
||
## Deploy to Deno Deploy | ||
|
||
> You need to have Deno >= 1.32 installed. | ||
```sh | ||
deno run -A --unstable src/server-deno.ts | ||
``` | ||
## Dev | ||
Run `yarn start` to start development | ||
```sh | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { serve } from "deno/server"; | ||
import app from "./core/index.ts"; | ||
import { KVDeno } from "./deno/db.ts"; | ||
import { DenoKV } from "./deno/db.ts"; | ||
import { load } from "dotenv"; | ||
|
||
await load(); | ||
|
||
globalThis.tokenGen = crypto.randomUUID; | ||
globalThis.opencatDB = new KVDeno(await Deno.openKv()); | ||
globalThis.opencatDB = new DenoKV(await Deno.openKv()); | ||
|
||
serve(app.fetch); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,23 @@ | ||
import app from "./core/index"; | ||
import { uuid } from "@cfworker/uuid"; | ||
import { KVWorker } from "./worker/db"; | ||
import { WorkerKV } from "./worker/db"; | ||
import { Hono } from "hono"; | ||
import { Bindings } from "./type"; | ||
|
||
globalThis.tokenGen = uuid; | ||
globalThis.opencatDB = new KVWorker(OPENCAT_DB); | ||
export default app; | ||
const server = new Hono<{ Bindings: Bindings }>(); | ||
|
||
server.use((ctx, next) => { | ||
if (!globalThis.tokenGen) { | ||
globalThis.tokenGen = uuid; | ||
} | ||
|
||
if (!globalThis.opencatDB) { | ||
globalThis.opencatDB = new WorkerKV(ctx.env.OPENCAT_DB); | ||
} | ||
|
||
return next(); | ||
}); | ||
|
||
server.route("/", app); | ||
|
||
export default server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
name = "opencatd_worker" | ||
main = "src/index.ts" | ||
main = "src/server-worker.ts" | ||
compatibility_date = "2023-03-30" | ||
routes = [{ pattern = "xxxxxxxx", custom_domain = true }] | ||
kv_namespaces = [{ binding = "OPENCAT_DB", id = "xxxxxxxxxxx" }] | ||
vars = { OPENAI_DOMAIN = "https://api.openai.com" } |