diff --git a/Dockerfile.dev b/Dockerfile.dev index eca3c36..0463c02 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -1,5 +1,7 @@ FROM node:20-alpine +RUN apk add --no-cache sqlite + WORKDIR /server # For dev, we install in container, but to speed up, perhaps copy package and install diff --git a/README.md b/README.md index 69f60a8..54e8c4b 100644 --- a/README.md +++ b/README.md @@ -4,107 +4,58 @@ This project consists of both the front-end React client and back-end Node.js ga The application is designed to run inside Docker containers. Do not run `npm install` or modify `node_modules` locally, as this can create environment drift. All development and building should be done through the provided Docker workflows. -## Development +## Development and Production -For live development with hot reloading, React DevTools support, and TypeScript compilation on-the-fly. +The application can be launched in development or production mode by setting the `PRODUCTION` environment variable. + +- Set `PRODUCTION=0` (or leave unset) for development mode: hot-reloading server and client. +- Set `PRODUCTION=1` for production mode: static built site served by the server. ### Prerequisites - Docker - Docker Compose -### Running the Server (Backend) - -The server uses TypeScript with hot reloading via `ts-node-dev`. +### Launching ```bash -docker compose -f docker-compose.dev.yml up +# For development (hot-reload) +PRODUCTION=0 ./launch.sh + +# For production (static build) +PRODUCTION=1 ./launch.sh ``` -This will: -- Build the server container using `Dockerfile.server` -- Mount the `server/` directory for live code changes -- Run `npm run start:dev` which uses `ts-node-dev` for hot reloading -- Expose the server on port 8930 +#### Development Mode -### Testing the Server +When `PRODUCTION=0`: +- Server runs with hot-reloading via `ts-node-dev` on port 8930 +- Client runs with React hot module replacement on port 3001 (HTTPS) +- Client proxies API calls to the server +- Both services run in separate containers with mounted source directories -The server uses Jest for unit testing. Tests are located in `server/tests/`. +Open `https://localhost:3001` in your browser for the client. Changes to server or client code will trigger automatic reloads. -To run tests: +#### Production Mode -```bash -docker compose run --rm server npm test -``` - -This will run all test files matching `*.test.ts` in the `server/tests/` directory. - -To run tests in watch mode: - -```bash -docker compose run --rm server npm run test:watch -``` - -Note: If you add `test:watch` script, but for now, it's just `test`. - -### Running the Client (Frontend) - -The client is a React application with hot reloading and debug support. - -```bash -docker compose -f docker-compose.client.dev.yml up -``` - -This will: -- Mount the `client/` directory for live code changes -- Run `npm start` in development mode -- Enable React DevTools and hot module replacement -- Expose the client on port 3001 (HTTPS) -- Proxy API calls to the server at `http://host.docker.internal:8930` - -### Full Development Stack - -To run both server and client together: - -```bash -# Terminal 1: Server -docker compose -f docker-compose.dev.yml up - -# Terminal 2: Client -docker compose -f docker-compose.client.dev.yml up -``` - -Open `https://localhost:3001` in your browser. The client will hot-reload on code changes, and the server will restart automatically on backend changes. - -## Production - -For production deployment, the client is built as static files and served directly by the Node.js server. - -### Building - -```bash -docker compose build -``` - -This builds the production image using `Dockerfile`, which: -- Installs server dependencies and builds TypeScript to `dist/` -- Installs client dependencies and builds static files to `client/build/` -- Copies the built client files to be served by the server - -### Running - -```bash -docker compose up -``` - -This will: -- Start the server on port 8930 -- Serve the built client static files -- Mount the `db/` directory for persistent database storage -- Mount `server/routes/` for dynamic route files +When `PRODUCTION=1`: +- Builds static client files and serves them directly from the Node.js server +- Server runs compiled TypeScript from `dist/` on port 8930 +- No hot-reloading; requires rebuild for changes +- Database mounted for persistence The application will be available at `http://localhost:8930`. +### Building (for Production) + +If you need to manually build the production image: + +```bash +docker-compose build +``` + +This builds the image with server and client dependencies installed and built. + ### Environment Variables Create a `.env` file in the project root with any required environment variables. The server start script loads these via `export $(cat ../.env | xargs)`. diff --git a/docker-compose.client.dev.yml b/docker-compose.client.dev.yml deleted file mode 100644 index 66de515..0000000 --- a/docker-compose.client.dev.yml +++ /dev/null @@ -1,23 +0,0 @@ -services: - peddlers-client: - build: - context: . - dockerfile: Dockerfile - hostname: peddlers-client - container_name: peddlers-client - working_dir: /client - volumes: - - ./client:/client:rw - ports: - - 3001:3000 - environment: - - BROWSER=none - - HTTPS=true - - HOST=0.0.0.0 - command: ["bash", "-c", "cd /client && npm install --legacy-peer-deps --silent --no-audit --no-fund && npm start"] - networks: - - peddlers-network - -networks: - peddlers-network: - driver: bridge diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml deleted file mode 100644 index f39894d..0000000 --- a/docker-compose.dev.yml +++ /dev/null @@ -1,21 +0,0 @@ -services: - peddlers-of-ketran: - hostname: peddlers-server - build: - context: . - dockerfile: Dockerfile.dev - volumes: - - ./server:/server:rw - - ./db:/db:rw - #- ./server/node_modules:app/server/node_modules:rw - command: ["sh", "-c", "cd /server && npm install --no-audit --no-fund --silent && npm rebuild sqlite3 && npm run start:dev"] - ports: - - 8930:8930 - environment: - - NODE_ENV=development - networks: - - peddlers-network - -networks: - peddlers-network: - driver: bridge diff --git a/docker-compose.yml b/docker-compose.yml index 5eed084..696d633 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,6 @@ services: peddlers-of-ketran: + profiles: [prod] container_name: ketr.ketran build: context: . @@ -11,15 +12,43 @@ services: - ./db:/db:rw - ./server/routes:/server/routes:ro working_dir: /server + peddlers-of-ketran-dev: + profiles: [dev] + container_name: ketr.ketran.dev + hostname: peddlers-server + build: + context: . + dockerfile: Dockerfile.dev + volumes: + - ./server:/server:rw + - ./db:/db:rw + command: ["sh", "-c", "cd /server && npm install --no-audit --no-fund --silent && npm rebuild sqlite3 && npm run start:dev"] + ports: + - 8930:8930 + environment: + - NODE_ENV=development + networks: + - peddlers-network peddlers-client: + profiles: [dev] container_name: ketr.client + hostname: peddlers-client build: context: . dockerfile: Dockerfile - restart: 'no' working_dir: /client volumes: - ./client:/client:rw - #- ./client:/client/node_modules:rw - tty: true - stdin_open: true + ports: + - 3001:3000 + environment: + - BROWSER=none + - HTTPS=true + - HOST=0.0.0.0 + command: ["bash", "-c", "cd /client && npm install --legacy-peer-deps --silent --no-audit --no-fund && npm start"] + networks: + - peddlers-network + +networks: + peddlers-network: + driver: bridge