From 7b0baf3be8dd3956dc0f33bd952288775c5e957f Mon Sep 17 00:00:00 2001 From: neodk2004 Date: Sun, 21 Jun 2026 18:51:41 +0200 Subject: [PATCH] fix(docker): lazy-init Supabase+Redis, self-host Redis, simplify build args --- .env.example | 5 +++ docker/Dockerfile | 15 +------ docker/docker-compose.yml | 25 ++++++++---- package-lock.json | 81 +++++++++++++++++++++++++++++-------- package.json | 2 +- src/lib/feed-helpers.ts | 54 ++++++++++--------------- src/lib/redis.ts | 21 ++++------ src/lib/supabase-storage.ts | 25 +++++++----- 8 files changed, 135 insertions(+), 93 deletions(-) diff --git a/.env.example b/.env.example index ee8ebec..b238ebf 100644 --- a/.env.example +++ b/.env.example @@ -30,6 +30,11 @@ ADMIN_SECRET="your-random-uuid-here" # ADMIN_OWNER_ID: Your Clerk user ID (Clerk dashboard). Bootstraps the super-admin. ADMIN_OWNER_ID="user_xxxxxxxxxxxxxxxxxxxx" +# ── Redis ───────────────────────────────────────────────────── +# Self-hosted: use the Docker Redis service (docker-compose sets this automatically). +# Local dev: run `docker run -d -p 6379:6379 redis:7-alpine` or install Redis locally. +REDIS_URL="redis://localhost:6379" + # ── Invite Gate ─────────────────────────────────────────────── # Set to "false" to disable the invite requirement after the Alpha/Beta phase. # Default: "true" (invite required — enforced in proxy.ts) diff --git a/docker/Dockerfile b/docker/Dockerfile index b5ed96c..b7ef3cb 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -17,24 +17,13 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . -# All vars needed during "npm run build": -# - NEXT_PUBLIC_* are baked into the client bundle -# - Server vars are needed because Next.js runs server code during page-data collection +# Only NEXT_PUBLIC_* vars are baked into the client bundle at build time. +# All server-side secrets are injected at runtime via docker-compose env_file. ARG NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY ARG NEXT_PUBLIC_SUPABASE_URL -ARG CLERK_SECRET_KEY -ARG SUPABASE_SERVICE_ROLE_KEY -ARG UPSTASH_REDIS_REST_URL -ARG UPSTASH_REDIS_REST_TOKEN -ARG DATABASE_URL ENV NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=$NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY ENV NEXT_PUBLIC_SUPABASE_URL=$NEXT_PUBLIC_SUPABASE_URL -ENV CLERK_SECRET_KEY=$CLERK_SECRET_KEY -ENV SUPABASE_SERVICE_ROLE_KEY=$SUPABASE_SERVICE_ROLE_KEY -ENV UPSTASH_REDIS_REST_URL=$UPSTASH_REDIS_REST_URL -ENV UPSTASH_REDIS_REST_TOKEN=$UPSTASH_REDIS_REST_TOKEN -ENV DATABASE_URL=$DATABASE_URL ENV NEXT_TELEMETRY_DISABLED=1 RUN npx prisma generate diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 87229d3..d1a7f54 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -1,24 +1,33 @@ services: + redis: + image: redis:7-alpine + container_name: pawfeed-redis + restart: unless-stopped + volumes: + - redis_data:/data + command: redis-server --appendonly yes + pawfeed: build: context: .. dockerfile: docker/Dockerfile args: - # All vars needed at build time (Next.js runs server code during build). - # Docker Compose automatically reads .env from the same directory as this file. + # Only public vars needed at build time (baked into client bundle). + # Docker Compose reads these from docker/.env automatically. NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY: ${NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY} NEXT_PUBLIC_SUPABASE_URL: ${NEXT_PUBLIC_SUPABASE_URL} - CLERK_SECRET_KEY: ${CLERK_SECRET_KEY} - SUPABASE_SERVICE_ROLE_KEY: ${SUPABASE_SERVICE_ROLE_KEY} - UPSTASH_REDIS_REST_URL: ${UPSTASH_REDIS_REST_URL} - UPSTASH_REDIS_REST_TOKEN: ${UPSTASH_REDIS_REST_TOKEN} - DATABASE_URL: ${DATABASE_URL} image: pawfeed:latest container_name: pawfeed restart: unless-stopped ports: - "4563:3000" env_file: - - .env # docker/.env — same directory as this file + - .env environment: NODE_ENV: production + REDIS_URL: redis://redis:6379 + depends_on: + - redis + +volumes: + redis_data: diff --git a/package-lock.json b/package-lock.json index 96e02cf..ebdb329 100644 --- a/package-lock.json +++ b/package-lock.json @@ -23,11 +23,11 @@ "@trpc/client": "^11.17.0", "@trpc/server": "^11.17.0", "@trpc/tanstack-react-query": "^11.17.0", - "@upstash/redis": "^1.38.0", "class-variance-authority": "^0.7.1", "client-only": "^0.0.1", "clsx": "^2.1.1", "embla-carousel-react": "^8.6.0", + "ioredis": "^5.11.1", "lucide-react": "^1.17.0", "next": "16.2.7", "next-themes": "^0.4.6", @@ -2841,6 +2841,12 @@ } } }, + "node_modules/@ioredis/commands": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@ioredis/commands/-/commands-1.10.0.tgz", + "integrity": "sha512-UmeW7z4LfctwoQ5wkhVzgq8tXkreED2xZGpX+Bg+zA+WJFZCT6c062AfCK/Dfk81xZnnwdhJCUMkitihRaoC2Q==", + "license": "MIT" + }, "node_modules/@jridgewell/gen-mapping": { "version": "0.3.13", "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", @@ -5375,15 +5381,6 @@ "win32" ] }, - "node_modules/@upstash/redis": { - "version": "1.38.0", - "resolved": "https://registry.npmjs.org/@upstash/redis/-/redis-1.38.0.tgz", - "integrity": "sha512-wu+dZBptlLy0+MCUEoHmzrY/TnmgDey3+c7EbIGwrLqAvkP8yi5MWZHYGIFtAygmL4Bkz2TdFu+eU0vFPncIcg==", - "license": "MIT", - "dependencies": { - "uncrypto": "^0.1.3" - } - }, "node_modules/@vitejs/plugin-react": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-6.0.2.tgz", @@ -6393,6 +6390,15 @@ "node": ">=6" } }, + "node_modules/cluster-key-slot": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cluster-key-slot/-/cluster-key-slot-1.1.1.tgz", + "integrity": "sha512-rwHwUfXL40Chm1r08yrhU3qpUvdVlgkKNeyeGPOxnW8/SyVDvgRaed/Uz54AqWNaTCAThlj6QAs3TZcKI0xDEw==", + "license": "Apache-2.0", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/code-block-writer": { "version": "13.0.3", "resolved": "https://registry.npmjs.org/code-block-writer/-/code-block-writer-13.0.3.tgz", @@ -8869,6 +8875,28 @@ "node": ">= 0.4" } }, + "node_modules/ioredis": { + "version": "5.11.1", + "resolved": "https://registry.npmjs.org/ioredis/-/ioredis-5.11.1.tgz", + "integrity": "sha512-ehuGcf94bQXhfagULNXrJdfnWO38v070jxSx/qE87Kjzmu2fU7ro5EFAb+OPituLqgfyuQaym5DlrNydW2sJ9A==", + "license": "MIT", + "dependencies": { + "@ioredis/commands": "1.10.0", + "cluster-key-slot": "1.1.1", + "debug": "4.4.3", + "denque": "2.1.0", + "redis-errors": "1.2.0", + "redis-parser": "3.0.0", + "standard-as-callback": "2.1.0" + }, + "engines": { + "node": ">=12.22.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/ioredis" + } + }, "node_modules/ip-address": { "version": "10.2.0", "resolved": "https://registry.npmjs.org/ip-address/-/ip-address-10.2.0.tgz", @@ -11741,6 +11769,27 @@ "node": ">= 4" } }, + "node_modules/redis-errors": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/redis-errors/-/redis-errors-1.2.0.tgz", + "integrity": "sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w==", + "license": "MIT", + "engines": { + "node": ">=4" + } + }, + "node_modules/redis-parser": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/redis-parser/-/redis-parser-3.0.0.tgz", + "integrity": "sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A==", + "license": "MIT", + "dependencies": { + "redis-errors": "^1.0.0" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/reflect.getprototypeof": { "version": "1.0.10", "resolved": "https://registry.npmjs.org/reflect.getprototypeof/-/reflect.getprototypeof-1.0.10.tgz", @@ -12535,6 +12584,12 @@ "dev": true, "license": "MIT" }, + "node_modules/standard-as-callback": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/standard-as-callback/-/standard-as-callback-2.1.0.tgz", + "integrity": "sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A==", + "license": "MIT" + }, "node_modules/standardwebhooks": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/standardwebhooks/-/standardwebhooks-1.0.0.tgz", @@ -13350,12 +13405,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/uncrypto": { - "version": "0.1.3", - "resolved": "https://registry.npmjs.org/uncrypto/-/uncrypto-0.1.3.tgz", - "integrity": "sha512-Ql87qFHB3s/De2ClA9e0gsnS6zXG27SkTiSJwjCc9MebbfapQfuPzumMIUMi38ezPZVNFcHI9sUIepeQfw8J8Q==", - "license": "MIT" - }, "node_modules/undici": { "version": "7.27.1", "resolved": "https://registry.npmjs.org/undici/-/undici-7.27.1.tgz", diff --git a/package.json b/package.json index bd909f1..625a765 100644 --- a/package.json +++ b/package.json @@ -27,11 +27,11 @@ "@trpc/client": "^11.17.0", "@trpc/server": "^11.17.0", "@trpc/tanstack-react-query": "^11.17.0", - "@upstash/redis": "^1.38.0", "class-variance-authority": "^0.7.1", "client-only": "^0.0.1", "clsx": "^2.1.1", "embla-carousel-react": "^8.6.0", + "ioredis": "^5.11.1", "lucide-react": "^1.17.0", "next": "16.2.7", "next-themes": "^0.4.6", diff --git a/src/lib/feed-helpers.ts b/src/lib/feed-helpers.ts index 3f05cee..54263f6 100644 --- a/src/lib/feed-helpers.ts +++ b/src/lib/feed-helpers.ts @@ -1,5 +1,5 @@ import "server-only"; -import type { Redis } from "@upstash/redis"; +import type { Redis } from "ioredis"; import type { PrismaClient } from "@prisma/client"; /** @@ -8,15 +8,12 @@ import type { PrismaClient } from "@prisma/client"; * Key convention: feed:{petId} * - sorted set, score = Unix timestamp (ms), member = postId * - * Security (T-02-02): server-only — UPSTASH tokens never reach client bundle. + * Security (T-02-02): server-only — Redis credentials never reach client bundle. */ export const PAGE_SIZE = 20; export const FEED_RETENTION_MS = 90 * 24 * 60 * 60 * 1000; // 90 days -/** - * Returns the Redis sorted-set key for a pet's personal feed inbox. - */ export function feedKey(petId: string): string { return `feed:${petId}`; } @@ -24,7 +21,6 @@ export function feedKey(petId: string): string { /** * Fan-out a new post to all followers' feed inboxes and the poster's own feed. * Score = Unix timestamp (ms); member = postId. - * Called synchronously in posts.create (acceptable at v1 scale — Open Question 3). */ export async function fanOutPost( redis: Redis, @@ -42,20 +38,16 @@ export async function fanOutPost( const pipeline = redis.pipeline(); - // Fan-out to every follower's inbox for (const { followerPetId } of followers) { - pipeline.zadd(feedKey(followerPetId), { score, member: postId }); + pipeline.zadd(feedKey(followerPetId), score, postId); } - - // Also add to own feed so own posts appear in the poster's timeline (Open Question 3) - pipeline.zadd(feedKey(petId), { score, member: postId }); + pipeline.zadd(feedKey(petId), score, postId); await pipeline.exec(); } /** * Backfill the follower's feed when they follow a new pet. - * Adds the followee's recent posts (up to `limit`) to the follower's feed. */ export async function backfillOnFollow( redis: Redis, @@ -75,20 +67,14 @@ export async function backfillOnFollow( const pipeline = redis.pipeline(); for (const post of recentPosts) { - pipeline.zadd(feedKey(followerPetId), { - score: post.createdAt.getTime(), - member: post.id, - }); + pipeline.zadd(feedKey(followerPetId), post.createdAt.getTime(), post.id); } await pipeline.exec(); } /** * Retrieve a page of postIds from a pet's feed inbox. - * Uses ZRANGE with byScore + rev for O(log N + page) retrieval. - * - * Pitfall 2: rev:true requires maxScore as first positional arg. - * Pitfall 1: @upstash/redis 1.38.0 returns Array<{member, score}>, not interleaved. + * Uses ZREVRANGEBYSCORE for reverse-chronological retrieval. */ export async function getFeedPage( redis: Redis, @@ -98,20 +84,25 @@ export async function getFeedPage( const maxScore = cursor ?? Date.now(); const minScore = 0; - // Fetch PAGE_SIZE + 1 to detect hasNextPage - // @upstash/redis requires {offset, count} together when limiting (not count alone) - const results = (await redis.zrange(feedKey(petId), maxScore, minScore, { - byScore: true, - rev: true, - withScores: true, - offset: 0, - count: PAGE_SIZE + 1, - })) as Array<{ member: string; score: number }>; + // WITHSCORES returns interleaved [member, score, member, score, ...] + const raw = (await redis.zrevrangebyscore( + feedKey(petId), + maxScore, + minScore, + "WITHSCORES", + "LIMIT", + 0, + PAGE_SIZE + 1 + )) as string[]; + + const results: Array<{ member: string; score: number }> = []; + for (let i = 0; i < raw.length; i += 2) { + results.push({ member: raw[i], score: parseFloat(raw[i + 1]) }); + } const hasNextPage = results.length > PAGE_SIZE; const items = hasNextPage ? results.slice(0, PAGE_SIZE) : results; - const nextCursor = - hasNextPage ? items[items.length - 1].score - 1 : null; + const nextCursor = hasNextPage ? items[items.length - 1].score - 1 : null; return { postIds: items.map((r) => r.member), @@ -121,7 +112,6 @@ export async function getFeedPage( /** * Trim a pet's feed to the 90-day retention window. - * Called by the pg_cron cleanup job — not on the request path. */ export async function trimFeed(redis: Redis, petId: string): Promise { const cutoff = Date.now() - FEED_RETENTION_MS; diff --git a/src/lib/redis.ts b/src/lib/redis.ts index 8ab7250..5806704 100644 --- a/src/lib/redis.ts +++ b/src/lib/redis.ts @@ -1,16 +1,9 @@ import "server-only"; -import { Redis } from "@upstash/redis"; +import { Redis } from "ioredis"; -/** - * @upstash/redis singleton for Phase 2 feed fan-out sorted sets. - * - * Security (T-02-02): server-only import ensures UPSTASH_REDIS_REST_TOKEN - * never reaches the client bundle. - * - * Environment: UPSTASH_REDIS_REST_URL and UPSTASH_REDIS_REST_TOKEN must be - * set in .env.local (Upstash Console → Redis database → REST API). - */ -export const redis = new Redis({ - url: process.env.UPSTASH_REDIS_REST_URL!, - token: process.env.UPSTASH_REDIS_REST_TOKEN!, -}); +// lazyConnect: true — connection only established on first command, +// so module evaluation during Next.js build does not attempt a TCP connect. +export const redis = new Redis( + process.env.REDIS_URL ?? "redis://localhost:6379", + { maxRetriesPerRequest: 3, lazyConnect: true } +); diff --git a/src/lib/supabase-storage.ts b/src/lib/supabase-storage.ts index 8d5c11e..ef1d2a5 100644 --- a/src/lib/supabase-storage.ts +++ b/src/lib/supabase-storage.ts @@ -1,13 +1,20 @@ import "server-only"; -import { createClient } from "@supabase/supabase-js"; +import { createClient, type SupabaseClient } from "@supabase/supabase-js"; -const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL!; -const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY!; +// Lazy singleton — not initialized at module eval so Next.js build does not +// throw when env vars are absent during the page-data collection phase. +let _client: SupabaseClient | null = null; -// Server-side Supabase client with service role key — never exposed to client -const supabase = createClient(supabaseUrl, serviceRoleKey, { - auth: { persistSession: false }, -}); +function getClient(): SupabaseClient { + if (!_client) { + _client = createClient( + process.env.NEXT_PUBLIC_SUPABASE_URL!, + process.env.SUPABASE_SERVICE_ROLE_KEY!, + { auth: { persistSession: false } } + ); + } + return _client; +} export const STORAGE_BUCKET = "pet-avatars"; export const MAX_FILE_SIZE = 10 * 1024 * 1024; // 10 MB @@ -18,7 +25,7 @@ export const ALLOWED_CONTENT_TYPES = ["image/jpeg", "image/png", "image/webp"] a * The client PUTs the file directly to this URL — bytes never pass through the API server. */ export async function createSignedUploadUrl(key: string) { - const { data, error } = await supabase.storage + const { data, error } = await getClient().storage .from(STORAGE_BUCKET) .createSignedUploadUrl(key); @@ -30,6 +37,6 @@ export async function createSignedUploadUrl(key: string) { * Returns the public CDN URL for a stored object. */ export function getPublicUrl(key: string): string { - const { data } = supabase.storage.from(STORAGE_BUCKET).getPublicUrl(key); + const { data } = getClient().storage.from(STORAGE_BUCKET).getPublicUrl(key); return data.publicUrl; }