diff --git a/.env.example b/.env.example index 49b4993..566b82f 100644 --- a/.env.example +++ b/.env.example @@ -3,16 +3,16 @@ APP_URL=http://localhost:3000 # openssl rand -base64 48 SESSION_SECRET= -# ---- database and photos (npm run setup fills these in) ---- +# ---- database and photos (bun run setup fills these in) ---- # TigerRetail development PostgreSQL database (ask the project lead) DATABASE_URL=postgresql://:@:5432/?sslmode=require -# Cloudflare R2 bucket for listing photos and avatars -R2_ACCOUNT_ID= -R2_BUCKET=tigerretail -R2_ACCESS_KEY_ID= -R2_SECRET_ACCESS_KEY= -# the bucket's Public Development URL (R2 → bucket → Settings) -R2_PUBLIC_URL=https://pub-.r2.dev +# Private S3 bucket for listing photos and avatars; CloudFront serves reads. +AWS_REGION=us-east-1 +S3_BUCKET=tigerretail-photos-104733724423-us-east-1 +S3_PUBLIC_URL=https://d1ugkt2f7zwiiq.cloudfront.net +# Local development only: AWS CLI profile with access to the photo bucket. +# Run aws sso login --profile . Omit on EC2; its IAM role supplies credentials. +# AWS_PROFILE=TigerApps # ---- listserv import ---- # netids allowed to run imports and approve them, comma-separated @@ -22,7 +22,7 @@ ANTHROPIC_API_KEY= # InboxEngine finds the sale emails (ask TigerApps for a token) INBOX_ENGINE_URL=https://inbox-engine.tigerapps.org INBOX_ENGINE_TOKEN= -# protects /api/cron/import (the daily scheduled job sends it; npm run import uses it). openssl rand -hex 24 +# protects /api/cron/import (the daily scheduled job sends it; bun run import uses it). openssl rand -hex 24 CRON_SECRET= # Invites to people whose listserv sales were imported (admin clicks "send invite"). diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..e77683c --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,82 @@ +name: CI and deploy +on: + pull_request: + push: + branches: [main] + workflow_dispatch: + +permissions: + contents: read + +concurrency: + group: tigerretail-${{ github.ref }} + cancel-in-progress: false + +jobs: + build: + runs-on: ubuntu-24.04-arm + timeout-minutes: 20 + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + node-version: '22' + - uses: oven-sh/setup-bun@v2 + with: + bun-version: '1.3.5' + - run: bun install --frozen-lockfile + - run: bun run lint + - run: bun run test + - run: bun run build + env: + NEXT_TELEMETRY_DISABLED: '1' + - run: bash deploy/package.sh + - uses: actions/upload-artifact@v4 + with: + name: release + path: | + release.tar.gz + release.tar.gz.sha256 + retention-days: 7 + + deploy: + needs: build + if: github.ref == 'refs/heads/main' && github.event_name != 'pull_request' + runs-on: ubuntu-latest + timeout-minutes: 15 + permissions: + contents: read + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + name: release + - uses: aws-actions/configure-aws-credentials@v4 + with: + role-to-assume: ${{ vars.AWS_DEPLOY_ROLE_ARN }} + aws-region: us-east-1 + - name: Upload and activate release + env: + INSTANCE_ID: ${{ vars.EC2_INSTANCE_ID }} + RELEASE_ID: ${{ github.sha }}-${{ github.run_id }}-${{ github.run_attempt }} + run: | + set -euo pipefail + sha256sum -c release.tar.gz.sha256 + checksum="$(cut -d ' ' -f1 release.tar.gz.sha256)" + aws s3 cp release.tar.gz "s3://tigerretail-deploy-104733724423-us-east-1/releases/$RELEASE_ID.tar.gz" --only-show-errors + command_id="$(aws ssm send-command \ + --instance-ids "$INSTANCE_ID" \ + --document-name TigerRetailDeploy \ + --parameters "Release=$RELEASE_ID,Sha256=$checksum" \ + --timeout-seconds 600 --query Command.CommandId --output text)" + for attempt in $(seq 1 120); do + status="$(aws ssm get-command-invocation --command-id "$command_id" --instance-id "$INSTANCE_ID" --query Status --output text 2>/dev/null || true)" + case "$status" in + Success) break ;; + Failed|Cancelled|TimedOut|Cancelling) break ;; + esac + sleep 5 + done + aws ssm get-command-invocation --command-id "$command_id" --instance-id "$INSTANCE_ID" --query '{Status:Status,Output:StandardOutputContent,Error:StandardErrorContent}' + [[ "$status" == Success ]] + curl --retry 3 --retry-delay 5 --fail --silent --show-error https://retail.tigerapps.org/api/health diff --git a/.gitignore b/.gitignore index 0fe7a84..f773ce3 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ yarn-error.log* # typescript *.tsbuildinfo next-env.d.ts + +# deployment artifacts +/release.tar.gz +/release.tar.gz.sha256 diff --git a/README.md b/README.md index 08636da..a18e952 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,14 @@ # TigerRetail -Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRetail development PostgreSQL database, and Cloudflare R2 for photos. +Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRetail development PostgreSQL database, and AWS S3 + CloudFront for photos. ## Run it on your machine -1. Install Node 22+ and Google Chrome (the e2e tests drive Chrome). -2. `git clone https://github.com/TigerAppsOrg/TigerRetail.git && cd TigerRetail && npm install` -3. `npm run setup` asks for the database and R2 keys one at a time and saves them to `.env.local`. +1. Install Node 22, Bun, AWS CLI, and Google Chrome (the e2e tests drive Chrome). +2. `git clone https://github.com/TigerAppsOrg/TigerRetail.git && cd TigerRetail && bun install --frozen-lockfile` +3. `bun run setup` asks for the database and S3 settings one at a time and saves them to `.env.local`. 4. Copy the remaining keys you need from `.env.example` into `.env.local` (see the table below). -5. `npm run dev`, open http://localhost:3000 and sign in with your netid. +5. `bun run dev`, open http://localhost:3000 and sign in with your netid. `.env.local` is gitignored. Never commit keys, and never paste them in Slack or GitHub. @@ -17,35 +17,57 @@ Princeton's student marketplace. Next.js app, Princeton CAS sign-in, the TigerRe | Key | Needed for | Where to get it | |---|---|---| | `DATABASE_URL` | everything | Ask Raj (shared dev database) | -| `R2_ACCOUNT_ID`, `R2_BUCKET`, `R2_ACCESS_KEY_ID`, `R2_SECRET_ACCESS_KEY`, `R2_PUBLIC_URL` | photos | Ask Raj for an R2 API token (Object Read & Write on bucket `tigerretail`) | +| `AWS_REGION`, `S3_BUCKET`, `S3_PUBLIC_URL` | photos | See [AWS hosting](deploy/README.md); ask TigerApps for AWS access, then `aws sso login --profile ` and set `AWS_PROFILE` locally | | `SESSION_SECRET` | sign-in | Make your own: `openssl rand -base64 48` | | `APP_URL` | sign-in | `http://localhost:3000` | | `ADMIN_NETIDS` | the import review page (`/admin/imports`) | Your netid | | `ANTHROPIC_API_KEY` | reading sale slide decks | Your own key from console.anthropic.com | | `INBOX_ENGINE_URL`, `INBOX_ENGINE_TOKEN` | finding listserv sale emails | Ask TigerApps | -| `CRON_SECRET` | `npm run import` | Make your own: `openssl rand -hex 24` | +| `CRON_SECRET` | `bun run import` | Make your own: `openssl rand -hex 24` | | `SENDGRID_API_KEY`, `MAIL_FROM` | sending seller invites | Ask Raj | | `INVITE_TEST_RECIPIENT` | sending seller invites | Your own `netid@princeton.edu`; every test invite goes here | -Only the first four rows are needed to run the app. Leave `INVITES_TO_SELLERS` unset: it sends invites to real sellers. +Only the first four rows are needed to run the app. S3 uses the AWS SDK default credential chain: your local AWS profile or the EC2 instance role. No R2 keys are needed. Leave `INVITES_TO_SELLERS` unset: it sends invites to real sellers. ## Commands -- `npm test`: unit tests. -- `npm run lint`: lint. -- `npm run e2e`, `npm run e2e:import`, `npm run e2e:map`: browser tests. Run them with `npm run dev` going in another terminal. -- `npm run migrate -- --status`: lists the database migrations that have been applied. -- `npm run migrate`: applies new files from `db/migrations/`. -- `npm run seed -- --owner `: adds the demo marketplace. -- `npm run seed:clear`: removes the demo marketplace. +- `bun run test`: unit tests. +- `bun run lint`: lint. +- `bun run e2e`, `bun run e2e:import`, `bun run e2e:map`: browser tests. Run them with `bun run dev` going in another terminal. +- `bun run migrate -- --status`: lists the database migrations that have been applied. +- `bun run migrate`: applies new files from `db/migrations/`. +- `bun run seed -- --owner `: adds the demo marketplace. +- `bun run seed:clear`: removes the demo marketplace. ## The shared database Everyone works on the same dev database and photo bucket, so: -- Tell the team before you run `npm run migrate`, `npm run seed` or `npm run seed:clear`. They change everyone's data. +- Tell the team before you run `bun run migrate`, `bun run seed` or `bun run seed:clear`. They change everyone's data. - The e2e tests create `e2e-*` users and delete them when they finish. -## Hosting +## Hosted MVP (AWS) -- Princeton CAS only accepts `*.tigerapps.org` addresses, so the hosted site lives on one. +The deployment targets https://retail.tigerapps.org. Pushes to `main` run CI and +then deploy to the dedicated EC2 instance through GitHub OIDC and AWS Systems Manager. +See [AWS hosting](deploy/README.md) for SSH, secrets, release management, and +shared database migration details. + +Princeton CAS requires the custom `*.tigerapps.org` hostname for sign-in. + +## Where to work + +`main` is the shared source of truth and the branch deployed to AWS. Start new +work from an up-to-date `main` and merge changes through a pull request. + +- `app/(site)/`: the live marketplace pages, actions, and shared UI styles. +- `components/campus-map/`: the live map and its filtering/rendering logic. +- `lib/`: authentication, database access, storage, messaging, and imports. +- `db/migrations/`: database history; keep applied migrations intact. +- `deploy/` and `.github/workflows/`: AWS release configuration. +- `fixtures/seed/`: development seed data and illustrations, used by `bun run seed`. +- `app/dev/map-demo/` and `fixtures/demoMarketplace.ts`: the development-only + map stress test, exercised by `bun run e2e:map`. It returns 404 in production. + +The standalone HTML prototype has been retired. Update the Next.js app directly; +its shared marketplace styles live in `app/(site)/marketplace.css`. diff --git a/app/(site)/_components/AutoRefresh.tsx b/app/(site)/_components/AutoRefresh.tsx new file mode 100644 index 0000000..8403dbd --- /dev/null +++ b/app/(site)/_components/AutoRefresh.tsx @@ -0,0 +1,15 @@ +"use client"; +import { useRouter } from "next/navigation"; +import { useEffect } from "react"; + +/* Re-renders the page from the server every few seconds while it's on screen. + For work that finishes in the background (reading a deck, searching the + listserv): the page renders this only while something is still going. */ +export function AutoRefresh({ seconds = 4 }: { seconds?: number }) { + const router = useRouter(); + useEffect(() => { + const t = setInterval(() => router.refresh(), seconds * 1000); + return () => clearInterval(t); + }, [router, seconds]); + return null; +} diff --git a/app/(site)/_components/Icon.tsx b/app/(site)/_components/Icon.tsx index e7980f1..43c7329 100644 --- a/app/(site)/_components/Icon.tsx +++ b/app/(site)/_components/Icon.tsx @@ -1,4 +1,4 @@ -/* Icons copied from the wireframe (wireframe/index.html, const I). Static, +/* Marketplace icons. Static, trusted SVG strings, so they render through innerHTML unchanged. */ const SVG = { search: '', diff --git a/app/(site)/actions.ts b/app/(site)/actions.ts index 12695e5..833b0d3 100644 --- a/app/(site)/actions.ts +++ b/app/(site)/actions.ts @@ -3,12 +3,13 @@ a Server Action is a public POST endpoint, so the page that rendered the button proves nothing about who is calling it. */ import { redirect } from "next/navigation"; +import { after } from "next/server"; import { revalidatePath } from "next/cache"; import { safeNext } from "@/lib/cas"; import { getViewer, requireUser } from "@/lib/auth"; import * as data from "@/lib/data"; import { claim, isAdmin, publishOwn, takeDown } from "@/lib/importer/admin"; -import { importOwnDeck, isResidentialBuilding } from "@/lib/importer/importer"; +import { isResidentialBuilding, startOwnDeck } from "@/lib/importer/importer"; import { deckIdsIn, linksIn } from "@/lib/importer/rules"; import { isBlankItem, parseContact, parseDollars, parseItem, parseProfile, type ContactChoice, type ItemDraft, type ItemInput } from "@/lib/rules"; import type { CardListing } from "./_components/ListingCard"; @@ -213,8 +214,11 @@ export async function importOwnDeckAction(_: DeckState, f: FormData): Promise { await requireAdmin(); const [deckId] = deckIdsIn([String(f.get("url") ?? "").trim()]); if (!deckId) return { error: "That isn't a Google Slides link." }; - const r = await importDeck(deckId, { + const email = { messageId: null, subject: String(f.get("subject") ?? "").trim(), text: "", @@ -29,22 +34,21 @@ export async function importLinkAction(_: ImportState, f: FormData): Promise readDeck(started.sourceId, deckId, email)); revalidatePath("/admin/imports"); - return r.status === "failed" - ? { error: `Couldn't import that deck: ${r.detail}` } - : { message: `${r.status === "skipped" ? "Already imported" : "Imported"}: ${r.detail}` }; + return { message: "Reading the deck. Its items show up below when it's done; a big deck takes a few minutes." }; } export async function runNowAction(): Promise { await requireAdmin(); try { - const r = await runListservImport(); + const run = await startListservRun(); + after(() => finishListservRun(run)); revalidatePath("/admin/imports"); - const created = [...r.decks, ...r.emailSales].reduce((n, d) => n + d.created, 0); - return { - message: `Checked ${r.emails} sale emails since ${new Date(r.since).toLocaleDateString()}: ${r.decks.length} decks, ${r.emailSales.length} sales written in the email, ${created} items for review.`, - }; + return { message: "Searching the listserv. The result shows above the queue when it's done." }; } catch (e) { return { error: e instanceof Error ? e.message : String(e) }; } diff --git a/app/(site)/admin/imports/ImportForms.tsx b/app/(site)/admin/imports/ImportForms.tsx index 0d66557..909f757 100644 --- a/app/(site)/admin/imports/ImportForms.tsx +++ b/app/(site)/admin/imports/ImportForms.tsx @@ -27,7 +27,7 @@ export function ImportForms({ engineConfigured }: { engineConfigured: boolean })

search the listserv

sale emails across the residential listservs: slides links, and sales written in the email with photos. the first run covers the last 30 days; after that each run picks up where the - last one ended. once deployed this also runs on its own three times a day. + last one ended. it doesn’t run on its own yet. a search takes a few minutes; the result shows above the queue.