# Mindavio VServer Setup

Kanonische Domain: `https://www.mindavio.de`

Server-IP fuer DNS A-Record: `45.131.108.18`

## 1. DNS

Setze diese Records:

```text
mindavio.de      A      45.131.108.18
www.mindavio.de  A      45.131.108.18
```

`www.mindavio.de` ist die Hauptdomain. `mindavio.de` leitet dauerhaft auf `www.mindavio.de` weiter.

## 2. Dateien hochladen

Diesen kompletten Ordner auf den VServer kopieren, zum Beispiel nach:

```bash
/var/www/mindavio
```

Dieser Ordner ist vollstaendig vorbereitet und enthaelt:

```text
node_modules
.next
package.json
package-lock.json
public
src
supabase
```

Nicht enthalten sind lokale Secrets wie `.env.local`, `.env` und Git-Daten.

## 3. Environment anlegen

Auf dem Server:

```bash
cp .env.production.example .env.local
```

Dann `.env.local` mit echten Werten ausfuellen. Wichtig:

```bash
NEXT_PUBLIC_APP_URL=https://www.mindavio.de
NEXT_PUBLIC_SUPABASE_URL=
NEXT_PUBLIC_SUPABASE_ANON_KEY=
SUPABASE_SERVICE_ROLE_KEY=
DATABASE_URL=
POSTGRES_SSL=true
AI_SETTINGS_SECRET=
INVITE_FROM_EMAIL=noreply@mindavio.de
```

Wenn Gmail/Google genutzt wird:

```bash
GOOGLE_CLIENT_ID=
GOOGLE_CLIENT_SECRET=
GOOGLE_CALENDAR_REDIRECT_URI=https://www.mindavio.de/api/integrations/google-calendar/callback
GOOGLE_GMAIL_REDIRECT_URI=https://www.mindavio.de/api/integrations/gmail/callback
GOOGLE_DRIVE_REDIRECT_URI=https://www.mindavio.de/api/integrations/google-drive/callback
```

## 4. Supabase einstellen

In Supabase Auth muessen die URLs auf die echte Domain zeigen:

```text
Site URL: https://www.mindavio.de
Redirect URLs:
https://www.mindavio.de/auth/callback
https://www.mindavio.de/reset-password
https://www.mindavio.de/**
```

Die Migrationen im Ordner `supabase/migrations` muessen angewendet sein.

## 5. Google OAuth einstellen

In Google Cloud Console diese Authorized redirect URIs setzen:

```text
https://www.mindavio.de/api/integrations/google-calendar/callback
https://www.mindavio.de/api/integrations/gmail/callback
https://www.mindavio.de/api/integrations/google-drive/callback
```

## 6. Start mit PM2

Da `node_modules` und `.next` bereits enthalten sind, reicht auf dem Server:

```bash
npm install -g pm2
pm2 start npm --name mindavio -- start
pm2 save
pm2 startup
```

Falls du nach dem Upload auf dem Server doch neu bauen willst:

```bash
npm run build
pm2 restart mindavio
```

## 7. Nginx Proxy

`www.mindavio.de` zeigt auf die Next-App. Die Domain ohne `www` wird auf die kanonische Domain weitergeleitet:

```nginx
server {
  server_name mindavio.de;
  return 301 https://www.mindavio.de$request_uri;
}

server {
  server_name www.mindavio.de;

  location / {
    proxy_pass http://45.131.108.18:3000;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host www.mindavio.de;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https;
  }
}
```

Danach SSL aktivieren:

```bash
certbot --nginx -d www.mindavio.de -d mindavio.de
```

## 8. Update spaeter

Neue vollstaendige Version hochladen, dann:

```bash
pm2 restart mindavio
```
