How sabbirhosen44/ai-website-builder works
1. This is a two-tier full-stack TypeScript application: a React/Vite frontend and an Express/Node backend. The backend exposes REST APIs under /api/v1 plus an auth surface under /api/auth, and the frontend consumes them through Axios and TanStack Query.
2. The backend follows a clear controller → service → persistence flow. Routes in backend/routes map to controllers, controllers delegate to services, and services use Prisma via backend/lib/prisma.ts to access PostgreSQL. Error handling is centralized through async wrappers and a global error middleware.
3. The main domain is “projects” and “users.” Project APIs handle creating projects, updating generated code, saving manual edits, previewing content, publishing/unpublishing, listing public projects, deleting projects, and rolling back to version snapshots. User APIs handle credit balance, credit purchase, and listing a user’s projects.
4. AI generation is isolated in backend/services/ai.service.ts and configured through backend/config/ai.config.ts, openai.config.ts, and prompt constants. The README indicates a fallback chain across multiple LLMs, so AI request handling is a dedicated subsystem rather than embedded in controllers.
5. Credit-based monetization is a separate backend concern. backend/config/stripe.config.ts, backend/services/stripe.service.ts, and backend/controllers/webhook.controller.ts indicate Stripe-backed payments and webhook processing, likely used to replenish user credits and reconcile payment events.
6. Authentication is backend-enforced and likely session/JWT-based via Better Auth plus custom middleware. backend/lib/auth.ts and backend/middlewares/auth.middleware.ts show that protected project and user routes are gated before business logic runs.
7. Persistence is centered on Prisma and PostgreSQL. The presence of a Prisma schema and migrations suggests the system stores users, projects, versions, and conversation history as first-class entities, with version immutability being an architectural feature for rollback and auditability.
8. The frontend is organized as a routed SPA. App.tsx, layout/RootLayout.tsx, and pages like HomePage, ProjectsPage, MyProjectsPage, PreviewPage, PricingPage, SettingsPage, and AuthPage define the user-facing flows from marketing pages to authenticated product areas.
9. The editor experience is a composed UI made of specialized panels rather than a monolithic page. Components like ChatSidebar, EditorPanel, PreviewPanel, and ConfirmModal suggest a split workspace where the user chats with the AI, inspects generated code, and previews output side by side.
10. Client-side state and server state are separated. src/lib/react-query.ts, feature hooks in src/hooks, and API modules in src/api indicate that remote data is fetched and cached centrally, while UI components stay mostly declarative.
11. Auth and API configuration on the frontend are also isolated. src/lib/auth-client.ts and src/lib/axios.ts act as boundary adapters between the SPA and backend, so authentication, base URLs, and request behavior are not scattered across components.
12. The main runtime data flow is: user input in the chat/editor UI → frontend API call → authenticated backend controller → service layer → AI/Stripe/Prisma integrations → persisted project/version state → refreshed UI preview and project views. The architecture is therefore centered on an AI-assisted project lifecycle with persistence, billing, and rollback as supporting subsystems.
Main entry points: project.route.ts API Routes · project.service.ts Project Service · axios.ts API Layer · HomePage.tsx Pages
Components 19
Backend
Express/Node API, business logic, and persistence
- Server · Bootstraps the API server and mounts routes and middleware.
backend/server.ts - API Routes · Maps REST surfaces to controllers for projects and users.
backend/routes/project.route.ts - Project Ctrl · Handles project lifecycle requests and delegates to services.
backend/controllers/project.controller.ts - User Ctrl · Handles user credit and project listing requests.
backend/controllers/user.controller.ts - Webhook Ctrl · Processes Stripe webhook events from the payment surface.
backend/controllers/webhook.controller.ts - Project Service · Implements project creation, updates, versions, preview, publish, and rollback.
backend/services/project.service.ts - User Service · Implements user-centric credit and ownership workflows.
backend/services/user.service.ts - AI Service · Coordinates generation requests and model fallback logic.
backend/services/ai.service.ts - Stripe Service · Handles payment-side logic that supports credit purchases and reconciliation.
backend/services/stripe.service.ts - Prisma · Shared database access layer for PostgreSQL via Prisma.
backend/lib/prisma.ts - Auth Lib · Backend auth integration and session/JWT support.
backend/lib/auth.ts - Middleware · Async wrapping, auth gating, and global error handling.
backend/middlewares/asyncHandler.middleware.ts
Frontend
React/Vite SPA, UI flows, and client data access
- SPA Shell · Top-level client application and route composition.
frontend/src/App.tsx - Root Layout · Shared frame for the routed frontend experience.
frontend/src/layout/RootLayout.tsx - Pages · Public and authenticated screens for marketing, auth, projects, preview, pricing, and settings.
frontend/src/pages/HomePage.tsx - Workspace UI · Composed chat/editor/preview workspace for AI-assisted building.
frontend/src/components/ChatSidebar.tsx - API Layer · Axios-based backend adapter and feature API modules.
frontend/src/lib/axios.ts - Query State · TanStack Query setup and feature hooks for cached remote data.
frontend/src/lib/react-query.ts - Auth Client · Frontend auth integration boundary for the SPA.
frontend/src/lib/auth-client.ts
Connections 20
- Server → Middleware: uses · Requests pass through async handling, auth checks, and error middleware.
- Server → API Routes: mounts · Server exposes REST routes under the API surface.
- API Routes → Project Ctrl: maps to · Project routes dispatch into project controller handlers.
- API Routes → User Ctrl: maps to · User routes dispatch into user controller handlers.
- API Routes → Webhook Ctrl: maps to · Webhook endpoints dispatch into payment event handling.
- Project Ctrl → Project Service: delegates · Controllers keep business logic in the service layer.
- User Ctrl → User Service: delegates · User requests are processed by domain services.
- Webhook Ctrl → Stripe Service: delegates · Webhook events are translated into Stripe-backed domain actions.
- Project Service → AI Service: calls · Project generation and regeneration rely on AI orchestration.
- Project Service → Prisma: persists · Projects, versions, and related state are stored through Prisma.
- User Service → Prisma: persists · User credits and ownership data are read and updated in PostgreSQL.
- Stripe Service → Prisma: persists · Payment reconciliation updates application state through the database.
- Middleware → Auth Lib: guards · Protected endpoints are enforced through backend auth integration.
- SPA Shell → Root Layout: composes · The SPA is framed by shared layout and routing structure.
- Root Layout → Pages: renders · Route-level pages provide the main product and marketing flows.
- Pages → Workspace UI: uses · Editor and preview pages assemble the chat/editor workspace.
- Workspace UI → API Layer: calls · UI actions trigger backend requests through the API boundary.
- API Layer → Query State: feeds · API modules are consumed through query hooks and cached server state.
- API Layer → Auth Client: uses · Request behavior and authentication are configured at the client boundary.
- Query State → Pages: powers · Pages read and refresh server state through hooks.