# pacopedrosa/smartboooks architecture

> Architecture diagram of the GitHub repository pacopedrosa/smartboooks, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-06-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pacopedrosa/smartboooks
- Repository: https://github.com/pacopedrosa/smartboooks

## Overview

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: [`backend/pages/api`](https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api) (Next API), [`backend/lib/db.js`](https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/db.js) (DB Layer), [`docker-compose.yml`](https://github.com/pacopedrosa/smartboooks/blob/main/docker-compose.yml) (Docker Compose), [`frontend/src/App.jsx`](https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/App.jsx) (React App)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend"]
  node_frontend_app["React App<br/>spa<br/>[App.jsx]"]
  node_auth_context(("Auth Context<br/>client state<br/>[AuthContext.jsx]"))
  node_api_client["API Client<br/>frontend gateway<br/>[api.js]"]
  node_protected_route["Protected Route<br/>route guard<br/>[ProtectedRoute.jsx]"]
end

subgraph group_backend["Backend"]
  node_backend_api["Next API<br/>api backend"]
  node_auth_routes["Auth Routes<br/>api domain"]
  node_book_routes["Book Routes<br/>api domain"]
  node_profile_route["Profile API<br/>api domain<br/>[profile.js]"]
  node_recommendations_api["Recommendations API<br/>api domain<br/>[recommendations.js]"]
  node_health_api["Health Check<br/>api domain<br/>[health.js]"]
  node_auth_middleware["Auth Middleware<br/>request guard<br/>[auth.js]"]
  node_jwt_lib["JWT Utils<br/>security library<br/>[jwt.js]"]
  node_db_lib["DB Layer<br/>postgres client<br/>[db.js]"]
  node_minio_lib["MinIO Client<br/>object storage client<br/>[minio.js]"]
end

subgraph group_ml["ML Service"]
  node_ml_app["FastAPI App<br/>ml service<br/>[main.py]"]
  node_recommender["Recommender<br/>ranking engine<br/>[recommender.py]"]
  node_ml_db["ML DB Layer<br/>postgres client<br/>[database.py]"]
  node_seed_pipeline["Seed Pipeline<br/>bootstrap job<br/>[seed.py]"]
end

subgraph group_data["Data & Ops"]
  node_docker_compose["Docker Compose<br/>deployment<br/>[docker-compose.yml]"]
  node_dataset["Book Dataset<br/>corpus"]
  node_schema[("DB Schema<br/>postgres schema<br/>[init.sql]")]
  node_seed_users["Seed Users<br/>utility script<br/>[seed_users.py]"]
end

node_docker_compose -->|"runs"| node_frontend_app
node_docker_compose -->|"runs"| node_backend_api
node_docker_compose -->|"runs"| node_ml_app
node_docker_compose -->|"provisions"| node_schema
node_frontend_app -->|"uses"| node_auth_context
node_frontend_app -->|"guards"| node_protected_route
node_frontend_app -->|"calls"| node_api_client
node_api_client -->|"HTTP API"| node_backend_api
node_protected_route -->|"reads"| node_auth_context
node_backend_api -->|"routes"| node_auth_routes
node_backend_api -->|"routes"| node_book_routes
node_backend_api -->|"routes"| node_profile_route
node_backend_api -->|"routes"| node_recommendations_api
node_backend_api -->|"routes"| node_health_api
node_auth_routes -->|"issues tokens"| node_jwt_lib
node_auth_routes -->|"stores users"| node_db_lib
node_book_routes -->|"protects"| node_auth_middleware
node_profile_route -->|"protects"| node_auth_middleware
node_recommendations_api -->|"protects"| node_auth_middleware
node_book_routes -->|"reads/writes"| node_db_lib
node_profile_route -->|"reads/writes"| node_db_lib
node_recommendations_api -->|"reads history"| node_db_lib
node_recommendations_api -->|"delegates"| node_ml_app
node_ml_app -->|"serves"| node_recommender
node_recommender -->|"uses"| node_ml_db
node_seed_pipeline -->|"imports"| node_dataset
node_seed_pipeline -->|"primes"| node_recommender
node_schema -->|"references"| node_dataset
node_seed_users -->|"loads"| node_db_lib
node_health_api -->|"checks"| node_db_lib
node_health_api -->|"checks"| node_ml_app

click node_docker_compose "https://github.com/pacopedrosa/smartboooks/blob/main/docker-compose.yml"
click node_frontend_app "https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/App.jsx"
click node_auth_context "https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/context/AuthContext.jsx"
click node_api_client "https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/services/api.js"
click node_protected_route "https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/components/ProtectedRoute.jsx"
click node_backend_api "https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api"
click node_auth_routes "https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api/auth"
click node_book_routes "https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api/books"
click node_profile_route "https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/users/profile.js"
click node_recommendations_api "https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/recommendations.js"
click node_health_api "https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/health.js"
click node_auth_middleware "https://github.com/pacopedrosa/smartboooks/blob/main/backend/middleware/auth.js"
click node_jwt_lib "https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/jwt.js"
click node_db_lib "https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/db.js"
click node_minio_lib "https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/minio.js"
click node_ml_app "https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/main.py"
click node_recommender "https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/recommender.py"
click node_ml_db "https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/database.py"
click node_seed_pipeline "https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/seed.py"
click node_dataset "https://github.com/pacopedrosa/smartboooks/blob/main/BooksDatasetClean.csv"
click node_schema "https://github.com/pacopedrosa/smartboooks/blob/main/database/init.sql"
click node_seed_users "https://github.com/pacopedrosa/smartboooks/blob/main/scripts/seed_users.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_auth_context,node_api_client,node_protected_route toneBlue
class node_backend_api,node_auth_routes,node_book_routes,node_profile_route,node_recommendations_api,node_health_api,node_auth_middleware,node_jwt_lib,node_db_lib,node_minio_lib toneAmber
class node_ml_app,node_recommender,node_ml_db,node_seed_pipeline toneMint
class node_docker_compose,node_dataset,node_schema,node_seed_users toneRose
```

## Components

### Frontend

React/Vite client UI and browser-side auth/state

- [React App](https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/App.jsx): spa · Browser UI for browsing, auth, profile, and recommendations · `frontend/src/App.jsx`
- [Auth Context](https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/context/AuthContext.jsx): client state · Holds session state and login lifecycle in the browser · `frontend/src/context/AuthContext.jsx`
- [API Client](https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/services/api.js): frontend gateway · Single client-side entrypoint for backend requests · `frontend/src/services/api.js`
- [Protected Route](https://github.com/pacopedrosa/smartboooks/blob/main/frontend/src/components/ProtectedRoute.jsx): route guard · Blocks unauthenticated access to private screens · `frontend/src/components/ProtectedRoute.jsx`

### Backend

Next.js API layer and application orchestration

- [Next API](https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api): api backend · Central orchestration layer for application requests · `backend/pages/api`
- [Auth Routes](https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api/auth): api domain · Register, login, and logout endpoints · `backend/pages/api/auth`
- [Book Routes](https://github.com/pacopedrosa/smartboooks/tree/main/backend/pages/api/books): api domain · Catalog, detail, like, favorite, rating, and similar-book endpoints · `backend/pages/api/books`
- [Profile API](https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/users/profile.js): api domain · User profile retrieval and updates · `backend/pages/api/users/profile.js`
- [Recommendations API](https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/recommendations.js): api domain · Personalized recommendation endpoint · `backend/pages/api/recommendations.js`
- [Health Check](https://github.com/pacopedrosa/smartboooks/blob/main/backend/pages/api/health.js): api domain · Service and dependency status endpoint · `backend/pages/api/health.js`
- [Auth Middleware](https://github.com/pacopedrosa/smartboooks/blob/main/backend/middleware/auth.js): request guard · Validates protected requests using JWTs · `backend/middleware/auth.js`
- [JWT Utils](https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/jwt.js): security library · Token creation and verification helpers · `backend/lib/jwt.js`
- [DB Layer](https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/db.js): postgres client · Node connection layer for PostgreSQL · `backend/lib/db.js`
- [MinIO Client](https://github.com/pacopedrosa/smartboooks/blob/main/backend/lib/minio.js): object storage client · Object storage integration helper · `backend/lib/minio.js`

### ML Service

Python FastAPI recommendation engine

- [FastAPI App](https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/main.py): ml service · Recommendation service entrypoint · `ml-service/app/main.py`
- [Recommender](https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/recommender.py): ranking engine · Adaptive recommendation logic and ranking strategies · `ml-service/app/recommender.py`
- [ML DB Layer](https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/database.py): postgres client · Python connection layer for cached embeddings and model data · `ml-service/app/database.py`
- [Seed Pipeline](https://github.com/pacopedrosa/smartboooks/blob/main/ml-service/app/seed.py): bootstrap job · Dataset import and embedding precompute startup work · `ml-service/app/seed.py`

### Data & Ops

Persistent storage, dataset, and deployment glue

- [Docker Compose](https://github.com/pacopedrosa/smartboooks/blob/main/docker-compose.yml): deployment · Coordinates the containerized stack · `docker-compose.yml`
- [Book Dataset](https://github.com/pacopedrosa/smartboooks/blob/main/BooksDatasetClean.csv): corpus · Imported book catalog used by backend and ML bootstrapping · `BooksDatasetClean.csv`
- [DB Schema](https://github.com/pacopedrosa/smartboooks/blob/main/database/init.sql): postgres schema · Persistent schema for users, books, embeddings, and interactions · `database/init.sql`
- [Seed Users](https://github.com/pacopedrosa/smartboooks/blob/main/scripts/seed_users.py): utility script · Creates test users and drives recommendation evaluation · `scripts/seed_users.py`

## Connections

- 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
