-
-
Notifications
You must be signed in to change notification settings - Fork 194
. #446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vtempest
wants to merge
1
commit into
AOSSIE-Org:main
Choose a base branch
from
vtempest:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
. #446
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,13 @@ | ||
| # Copy to `.dev.vars` for local `wrangler`/`next dev`. Never commit `.dev.vars`. | ||
| # Use the SAME JWT_SECRET as the existing Go backend so tokens stay interoperable | ||
| # during a phased migration. | ||
| JWT_SECRET="dev-secret-change-me" | ||
| JWT_EXPIRY_MINUTES="1440" | ||
| APP_BASE_URL="http://localhost:3000" | ||
|
|
||
| GOOGLE_OAUTH_CLIENT_ID="" | ||
| GEMINI_API_KEY="" | ||
|
|
||
| EMAIL_PROVIDER="console" # console = log the email instead of sending | ||
| EMAIL_FROM="DebateAI <no-reply@debateai.example>" | ||
| RESEND_API_KEY="" |
This file contains hidden or 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,8 @@ | ||
| node_modules/ | ||
| .next/ | ||
| .open-next/ | ||
| .wrangler/ | ||
| .dev.vars | ||
| *.tsbuildinfo | ||
| .env*.local | ||
| src/lib/cloudflare-env.d.ts.bak |
This file contains hidden or 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,124 @@ | ||
| # DebateAI — Cloudflare edition (`cf-app/`) | ||
|
|
||
| A Next.js (App Router) app that runs entirely on Cloudflare Workers via | ||
| [`@opennextjs/cloudflare`](https://opennext.js.org/cloudflare), replacing the Go | ||
| backend's infrastructure: | ||
|
|
||
| | Was (Go backend) | Now (this app) | | ||
| | ------------------------------------ | ----------------------------------------------- | | ||
| | MongoDB (`go.mongodb.org/mongo-driver`) | **D1** (SQLite) via Drizzle ORM | | ||
| | Redis (`redis/go-redis`) | **Workers KV** (TTL / ephemeral state) | | ||
| | gorilla WebSocket hub + turn timers | **Durable Object** `DebateRoom` + cron sweep | | ||
| | background goroutines | **Cron Triggers** (`scheduled()` handler) | | ||
| | `net/smtp` | HTTPS email (Resend / MailChannels) | | ||
| | `google.golang.org/genai` | Gemini REST via `fetch` | | ||
| | Gin route groups + `AuthMiddleware` | Next.js route handlers + `requireUser()` | | ||
| | Casbin + mongodb-adapter | `role_grants` / `user_roles` tables | | ||
|
|
||
| This scaffold **fully ports auth, profile, and leaderboard** as the reference | ||
| pattern. Every other domain has a schema, an adapter, and an entry in | ||
| `GET /api/_status`. See [`docs/CLOUDFLARE-MIGRATION.md`](./docs/CLOUDFLARE-MIGRATION.md) | ||
| for the porting playbook and the MongoDB→D1 data-migration steps. | ||
|
|
||
| --- | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Node 20+ | ||
| - A Cloudflare account + `npx wrangler login` | ||
|
|
||
| ## One-time setup | ||
|
|
||
| ```bash | ||
| cd cf-app | ||
| npm install | ||
|
|
||
| # 1. Create the D1 database and KV namespace, then paste the IDs into wrangler.toml | ||
| npx wrangler d1 create debateai | ||
| npx wrangler kv namespace create KV | ||
|
|
||
| # 2. Local secrets | ||
| cp .dev.vars.example .dev.vars | ||
| # -> set JWT_SECRET to the SAME value as the Go backend's jwt.secret | ||
| # so existing tokens keep working during a phased cutover | ||
|
|
||
| # 3. Apply the schema to the local D1 | ||
| npm run db:migrate:local | ||
| ``` | ||
|
|
||
| ## Run locally | ||
|
|
||
| ```bash | ||
| npm run dev # next dev, with real D1/KV/DO bindings via OpenNext | ||
| # app on http://localhost:3000 | ||
| ``` | ||
|
|
||
| `next dev` runs the route handlers but **not** `src/worker/index.ts` (the | ||
| WebSocket router + cron wrapper). To exercise those, build for Workers and run | ||
| the real runtime: | ||
|
|
||
| ```bash | ||
| npm run preview # opennextjs-cloudflare build && wrangler dev | ||
| ``` | ||
|
|
||
| Smoke test: | ||
|
|
||
| ```bash | ||
| curl -s localhost:3000/api/_status | jq | ||
| curl -s -XPOST localhost:3000/signup -H 'content-type: application/json' \ | ||
| -d '{"email":"a@b.com","password":"hunter2hunter2"}' | ||
| # EMAIL_PROVIDER=console -> the verification code is printed in the dev log | ||
| ``` | ||
|
|
||
| ## Deploy | ||
|
|
||
| ```bash | ||
| # secrets (once per environment) | ||
| npx wrangler secret put JWT_SECRET | ||
| npx wrangler secret put GEMINI_API_KEY | ||
| npx wrangler secret put RESEND_API_KEY # if EMAIL_PROVIDER=resend | ||
|
|
||
| npm run db:migrate:remote | ||
| npm run deploy # opennextjs-cloudflare build && wrangler deploy | ||
| ``` | ||
|
|
||
| ## Using it from the existing React frontend | ||
|
|
||
| The ported routes keep the **same paths and JSON shapes** as the Go API, so the | ||
| current `frontend/` works against this app by changing one env var: | ||
|
|
||
| ``` | ||
| VITE_BASE_URL="https://debateai.<your-account>.workers.dev" | ||
| ``` | ||
|
|
||
| Live-debate sockets move from `ws://<host>/ws/debate/:id` (same path) — the token | ||
| is passed as `?token=<jwt>` instead of an `Authorization` header, since browsers | ||
| can't set headers on `WebSocket`. `/ws/matchmaking` is replaced by polling | ||
| `POST /matchmaking/heartbeat` every ~30s. | ||
|
|
||
| ## Layout | ||
|
|
||
| ``` | ||
| cf-app/ | ||
| wrangler.toml bindings: DB (D1), KV, DEBATE_ROOM (DO), cron | ||
| open-next.config.ts OpenNext adapter config | ||
| drizzle.config.ts schema -> ./migrations | ||
| migrations/0000_init.sql runnable D1 schema + RBAC seed | ||
| src/ | ||
| db/schema.ts D1 tables (was Mongo collections) | ||
| db/client.ts getDb() -> drizzle(env.DB) | ||
| lib/ | ||
| auth.ts signToken / verifyToken / requireUser (was utils/auth.go + AuthMiddleware) | ||
| password.ts bcrypt (hashes migrate verbatim) | ||
| google.ts Google ID-token verify (was idtoken.Validate) | ||
| gemini.ts Gemini REST | ||
| email.ts Resend / MailChannels / console | ||
| kv.ts Redis replacement: matchmaking pool, rate limits, poll cache | ||
| users.ts userResponse / normalizeUserStats / nameFromEmail | ||
| http.ts json/ok/badRequest/... helpers | ||
| ids.ts ObjectID-compatible id generator | ||
| app/ route handlers (paths mirror the Go router) | ||
| durable-objects/DebateRoom.ts live debate: sockets + phase/turn state + alarm timer | ||
| worker/index.ts custom entry: WS routing + cron, wraps OpenNext | ||
| worker/matchmaking-sweep.ts cron pairing (was periodicMatchmaking goroutine) | ||
| ``` | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔒 Security & Privacy | 🟠 Major | 🏗️ Heavy lift
🧩 Analysis chain
🏁 Script executed:
Repository: AOSSIE-Org/DebateAI
Length of output: 27998
Sensitive Data Exposure (CWE-598)
Reachability: External
Keep raw JWTs out of WebSocket URLs.
The Worker reads
?token=and forwards the original URL to the Durable Object, so the JWT remains in the request URL. If URL logging or tracing retains query strings, an exposed token can be replayed. Use a short-lived, single-use WebSocket ticket or an authenticated cookie, and redact query strings from retained request data.Update
cf-app/README.md#L94-L96andcf-app/docs/CLOUDFLARE-MIGRATION.md#L193-L196.📍 Affects 2 files
cf-app/README.md#L94-L96(this comment)cf-app/docs/CLOUDFLARE-MIGRATION.md#L193-L196🤖 Prompt for AI Agents