# madcoderzx/forms architecture

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

- Interactive diagram: https://gitdiagram.com/madcoderzx/forms
- Repository: https://github.com/madcoderzx/forms

## Overview

#### 1. System purpose and entry points
This is a containerized form-builder system: authenticated users create and manage forms, anonymous users submit published forms, and owners review responses. Its primary architectural objective is also demonstrating a React/Express/PostgreSQL Docker deployment.

Runtime entry points are the React bootstrap at `frontend/src/main.jsx`, Express startup at `backend/src/index.js` and `backend/src/server.js`, and Compose orchestration in `docker-compose.yml`.

#### 2. Browser application and routing
The frontend is a React 18/Vite single-page application rooted in `frontend/src/App.jsx`. It provides authentication pages, an owner dashboard/editor/response viewer, and an anonymous public-form page under `frontend/src/pages/`.

`frontend/src/components/ProtectedRoute.jsx` establishes the client-side access boundary for owner workflows. `frontend/src/context/AuthContext.jsx` holds authentication state for UI components. API calls are centralized through `frontend/src/api/apiClient.js`, keeping page components dependent on an API boundary rather than direct backend details.

#### 3. Edge delivery and API proxy boundary
Production frontend assets are built and served by Nginx through `frontend/Dockerfile` and `frontend/nginx.conf`. Nginx is the browser-facing service and proxies API requests to the Express backend, separating static SPA delivery from application API execution.

The frontend build is multi-stage: Node/Vite produces static assets, while the final Nginx image contains only runtime assets. The browser uses the configured API URL, with Compose providing the container-network deployment path.

#### 4. Express API composition
`backend/src/app.js` composes Express middleware and mounts route groups. `backend/src/routes/auth.js` owns registration, login, and current-user operations; `backend/src/routes/forms.js` owns protected form lifecycle operations; and `backend/src/routes/publicForms.js` exposes anonymous retrieval and response submission. `backend/src/routes/health.js` supports service health observation.

The API divides control flow into authenticated owner operations versus public form consumption. This is the principal authorization boundary, enforced by `backend/src/middleware/auth.js` before protected handlers.

#### 5. Identity and authorization
User identity is persisted by `backend/src/models/user.js`. Login/register handlers issue and validate JWTs through `backend/src/utils/jwt.js`; the auth middleware resolves bearer-token identity for protected routes.

The JWT secret is an environment-level security dependency, not frontend state. The frontend auth context and protected-route layer improve navigation behavior, but backend middleware remains the authoritative access-control enforcement point.

#### 6. Form-definition and submission data model
PostgreSQL is the durable system of record. The relational schema is initialized by `database/init.sql`; backend database connectivity is owned by `backend/src/config/db.js`.

The form model spans `backend/src/models/form.js`, `backend/src/models/question.js`, and `backend/src/models/option.js`: an owner creates a form and its structured questions/options. Submission flow uses `backend/src/models/response.js`, which records a public response and its answers. Public lookup is keyed by a public form identifier, while owner-facing form operations are tied to authenticated ownership.

#### 7. Container runtime and infrastructure
`docker-compose.yml` runs three cooperating services: frontend, backend, and PostgreSQL 15. They communicate on the isolated Compose network; frontend-to-backend traffic passes through Nginx, and backend-to-database traffic uses the PostgreSQL service connection string.

`backend/Dockerfile` packages the Node/Express runtime. `frontend/Dockerfile` packages the static UI in Nginx. Compose-managed PostgreSQL persistence uses a named volume, while initialization SQL establishes the schema on a fresh database. Environment inputs represented by `.env.example` include backend port, database URL, JWT secret, and frontend API URL.

Main entry points: [`backend/src/app.js`](https://github.com/madcoderzx/forms/blob/main/backend/src/app.js) (Express app), [`backend/src/routes/forms.js`](https://github.com/madcoderzx/forms/blob/main/backend/src/routes/forms.js) (Owner form routes), [`backend/src/config/db.js`](https://github.com/madcoderzx/forms/blob/main/backend/src/config/db.js) (Database client), [`docker-compose.yml`](https://github.com/madcoderzx/forms/blob/main/docker-compose.yml) (Docker Compose)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Browser &amp; Edge"]
  node_react["React SPA<br/>React/Vite<br/>[App.jsx]"]
  node_auth_ui["Auth state &amp; guards<br/>React auth<br/>[AuthContext.jsx]"]
  node_api_client["API client<br/>frontend API boundary<br/>[apiClient.js]"]
  node_nginx{{"Nginx edge<br/>static server / proxy<br/>[nginx.conf]"}}
end

subgraph group_api["Express API"]
  node_express["Express app<br/>API composition<br/>[app.js]"]
  node_auth_routes["Auth routes<br/>route group<br/>[auth.js]"]
  node_owner_routes["Owner form routes<br/>protected route group<br/>[forms.js]"]
  node_public_routes["Public form routes<br/>public route group<br/>[publicForms.js]"]
  node_auth_middleware["Auth middleware<br/>authorization boundary<br/>[auth.js]"]
  node_jwt["JWT utility<br/>token service<br/>[jwt.js]"]
end

subgraph group_data["Data &amp; Identity"]
  node_db_client["Database client<br/>PostgreSQL connection<br/>[db.js]"]
  node_identity_model["User model<br/>identity persistence<br/>[user.js]"]
  node_form_models["Form definition models<br/>form persistence<br/>[form.js]"]
  node_response_model["Response model<br/>submission persistence<br/>[response.js]"]
  node_postgres[("PostgreSQL 15<br/>durable system of record<br/>[init.sql]")]
end

subgraph group_runtime["Container Runtime"]
  node_compose{{"Docker Compose<br/>service orchestration<br/>[docker-compose.yml]"}}
  node_frontend_image["Frontend image<br/>multi-stage Docker image"]
  node_backend_runtime["Express runtime<br/>Node container<br/>[server.js]"]
end

node_react -->|"uses"| node_auth_ui
node_react -->|"requests through"| node_api_client
node_auth_ui -->|"supplies session"| node_api_client
node_api_client -->|"browser API traffic"| node_nginx
node_nginx -->|"proxies /api"| node_express
node_express -->|"mounts"| node_auth_routes
node_express -->|"mounts"| node_owner_routes
node_express -->|"mounts"| node_public_routes
node_owner_routes -->|"requires"| node_auth_middleware
node_auth_middleware -->|"validates token"| node_jwt
node_auth_routes -->|"issues tokens"| node_jwt
node_auth_routes -->|"manages users"| node_identity_model
node_owner_routes -->|"manages definitions"| node_form_models
node_owner_routes -->|"reviews responses"| node_response_model
node_public_routes -->|"loads published form"| node_form_models
node_public_routes -->|"records submission"| node_response_model
node_identity_model -->|"queries via"| node_db_client
node_form_models -->|"queries via"| node_db_client
node_response_model -->|"queries via"| node_db_client
node_db_client -->|"connects"| node_postgres
node_compose -->|"runs frontend service"| node_frontend_image
node_compose -->|"runs backend service"| node_backend_runtime
node_compose -->|"runs persistent database"| node_postgres
node_frontend_image -->|"packages"| node_nginx
node_backend_runtime -->|"starts"| node_express

click node_react "https://github.com/madcoderzx/forms/blob/main/frontend/src/App.jsx"
click node_auth_ui "https://github.com/madcoderzx/forms/blob/main/frontend/src/context/AuthContext.jsx"
click node_api_client "https://github.com/madcoderzx/forms/blob/main/frontend/src/api/apiClient.js"
click node_nginx "https://github.com/madcoderzx/forms/blob/main/frontend/nginx.conf"
click node_express "https://github.com/madcoderzx/forms/blob/main/backend/src/app.js"
click node_auth_routes "https://github.com/madcoderzx/forms/blob/main/backend/src/routes/auth.js"
click node_owner_routes "https://github.com/madcoderzx/forms/blob/main/backend/src/routes/forms.js"
click node_public_routes "https://github.com/madcoderzx/forms/blob/main/backend/src/routes/publicForms.js"
click node_auth_middleware "https://github.com/madcoderzx/forms/blob/main/backend/src/middleware/auth.js"
click node_jwt "https://github.com/madcoderzx/forms/blob/main/backend/src/utils/jwt.js"
click node_db_client "https://github.com/madcoderzx/forms/blob/main/backend/src/config/db.js"
click node_identity_model "https://github.com/madcoderzx/forms/blob/main/backend/src/models/user.js"
click node_form_models "https://github.com/madcoderzx/forms/blob/main/backend/src/models/form.js"
click node_response_model "https://github.com/madcoderzx/forms/blob/main/backend/src/models/response.js"
click node_postgres "https://github.com/madcoderzx/forms/blob/main/database/init.sql"
click node_compose "https://github.com/madcoderzx/forms/blob/main/docker-compose.yml"
click node_frontend_image "https://github.com/madcoderzx/forms/blob/main/frontend/Dockerfile"
click node_backend_runtime "https://github.com/madcoderzx/forms/blob/main/backend/src/server.js"

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_react,node_auth_ui,node_api_client,node_nginx toneBlue
class node_express,node_auth_routes,node_owner_routes,node_public_routes,node_auth_middleware,node_jwt toneAmber
class node_db_client,node_identity_model,node_form_models,node_response_model,node_postgres toneMint
class node_compose,node_frontend_image,node_backend_runtime toneRose
```

## Components

### Browser & Edge

- [React SPA](https://github.com/madcoderzx/forms/blob/main/frontend/src/App.jsx): React/Vite · Owner and public form UI. · `frontend/src/App.jsx`
- [Auth state & guards](https://github.com/madcoderzx/forms/blob/main/frontend/src/context/AuthContext.jsx): React auth · Maintains session UI state and owner navigation boundary. · `frontend/src/context/AuthContext.jsx`
- [API client](https://github.com/madcoderzx/forms/blob/main/frontend/src/api/apiClient.js): frontend API boundary · `frontend/src/api/apiClient.js`
- [Nginx edge](https://github.com/madcoderzx/forms/blob/main/frontend/nginx.conf): static server / proxy · Serves the SPA and proxies API traffic. · `frontend/nginx.conf`

### Express API

- [Express app](https://github.com/madcoderzx/forms/blob/main/backend/src/app.js): API composition · Mounts middleware and route groups. · `backend/src/app.js`
- [Auth routes](https://github.com/madcoderzx/forms/blob/main/backend/src/routes/auth.js): route group · Registration, login, and current-user endpoints. · `backend/src/routes/auth.js`
- [Owner form routes](https://github.com/madcoderzx/forms/blob/main/backend/src/routes/forms.js): protected route group · Authenticated form lifecycle and response review. · `backend/src/routes/forms.js`
- [Public form routes](https://github.com/madcoderzx/forms/blob/main/backend/src/routes/publicForms.js): public route group · Anonymous form lookup and submission. · `backend/src/routes/publicForms.js`
- [Auth middleware](https://github.com/madcoderzx/forms/blob/main/backend/src/middleware/auth.js): authorization boundary · Resolves bearer-token identity before owner handlers. · `backend/src/middleware/auth.js`
- [JWT utility](https://github.com/madcoderzx/forms/blob/main/backend/src/utils/jwt.js): token service · Issues and validates login tokens using the environment secret. · `backend/src/utils/jwt.js`

### Data & Identity

- [Database client](https://github.com/madcoderzx/forms/blob/main/backend/src/config/db.js): PostgreSQL connection · `backend/src/config/db.js`
- [User model](https://github.com/madcoderzx/forms/blob/main/backend/src/models/user.js): identity persistence · `backend/src/models/user.js`
- [Form definition models](https://github.com/madcoderzx/forms/blob/main/backend/src/models/form.js): form persistence · Forms, questions, and options owned by authenticated users. · `backend/src/models/form.js`
- [Response model](https://github.com/madcoderzx/forms/blob/main/backend/src/models/response.js): submission persistence · Stores public responses and answers. · `backend/src/models/response.js`
- [PostgreSQL 15](https://github.com/madcoderzx/forms/blob/main/database/init.sql): durable system of record · Persists users, form definitions, and submissions. · `database/init.sql`

### Container Runtime

- [Docker Compose](https://github.com/madcoderzx/forms/blob/main/docker-compose.yml): service orchestration · Runs frontend, backend, and database on an isolated network. · `docker-compose.yml`
- [Frontend image](https://github.com/madcoderzx/forms/blob/main/frontend/Dockerfile): multi-stage Docker image · Builds Vite assets and runs only Nginx at runtime. · `frontend/Dockerfile`
- [Express runtime](https://github.com/madcoderzx/forms/blob/main/backend/src/server.js): Node container · Starts the Express API service. · `backend/src/server.js`

## Connections

- React SPA → Auth state & guards: uses
- React SPA → API client: requests through
- Auth state & guards → API client: supplies session
- API client → Nginx edge: browser API traffic
- Nginx edge → Express app: proxies /api
- Express app → Auth routes: mounts
- Express app → Owner form routes: mounts
- Express app → Public form routes: mounts
- Owner form routes → Auth middleware: requires
- Auth middleware → JWT utility: validates token
- Auth routes → JWT utility: issues tokens
- Auth routes → User model: manages users
- Owner form routes → Form definition models: manages definitions
- Owner form routes → Response model: reviews responses
- Public form routes → Form definition models: loads published form
- Public form routes → Response model: records submission
- User model → Database client: queries via
- Form definition models → Database client: queries via
- Response model → Database client: queries via
- Database client → PostgreSQL 15: connects
- Docker Compose → Frontend image: runs frontend service
- Docker Compose → Express runtime: runs backend service
- Docker Compose → PostgreSQL 15: runs persistent database
- Frontend image → Nginx edge: packages
- Express runtime → Express app: starts
