# Pavti Pustak Setup Guide

This repository has two parts:

- Backend: `pp_test_backend`
- Frontend: `pp_test_backend/frontDev`

## Minimum Requirements

- Node.js: 18.x or newer. Node 20.x LTS is recommended.
- npm: the version bundled with your Node installation.
- Database: MySQL 8.0 or newer.
- RAM: 2 GB minimum for local development, 4 GB or more recommended for production builds.
- Disk: at least 1 GB free for dependencies, uploads, and build artifacts.

## Shared Setup Steps

1. Clone the repository.
2. Install the backend dependencies.
3. Install the frontend dependencies in `pp_test_backend/frontDev`.
4. Create `pp_test_backend/.env` from `pp_test_backend/.env.example`.
5. Configure database credentials in `.env` if you are not using the defaults.
6. Run the database sync/migration step with `node alter.js` from `pp_test_backend`.
7. Build the frontend with `npm run build:frontend` from `pp_test_backend`.
8. Start the backend with `node main.js` from `pp_test_backend`.

## LocalHost

Best for development on your machine.

- Install MySQL locally and create the `test_db` database.
- Copy `pp_test_backend/.env.example` to `pp_test_backend/.env`.
- Update `DB_HOST`, `DB_NAME`, `DB_USER`, `DB_PASS`, and `SESSION_SECRET` as needed.
- Run `node setup.js` from the repo root to install dependencies and prepare the frontend.
- Run `node alter.js` from `pp_test_backend` to sync the schema.
- Run `node main.js` from `pp_test_backend`.
- Open the app in your browser on the backend port, usually `http://localhost:5000`.

## CPanel

Best when you have shared hosting with Node.js support.

- Use a MySQL database provided by cPanel.
- Create a database user and grant it access to the database.
- Upload the backend files to your app directory.
- Upload the built frontend output into `pp_test_backend/frontend`.
- Create `pp_test_backend/.env` with the cPanel database host, name, user, password, and a strong `SESSION_SECRET`.
- Run `npm install` in `pp_test_backend` and `pp_test_backend/frontDev` if shell access is available.
- Run `node alter.js` from `pp_test_backend` if the host allows Node execution.
- Point the Node app entrypoint to `main.js`.

Notes:

- If the host does not allow long-running Node processes, you may need a reverse proxy or a different platform.
- If your cPanel account exposes a temporary app URL, verify cookies and uploads there before switching domains.

## Linux VPS

Best for full control with Ubuntu, Debian, or similar.

- Install Node.js 18+ and MySQL 8.0+.
- Create a dedicated MySQL database and user.
- Copy `.env.example` to `.env` and fill in production values.
- Run `node setup.js` from the repo root.
- Run `node alter.js` from `pp_test_backend`.
- Build the frontend with `npm run build:frontend` from `pp_test_backend`.
- Run the backend with a process manager such as PM2 or systemd.
- Put Nginx or Apache in front of the app as a reverse proxy.
- Serve HTTPS with a certificate such as Let’s Encrypt.

Recommended layout:

- App process: Node on port `5000`
- Reverse proxy: Nginx on port `80` / `443`
- Database: local MySQL service or managed MySQL

## AWS

Best for scalable deployments on EC2, Lightsail, or container-based services.

- Use EC2 or Lightsail for a simple VPS-style deployment, or ECS/Fargate if you prefer containers.
- Use Amazon RDS MySQL 8.0 or compatible MySQL 8.0 managed service.
- Store secrets in AWS Systems Manager Parameter Store or Secrets Manager.
- Build the frontend before deployment and copy the output into `pp_test_backend/frontend`.
- Run the backend with PM2, systemd, or a container supervisor.
- Put Application Load Balancer or Nginx in front of the app.
- Open only the ports you need, typically `80`, `443`, and the internal app port if required.

Recommended AWS minimums:

- EC2: 1 vCPU, 2 GB RAM minimum for testing, 2 vCPU / 4 GB recommended.
- RDS: MySQL 8.0, small instance for testing, larger for production usage.

## Azure

Best for App Service, VM, or container-based hosting.

- Use Azure Database for MySQL Flexible Server or another MySQL 8.0 compatible option.
- Store secrets in App Service configuration, Key Vault, or environment variables.
- Deploy the backend as an App Service, VM process, or container.
- Deploy the built frontend into `pp_test_backend/frontend` before publishing.
- Run `node alter.js` during initial setup or deployment.
- Make sure the app can write to the uploads directory.
- Put Azure Front Door, Application Gateway, or App Service in front of the app depending on your architecture.

Recommended Azure minimums:

- App Service / VM: at least 1 vCPU and 2 GB RAM for testing.
- MySQL: MySQL 8.0 compatible managed database.

## Google

Best for Compute Engine, Cloud Run, or container-based hosting.

- Use Cloud SQL for MySQL 8.0 compatible database hosting.
- Use Compute Engine for a VM-style deployment or Cloud Run for containers.
- Store secrets in Secret Manager or environment variables.
- Build the frontend before deployment and include the generated files in `pp_test_backend/frontend`.
- Run `node alter.js` the first time the database is provisioned.
- Ensure persistent storage is available for uploads if you use a container platform.

Recommended Google minimums:

- Compute Engine: 1 vCPU and 2 GB RAM minimum for testing.
- Cloud SQL: MySQL 8.0 compatible instance.

## Setup Script

From the repository root, run:

```bash
node setup.js
```

This script will:

- install backend dependencies
- install frontend dependencies
- copy `.env.example` to `.env` if needed
- optionally build the frontend with `--build-frontend`
- optionally sync the database schema with `--sync-db`

Example:

```bash
node setup.js --build-frontend --sync-db
```

## After Setup

- Run `node alter.js` from `pp_test_backend` to sync schema changes.
- Start the backend with `node main.js` from `pp_test_backend`.
- For production, keep the backend behind HTTPS and a reverse proxy.
