How pacopedrosa/smartboooks works
1. Overall shape
- SmartBooks is a three-tier system deployed with Docker Compose: a React/Vite frontend, a Next.js backend exposing REST API routes, and a Python FastAPI ML service.
- The backend is the central orchestration layer: it serves user-facing API endpoints, talks to PostgreSQL, validates JWTs, and calls the ML service for recommendations.
2. Frontend boundary
- The frontend/ app is a single-page UI built with React 18 and Vite, served behind Nginx in production.
- Its main responsibilities are presentation and client-side state: login/register screens, book browsing, recommendations, and user profile.
- AuthContext and ProtectedRoute indicate that session state is handled in the browser, while services/api.js is the single client-side gateway to the backend API.
3. Backend boundary
- The backend/ directory is a Next.js 14 API-only backend; there is no separate server framework.
- API routes are organized by domain:
- auth/ for register/login/logout
- books/ for catalog, detail, likes, favorites, ratings, and similar-books
- users/profile for profile data and updates
- recommendations for personalized recommendations
- health for service checks
- middleware/auth.js shows that protected endpoints rely on token-based request validation.
4. Authentication and session flow
- Authentication is JWT-based.
- The frontend logs in/registers through backend auth routes, receives session state through the app’s auth context, and then attaches authenticated requests to protected endpoints.
- Backend helpers in lib/jwt.js and the auth middleware form the trust boundary for all user-specific actions such as likes, favorites, ratings, profile updates, and recommendations.
5. Catalog and interaction model
- The book domain is centered on a large imported dataset (BooksDatasetClean.csv in the repo).
- Users can browse books, filter by genre, view details, and record interactions: like, favorite, and rating.
- These interactions are not just UI events; they are the input signal for both the user profile statistics and the recommendation engine.
6. Recommendation pipeline
- Personalized recommendations are served through the backend’s /api/recommendations endpoint, which delegates recommendation logic to the ML service.
- The system uses an adaptive strategy based on interaction volume:
- no history: popular books
- small history: content-based recommendations
- enough history: hybrid content + collaborative filtering
- This means the recommendation subsystem depends on both the user’s interaction history in PostgreSQL and the ML service’s precomputed book representations.
7. ML service boundary
- ml-service/ is a separate FastAPI application in Python 3.11.
- Its role is to build and serve the recommendation model, not to handle user auth or the general API.
- The service imports the book dataset, computes embeddings with sentence-transformers (all-MiniLM-L6-v2), and caches them in the database.
- The recommendation logic lives in recommender.py, with bootstrap/import work in seed.py and app startup in main.py.
8. Data storage and persistence
- PostgreSQL is the main system of record for users, books, embeddings, and interaction history.
- database/init.sql defines the schema, while backend/lib/db.js and ml-service/app/database.py indicate separate connection layers for Node and Python.
- The repository also includes MinIO support via backend/lib/minio.js, which suggests object storage integration for binary assets or future media handling, even though the core book/recommendation flow is database-driven.
9. Operational services
- pgweb is included as an administrative interface for inspecting and managing PostgreSQL.
- The health endpoint exists to check whether dependent services are reachable, which is important because the backend depends on both PostgreSQL and the ML service.
- Dockerfiles exist for all runtime components, so the repo is designed for containerized local deployment and reproducible environments.
10. Bootstrap and batch utilities
- The repository includes scripts for non-interactive data setup, especially scripts/seed_users.py for creating test users and evaluating recommendation behavior.
- The ML service also performs a one-time-ish heavy startup task: importing the book corpus and precomputing embeddings, which is why first boot is slower than normal.
11. Main end-to-end flow
- A user opens the React app → the frontend calls the Next.js API → the backend validates JWTs and reads/writes PostgreSQL → recommendation requests are forwarded to the ML service → results are returned to the frontend.
- That makes the backend the main integration point, the database the durable state store, and the ML service the specialized ranking engine.
Main entry points: api Next API · db.js DB Layer · docker-compose.yml Docker Compose · App.jsx React App
Components 22
Frontend
React/Vite client UI and browser-side auth/state
- React App · Browser UI for browsing, auth, profile, and recommendations
frontend/src/App.jsx - Auth Context · Holds session state and login lifecycle in the browser
frontend/src/context/AuthContext.jsx - API Client · Single client-side entrypoint for backend requests
frontend/src/services/api.js - Protected Route · Blocks unauthenticated access to private screens
frontend/src/components/ProtectedRoute.jsx
Backend
Next.js API layer and application orchestration
- Next API · Central orchestration layer for application requests
backend/pages/api - Auth Routes · Register, login, and logout endpoints
backend/pages/api/auth - Book Routes · Catalog, detail, like, favorite, rating, and similar-book endpoints
backend/pages/api/books - Profile API · User profile retrieval and updates
backend/pages/api/users/profile.js - Recommendations API · Personalized recommendation endpoint
backend/pages/api/recommendations.js - Health Check · Service and dependency status endpoint
backend/pages/api/health.js - Auth Middleware · Validates protected requests using JWTs
backend/middleware/auth.js - JWT Utils · Token creation and verification helpers
backend/lib/jwt.js - DB Layer · Node connection layer for PostgreSQL
backend/lib/db.js - MinIO Client · Object storage integration helper
backend/lib/minio.js
ML Service
Python FastAPI recommendation engine
- FastAPI App · Recommendation service entrypoint
ml-service/app/main.py - Recommender · Adaptive recommendation logic and ranking strategies
ml-service/app/recommender.py - ML DB Layer · Python connection layer for cached embeddings and model data
ml-service/app/database.py - Seed Pipeline · Dataset import and embedding precompute startup work
ml-service/app/seed.py
Data & Ops
Persistent storage, dataset, and deployment glue
- Docker Compose · Coordinates the containerized stack
docker-compose.yml - Book Dataset · Imported book catalog used by backend and ML bootstrapping
BooksDatasetClean.csv - DB Schema · Persistent schema for users, books, embeddings, and interactions
database/init.sql - Seed Users · Creates test users and drives recommendation evaluation
scripts/seed_users.py
Connections 31
- Docker Compose → React App: runs · Starts the frontend container
- Docker Compose → Next API: runs · Starts the API backend container
- Docker Compose → FastAPI App: runs · Starts the ML service container
- Docker Compose → DB Schema: provisions · Bootstraps PostgreSQL schema and state
- React App → Auth Context: uses · Stores and exposes client session state
- React App → Protected Route: guards · Protects private pages
- React App → API Client: calls · All backend requests flow through one client gateway
- API Client → Next API: HTTP API · Sends user requests to the Next.js backend
- Protected Route → Auth Context: reads · Checks whether the user is authenticated
- Next API → Auth Routes: routes · Exposes auth endpoints
- Next API → Book Routes: routes · Exposes book domain endpoints
- Next API → Profile API: routes · Exposes profile endpoints
- Next API → Recommendations API: routes · Exposes personalized recommendations
- Next API → Health Check: routes · Exposes dependency checks
- Auth Routes → JWT Utils: issues tokens · Creates and verifies authentication tokens
- Auth Routes → DB Layer: stores users · Persists account data
- Book Routes → Auth Middleware: protects · Requires valid user identity for interactions
- Profile API → Auth Middleware: protects · Requires valid user identity for profile access
- Recommendations API → Auth Middleware: protects · Requires valid user identity for personalized results
- Book Routes → DB Layer: reads/writes · Loads catalog data and records interactions
- Profile API → DB Layer: reads/writes · Loads and updates profile data
- Recommendations API → DB Layer: reads history · Uses interaction history and user data
- Recommendations API → FastAPI App: delegates · Forwards recommendation work to the ML service
- FastAPI App → Recommender: serves · Exposes recommendation logic via FastAPI
- Recommender → ML DB Layer: uses · Reads and stores embeddings and model data
- Seed Pipeline → Book Dataset: imports · Builds corpus inputs from the book CSV
- Seed Pipeline → Recommender: primes · Computes embeddings and caches them at startup
- DB Schema → Book Dataset: references · Schema is populated from the imported catalog
- Seed Users → DB Layer: loads · Creates and exercises test-user data
- Health Check → DB Layer: checks · Verifies database reachability
- Health Check → FastAPI App: checks · Verifies ML service reachability