From f0e9fa2cca0b646d7d9071e743beef8a0410b267 Mon Sep 17 00:00:00 2001 From: nlace <167659833+NLACE-COM@users.noreply.github.com> Date: Thu, 5 Jun 2025 18:54:52 -0400 Subject: [PATCH] Add database integration --- .env | 1 + README.md | 16 +++++++++++++--- app/api/get-access-token/route.ts | 6 ++++++ lib/db.ts | 26 ++++++++++++++++++++++++++ models/Token.ts | 8 ++++++++ package.json | 1 + 6 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 lib/db.ts create mode 100644 models/Token.ts diff --git a/.env b/.env index 759e7b7d6..050e3efe9 100644 --- a/.env +++ b/.env @@ -1,2 +1,3 @@ HEYGEN_API_KEY="your Heygen API key" NEXT_PUBLIC_BASE_API_URL=https://api.heygen.com +DATABASE_URL="your database connection string" diff --git a/README.md b/README.md index b647533a3..713d4f302 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,21 @@ Feel free to play around with the existing code and please leave any feedback fo 4. Enter your HeyGen Enterprise API Token in the `.env` file. Replace `HEYGEN_API_KEY` with your API key. This will allow the Client app to generate secure Access Tokens with which to create interactive sessions. - You can retrieve either the API Key by logging in to HeyGen and navigating to this page in your settings: [https://app.heygen.com/settings?from=&nav=Subscriptions%20%26%20API]. + You can retrieve either the API Key by logging in to HeyGen and navigating to this page in your settings: [https://app.heygen.com/settings?from=&nav=Subscriptions%20%26%20API]. -5. (Optional) If you would like to use the OpenAI features, enter your OpenAI Api Key in the `.env` file. +5. Add your database connection string in the `.env` file as `DATABASE_URL`. This will be used by the server API routes for storing generated access tokens. -6. Run `npm run dev` +6. (Optional) If you would like to use the OpenAI features, enter your OpenAI Api Key in the `.env` file. + +7. Run `npm run dev` + +### Environment variables + +``` +HEYGEN_API_KEY=your Heygen API key +NEXT_PUBLIC_BASE_API_URL=https://api.heygen.com +DATABASE_URL=your database connection string +``` ### Starting sessions diff --git a/app/api/get-access-token/route.ts b/app/api/get-access-token/route.ts index 57a391732..7dc7dedbb 100644 --- a/app/api/get-access-token/route.ts +++ b/app/api/get-access-token/route.ts @@ -1,3 +1,6 @@ +import connect from "@/lib/db"; +import Token from "@/models/Token"; + const HEYGEN_API_KEY = process.env.HEYGEN_API_KEY; export async function POST() { @@ -18,6 +21,9 @@ export async function POST() { const data = await res.json(); + await connect(); + await Token.create({ token: data.data.token }); + return new Response(data.data.token, { status: 200, }); diff --git a/lib/db.ts b/lib/db.ts new file mode 100644 index 000000000..771366be6 --- /dev/null +++ b/lib/db.ts @@ -0,0 +1,26 @@ +import mongoose from "mongoose"; + +const { DATABASE_URL } = process.env; + +if (!DATABASE_URL) { + throw new Error("DATABASE_URL is not defined"); +} + +let cached = (global as any).mongoose; + +if (!cached) { + cached = (global as any).mongoose = { conn: null, promise: null }; +} + +async function connect() { + if (cached.conn) return cached.conn; + + if (!cached.promise) { + cached.promise = mongoose.connect(DATABASE_URL); + } + cached.conn = await cached.promise; + + return cached.conn; +} + +export default connect; diff --git a/models/Token.ts b/models/Token.ts new file mode 100644 index 000000000..e9b137eee --- /dev/null +++ b/models/Token.ts @@ -0,0 +1,8 @@ +import { Schema, model, models } from "mongoose"; + +const TokenSchema = new Schema({ + token: { type: String, required: true }, + createdAt: { type: Date, default: Date.now }, +}); + +export default models.Token || model("Token", TokenSchema); diff --git a/package.json b/package.json index 219103816..2f9770f09 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "@radix-ui/react-switch": "^1.1.4", "@radix-ui/react-toggle-group": "^1.1.3", "ahooks": "^3.8.4", + "mongoose": "^8.15.1", "next": "^15.3.0", "openai": "^4.52.1", "react": "^19.1.0",