How itsomsarraf/dishcovery-nextjs works
Project Type and Purpose
• Dishcovery is a full-stack web application built with Next.js (App Router) that leverages AI for ingredient recognition and recipe generation. It integrates features like user authentication, a dynamic dish gallery, caching via Redis, and a PostgreSQL database accessed through Drizzle ORM. The README shows its purpose clearly as it involves image uploads, AI analysis to generate recipes, and a dynamic user interface to showcase these recipes.
Analyzing the File Structure
• The top-level directories and files indicate standard configuration and build scripts (.env.example, package.json, bun.lockb, tailwind.config.ts, etc.).
• The src directory holds most of the application code. The src/app folder contains the Next.js specific routing including API endpoints (e.g., src/app/api/auth/[...nextauth]) and application pages for dish details, gallery, login, etc.
• The lib folder encapsulates database setup (PostgreSQL with Drizzle ORM), Redis caching configurations, Supabase client code, and various utility functions.
• The presence of custom actions (under src/actions) and UI components (under src/components and src/components/ui) reflects a clear separation between business logic, UI concerns, and data handling.
Key Architectural Components
a. Frontend:
• Next.js App Router for routing and rendering pages (pages such as /dish, /dishgallery, /login, etc.).
• UI Components using Tailwind CSS and Shadcn components for styling.
• Custom React hooks and state management (including Zustand where applicable).
b. Backend/API Layer:
• Next.js API routes (e.g., /api/auth handled by NextAuth.js for authentication).
• Server-side route handlers that process image uploads, interact with the AI module and handle recipe generation.
• Middleware for global request handling (src/middleware.ts).
c. Database and ORM:
• PostgreSQL hosting the persistent data – recipes, user data, etc.
• Drizzle ORM used to interact with the database, with schema defined in src/lib/db/schema.ts.
d. Caching Layer:
• Redis implemented via Upstash for efficient caching of recipes and dish gallery data.
• Functions from src/lib/redis.ts that encapsulate caching logic.
e. AI Vision & Recipe Generation:
• Integration with the Llama Vision AI (meta-llama/Llama-Vision-Free) for image analysis to determine ingredients and generate recipe prompts.
• This process is triggered once an image upload is detected and processed by server-side logic.
f. Authentication:
• Managed using NextAuth.js with API endpoints in src/app/api/auth/[...nextauth].
• Frontend components to sign in and sign out, as seen in src/components/auth.
Relationships and Interactions
1. User uploads an image and provides dietary preferences from the Frontend (Next.js pages).
2. The request is sent to the backend API routes, where an authentication check is done (NextAuth.js integration).
3. The AI service (Llama Vision) is invoked to analyze the image.
4. The generated recipe is stored in PostgreSQL (via Drizzle ORM) and cached in Redis (via Upstash).
5. The dish gallery page retrieves recipes using the caching layer for fast performance.
Architectural Patterns and Design Principles
• Client-Server Separation: Clear separation between frontend (user interface, routing, components) and backend (API endpoints, business logic).
• Modularization: Organized folder structure keeps authentication, UI components, data-access layers, and utility functions in separate modules.
• Scalability: Use of caching (Redis) and a robust database layer ensures the system is prepared for high-traffic usage.
• Extensibility: The architecture supports future enhancements (e.g., user profiles, social sharing features) as outlined in the roadmap.