Backend: Added docs & AGENTS.md
This commit is contained in:
1 parent
b3f2097b8b
commit
b3ab69cc22
8 files changed
+3082
No files matched your search
@@ -0,0 +1,440 @@
|
||||
# Deployment Guide
|
||||
|
||||
> Showy backend supports three deployment modes: **Docker** (recommended), **standalone Node.js**, and **Electron-embedded**.
|
||||
|
||||
---
|
||||
|
||||
## Table of Contents
|
||||
|
||||
1. [Prerequisites](#1-prerequisites)
|
||||
2. [Docker Deployment (Recommended)](#2-docker-deployment-recommended)
|
||||
3. [Standalone Node.js](#3-standalone-nodejs)
|
||||
4. [Electron Desktop App](#4-electron-desktop-app)
|
||||
5. [Building a Portable Executable](#5-building-a-portable-executable)
|
||||
6. [Environment Configuration](#6-environment-configuration)
|
||||
7. [Database Setup](#7-database-setup)
|
||||
8. [Reverse Proxy (nginx)](#8-reverse-proxy-nginx)
|
||||
9. [Health Checks & Monitoring](#9-health-checks--monitoring)
|
||||
10. [Backup & Restore](#10-backup--restore)
|
||||
11. [Troubleshooting](#11-troubleshooting)
|
||||
|
||||
---
|
||||
|
||||
## 1. Prerequisites
|
||||
|
||||
| Requirement | Docker mode | Standalone mode | Electron mode |
|
||||
| ----------------------- | ------------------- | --------------- | -------------- |
|
||||
| Docker + Docker Compose | Yes | — | — |
|
||||
| Node.js 18+ | — | Yes | Yes |
|
||||
| MySQL 8.0 | Provided by compose | Manual install | Manual install |
|
||||
| pnpm / npm | — | Yes | Yes |
|
||||
| TMDB API key | Yes | Yes | Yes |
|
||||
|
||||
---
|
||||
|
||||
## 2. Docker Deployment (Recommended)
|
||||
|
||||
The quickest way to run the full stack (frontend + backend + MySQL).
|
||||
|
||||
### 2.1 Clone and configure
|
||||
|
||||
```bash
|
||||
git clone https://github.com/X4Lo/showy.git
|
||||
cd showy
|
||||
```
|
||||
|
||||
### 2.2 Configure `settings.json`
|
||||
|
||||
```bash
|
||||
cp backend/settings.json.example backend/settings.json
|
||||
```
|
||||
|
||||
Edit `backend/settings.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"userDataPath": "/app/data",
|
||||
"tmdbApiUrl": "https://api.themoviedb.org/3/",
|
||||
"tmdbApiKey": "YOUR_TMDB_API_KEY",
|
||||
"backPort": 4000
|
||||
}
|
||||
```
|
||||
|
||||
> In Docker, set `userDataPath` to `/app/data` so the container stores images inside a mountable volume.
|
||||
|
||||
### 2.3 Build and start
|
||||
|
||||
```bash
|
||||
docker-compose up --build -d
|
||||
```
|
||||
|
||||
This starts three containers:
|
||||
|
||||
| Service | Port | Description |
|
||||
| ---------- | ------ | ---------------------------- |
|
||||
| `frontend` | `3000` | Angular app served via nginx |
|
||||
| `backend` | `4000` | Express API server |
|
||||
| `mysql` | `3306` | MySQL 8.0 database |
|
||||
|
||||
### 2.4 Verify
|
||||
|
||||
```bash
|
||||
curl http://localhost:4000/api/v1/ping
|
||||
# → { "message": "pong" }
|
||||
```
|
||||
|
||||
### 2.5 Persist data with volumes
|
||||
|
||||
Add a volume mount to the backend service in `docker-compose.yml` to persist images across container restarts:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
volumes:
|
||||
- ./showy-data:/app/data # persistent media storage
|
||||
- ./backend/settings.json:/app/settings.json # config
|
||||
```
|
||||
|
||||
### 2.6 Stop
|
||||
|
||||
```bash
|
||||
docker-compose down # stop containers
|
||||
docker-compose down -v # stop + remove volumes (deletes DB data!)
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Standalone Node.js
|
||||
|
||||
Run the backend directly on the host machine.
|
||||
|
||||
### 3.1 Install MySQL
|
||||
|
||||
Install MySQL 8.0 and create the database:
|
||||
|
||||
```sql
|
||||
CREATE DATABASE Showzy;
|
||||
```
|
||||
|
||||
### 3.2 Install dependencies
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
pnpm install
|
||||
```
|
||||
|
||||
### 3.3 Configure
|
||||
|
||||
```bash
|
||||
cp settings.json.example settings.json
|
||||
```
|
||||
|
||||
Edit `settings.json` with your local paths and TMDB key.
|
||||
|
||||
If your MySQL credentials differ from the defaults (`root` / `password` / `localhost`), update `config/database.js`:
|
||||
|
||||
```js
|
||||
const sequelize = new Sequelize('Showzy', 'YOUR_USER', 'YOUR_PASSWORD', {
|
||||
host: 'YOUR_HOST',
|
||||
dialect: 'mysql',
|
||||
});
|
||||
```
|
||||
|
||||
### 3.4 Start (development)
|
||||
|
||||
```bash
|
||||
pnpm run backend
|
||||
# Runs: nodemon server.js --trace-warnings
|
||||
```
|
||||
|
||||
Nodemon watches for file changes and auto-restarts.
|
||||
|
||||
### 3.5 Start (production)
|
||||
|
||||
```bash
|
||||
node server.js
|
||||
```
|
||||
|
||||
For background execution on Linux:
|
||||
|
||||
```bash
|
||||
# Using pm2
|
||||
npm install -g pm2
|
||||
pm2 start server.js --name showy-backend
|
||||
pm2 save
|
||||
pm2 startup
|
||||
|
||||
# Using systemd (see section below)
|
||||
```
|
||||
|
||||
### 3.6 systemd service (Linux)
|
||||
|
||||
Create `/etc/systemd/system/showy-backend.service`:
|
||||
|
||||
```ini
|
||||
[Unit]
|
||||
Description=Showy Backend API
|
||||
After=network.target mysql.service
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User=showy
|
||||
WorkingDirectory=/opt/showy/backend
|
||||
ExecStart=/usr/bin/node server.js
|
||||
Restart=on-failure
|
||||
RestartSec=5
|
||||
Environment=NODE_ENV=production
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
```
|
||||
|
||||
```bash
|
||||
sudo systemctl daemon-reload
|
||||
sudo systemctl enable showy-backend
|
||||
sudo systemctl start showy-backend
|
||||
sudo systemctl status showy-backend
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Electron Desktop App
|
||||
|
||||
The backend can be embedded inside an Electron app for a fully self-contained desktop experience.
|
||||
|
||||
### 4.1 How it works
|
||||
|
||||
1. Electron's `main.js` spawns the backend as a child process:
|
||||
```js
|
||||
spawn('node', ['backend/server.js'], {
|
||||
env: { PARENT_PROCESS: JSON.stringify({ electron_path: app.getPath('userData') }) }
|
||||
});
|
||||
```
|
||||
2. The backend detects the `PARENT_PROCESS` env var and knows it's running inside Electron.
|
||||
3. The frontend is loaded from local HTML files (`frontend_build/browser/index.html`).
|
||||
4. When Electron quits, it kills the backend child process.
|
||||
5. If the backend crashes, Electron **auto-restarts** it.
|
||||
|
||||
### 4.2 Build steps
|
||||
|
||||
```bash
|
||||
# 1. Build the frontend
|
||||
cd frontend
|
||||
pnpm run build
|
||||
|
||||
# 2. Copy build output to electron folder
|
||||
cp -r dist/showy/browser electron/frontend_build/browser
|
||||
|
||||
# 3. Install electron dependencies
|
||||
cd ../electron
|
||||
pnpm install
|
||||
|
||||
# 4. Run in dev mode
|
||||
npx electron .
|
||||
|
||||
# 5. Package for distribution (using electron-builder or similar)
|
||||
npx electron-builder
|
||||
```
|
||||
|
||||
### 4.3 Backend lifecycle in Electron
|
||||
|
||||
| Event | Action |
|
||||
| -------------------------------------------- | ----------------------------------------------------------------------- |
|
||||
| `app.on('ready')` | Starts Express + creates browser window |
|
||||
| Backend `stdout` contains `"Server started"` | Sends `backend-ready` event to renderer |
|
||||
| Backend crashes (`exit` event) | Auto-restart via `startExpressServer()` |
|
||||
| `app.on('before-quit')` | `backendExpressProcess.kill()` |
|
||||
| `GET/POST /killer` | Intentionally crashes backend (triggers restart loop or clean shutdown) |
|
||||
|
||||
---
|
||||
|
||||
## 5. Building a Portable Executable
|
||||
|
||||
The backend can be packaged into a standalone `.exe` using `pkg`:
|
||||
|
||||
```bash
|
||||
cd backend
|
||||
pnpm run package
|
||||
# Outputs: ../dist/backend/backend.exe (Windows x64, Node 18)
|
||||
```
|
||||
|
||||
This produces a single binary that includes Node.js and all dependencies. No Node.js installation required on the target machine — only MySQL must be available.
|
||||
|
||||
### pkg configuration (from `package.json`)
|
||||
|
||||
```json
|
||||
{
|
||||
"pkg": {
|
||||
"scripts": ["node_modules/**/*"],
|
||||
"assets": ["node_modules/**/*"],
|
||||
"targets": ["node18-win-x64"],
|
||||
"outputPath": "../dist/backend/backend.exe"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Environment Configuration
|
||||
|
||||
### `settings.json` (primary)
|
||||
|
||||
All runtime configuration is stored in `settings.json` at the backend root. See [Project Setup](project_setup.md) for the full reference.
|
||||
|
||||
### Environment variables (Docker only)
|
||||
|
||||
| Variable | Default | Description |
|
||||
| ---------------- | ----------- | ------------------------------------------ |
|
||||
| `MYSQL_HOST` | `localhost` | MySQL hostname |
|
||||
| `MYSQL_DB` | `Showzy` | Database name |
|
||||
| `MYSQL_USER` | `root` | Database user |
|
||||
| `MYSQL_PASSWORD` | `password` | Database password |
|
||||
| `PARENT_PROCESS` | `{}` | Set by Electron; JSON with `electron_path` |
|
||||
|
||||
> **Important:** In the current codebase, `config/database.js` has hardcoded credentials. For Docker, these env vars are passed but must be read manually (or update `database.js` to use `process.env`).
|
||||
|
||||
---
|
||||
|
||||
## 7. Database Setup
|
||||
|
||||
### Schema management
|
||||
|
||||
Sequelize auto-manages the schema at startup:
|
||||
|
||||
```js
|
||||
// In server.js
|
||||
sequelize.sync({ alter: doUpdate }) // doUpdate = false by default
|
||||
```
|
||||
|
||||
- **First run:** All tables are created automatically.
|
||||
- **Schema changes:** Set `doUpdate = true` in `server.js` to apply `ALTER TABLE` statements.
|
||||
- **Full reset:** Set `doForce = true` to drop and recreate all tables (destroys data).
|
||||
|
||||
### Data seeding
|
||||
|
||||
On startup, `dataInitializer.js` seeds **27 TMDB genre records** if they're missing. No other seeding is required.
|
||||
|
||||
### Recommended MySQL settings
|
||||
|
||||
```ini
|
||||
[mysqld]
|
||||
character-set-server = utf8mb4
|
||||
collation-server = utf8mb4_unicode_ci
|
||||
max_connections = 100
|
||||
innodb_buffer_pool_size = 256M
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Reverse Proxy (nginx)
|
||||
|
||||
For production deployments behind a reverse proxy:
|
||||
|
||||
```nginx
|
||||
server {
|
||||
listen 80;
|
||||
server_name showy.example.com;
|
||||
|
||||
# Frontend
|
||||
location / {
|
||||
proxy_pass http://localhost:3000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
}
|
||||
|
||||
# Backend API
|
||||
location /api/ {
|
||||
proxy_pass http://localhost:4000;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
client_max_body_size 20M; # for media uploads
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For HTTPS, add a TLS certificate via Let's Encrypt / Certbot.
|
||||
|
||||
---
|
||||
|
||||
## 9. Health Checks & Monitoring
|
||||
|
||||
### Health endpoint
|
||||
|
||||
```bash
|
||||
GET /api/v1/ping → { "message": "pong" }
|
||||
```
|
||||
|
||||
### Docker health check
|
||||
|
||||
Add to `docker-compose.yml`:
|
||||
|
||||
```yaml
|
||||
services:
|
||||
backend:
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://localhost:4000/api/v1/ping"]
|
||||
interval: 30s
|
||||
timeout: 5s
|
||||
retries: 3
|
||||
start_period: 10s
|
||||
```
|
||||
|
||||
### Log file
|
||||
|
||||
- **Location:** `backend/showy.log`
|
||||
- **Format:** `[YYYY-MM-DD HH:mm:ss] message`
|
||||
- **Contains:** TMDB fetch logs, media download/preview events, error traces
|
||||
|
||||
Monitor in real time:
|
||||
|
||||
```bash
|
||||
tail -f backend/showy.log
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. Backup & Restore
|
||||
|
||||
### What to back up
|
||||
|
||||
| Component | Location | Method |
|
||||
| ------------- | ------------------------ | ----------------- |
|
||||
| Database | MySQL `Showzy` | `mysqldump` |
|
||||
| Media files | `{userDataPath}/images/` | File copy / rsync |
|
||||
| Configuration | `backend/settings.json` | File copy |
|
||||
|
||||
### MySQL backup
|
||||
|
||||
```bash
|
||||
# Backup
|
||||
mysqldump -u root -p Showzy > showzy_backup_$(date +%F).sql
|
||||
|
||||
# Restore
|
||||
mysql -u root -p Showzy < showzy_backup_2026-03-30.sql
|
||||
```
|
||||
|
||||
### Media backup
|
||||
|
||||
```bash
|
||||
# Backup (rsync)
|
||||
rsync -av /path/to/showy-data/images/ /backup/showy-images/
|
||||
|
||||
# Or tar archive
|
||||
tar czf showy-media-$(date +%F).tar.gz -C /path/to/showy-data images/
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 11. Troubleshooting
|
||||
|
||||
| Problem | Cause | Fix |
|
||||
| ---------------------------- | -------------------------------------- | -------------------------------------------------------------------------------- |
|
||||
| `ECONNREFUSED` on startup | MySQL not running or wrong credentials | Check MySQL is up; verify `config/database.js` |
|
||||
| `ER_NOT_SUPPORTED_AUTH_MODE` | MySQL 8 auth plugin mismatch | Run `ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password';` |
|
||||
| Port 4000 already in use | Another process on that port | Change `backPort` in `settings.json` or kill the other process |
|
||||
| Images not loading | `userDataPath` misconfigured | Verify the path exists and is writable; check `settings.json` |
|
||||
| Previews not generating | `sharp` native bindings missing | Run `pnpm rebuild sharp` or reinstall with `pnpm install` |
|
||||
| TMDB fetch returns `null` | Invalid API key or rate limited | Verify `tmdbApiKey` in `settings.json`; check TMDB account status |
|
||||
| Schema out of sync | New model fields added | Set `doUpdate = true` in `server.js`, restart, then set back to `false` |
|
||||
| Docker MySQL won't start | Volume permission issues | Run `docker-compose down -v` then `docker-compose up --build` |
|
||||
| Sequelize sync error | Table conflicts on `alter` | Backup DB, set `doForce = true` temporarily (destroys data), restart |
|
||||
Reference in new issue
Block a user