# Deploying MultiMirror to Railway.app

Railway is the recommended free-tier host for MultiMirror because:
- ✅ Persistent volume (SQLite database survives redeploys)
- ✅ Long-running processes (no 10s timeout like Vercel)
- ✅ Dockerfile support (full control over runtime)
- ✅ Free tier with $5 monthly credit (enough for personal use)

## Step 1: Push your code to GitHub

If you haven't already, push this project to a GitHub repo:

```bash
git init
git add .
git commit -m "MultiMirror prototype"
git branch -M main
git remote add origin https://github.com/YOUR_USERNAME/multimirror.git
git push -u origin main
```

## Step 2: Sign up for Railway

1. Go to https://railway.app
2. Click "Login" → "Sign in with GitHub"
3. Authorize Railway to access your GitHub account

## Step 3: Create a new project from your GitHub repo

1. Click **"New Project"** in Railway
2. Select **"Deploy from GitHub repo"**
3. Choose your `multimirror` repo
4. Railway will detect the Dockerfile and start building automatically

## Step 4: Add a persistent volume (for SQLite database)

1. In your Railway project, click the **service** (e.g. `multimirror`)
2. Go to the **"Settings"** tab
3. Scroll down to **"Volumes"**
4. Click **"Add Volume"**
5. Set:
   - Mount path: `/data`
   - (Optional) Name: `multimirror-data`
6. Click **"Add"**

This ensures your SQLite database (`/data/multimirror.db`) persists across redeploys.

## Step 5: Set environment variables

In the same service, go to the **"Variables"** tab and add:

| Variable | Value | Required |
|----------|-------|----------|
| `DATABASE_URL` | `file:/data/multimirror.db` | ✅ Yes |
| `NODE_ENV` | `production` | ✅ Yes |
| `PORT` | (Railway sets this automatically) | ❌ No |
| `CRED_SECRET` | (any random string — for credential obfuscation) | ⚠️ Recommended |

To generate a random `CRED_SECRET`, run this locally:
```bash
openssl rand -hex 32
```

## Step 6: Wait for the first deploy

Railway will:
1. Pull the Dockerfile
2. Install dependencies with Bun
3. Build the Next.js standalone output
4. Run `prisma db push` to create the SQLite schema
5. Start `node server.js`

The first build takes ~3-5 minutes. Watch the **"Deployments"** tab for progress.

## Step 7: Get your public URL

1. Go to **"Settings"** → **"Networking"**
2. Click **"Generate Domain"**
3. Railway gives you a URL like `multimirror-production.up.railway.app`
4. Open it in your browser — your MultiMirror is live!

## Step 8: Re-enter your API credentials

Your credentials from the sandbox don't carry over (different database). Open your new site and:
1. Click **"API credentials"**
2. Re-enter your 1Fichier, ZeroStorage, GoFile, PixelDrain, Archive.org, and Buzzheavier keys
3. Click Save for each

## Useful Railway commands

- **View logs**: Click your service → "Logs" tab (real-time server logs)
- **Restart service**: Settings → "Redeploy"
- **Check resource usage**: Settings → "Metrics"
- **Set custom domain**: Settings → "Networking" → "Custom Domain"

## Free tier limits

Railway's free tier gives you:
- $5 of usage per month (~500 hours of small service)
- 512MB RAM
- 1GB persistent volume (enough for thousands of upload history entries)

If you exceed the limit, the service pauses until the next month. For personal use, this is plenty.

## Troubleshooting

### Build fails
Check the build logs in Railway. Common issues:
- Missing `bun.lock` — run `bun install` locally and commit the lockfile
- Prisma generate fails — make sure `prisma/schema.prisma` is committed

### Database errors
- Verify `DATABASE_URL=file:/data/multimirror.db` is set
- Verify the volume is mounted at `/data`
- Check logs for `prisma db push` output

### Uploads timeout
Railway's default timeout is 300s (5 min). For large video uploads, you may need to upgrade to a paid plan for longer timeouts. The app is configured with `maxDuration = 600` (10 min) but Railway may override.

### Out of memory
The app streams remote uploads through a temp file (not memory), so this shouldn't happen. If it does:
- Lower `MAX_CONCURRENT_FILES` in `src/store/upload-store.ts` to 1 (already set)
- Upgrade Railway plan for more RAM

## Alternative: Deploy to Render.com

Render has a similar free tier and works with the same Dockerfile:

1. Go to https://render.com
2. Create a new "Web Service"
3. Connect your GitHub repo
4. Render auto-detects the Dockerfile
5. Add a persistent disk at `/data` (size: 1GB)
6. Set environment variables (same as Railway)
7. Deploy

## Alternative: Deploy to Fly.io

Fly.io has a generous free tier and global edge:

1. Install `flyctl`: `curl -L https://fly.io/install.sh | sh`
2. Run `fly launch` in the project directory
3. Fly creates a `fly.toml` automatically
4. Add a volume: `fly volumes create multimirror_data --size 1`
5. Set secrets: `fly secrets set DATABASE_URL=file:/data/multimirror.db`
6. Deploy: `fly deploy`
7. Open: `fly apps open`

---

That's it! Your MultiMirror is now live on the internet with persistent storage and no arbitrary timeout limits. 🚀
