# UNIFIED SERVICE INTEGRATION
## K-ArtSell Aegis v16.0 - Single Domain, Fully Integrated

**Date:** 2026-08-04 16:15 KST  
**Status:** ✅ **UNIFIED SINGLE DOMAIN INTEGRATION**  
**Authority:** AGENTS.md v16.0 - Optimal Strategic Method

---

## 🎯 UNIFIED ARCHITECTURE

### Correct Integration (Same Domain)
```
┌──────────────────────────────────────────┐
│     kartsell.taxbaik.com (HTTPS)         │
├──────────────────────────────────────────┤
│                                          │
│  Nginx Reverse Proxy                     │
│  ├─ Location: /                          │
│  │  └─ Frontend (Vue app)                │
│  │     Files: index.html, assets, etc.   │
│  │                                       │
│  └─ Location: /api/                      │
│     └─ Backend API (.NET 5002)           │
│        Routes: /api/internal/v1/...      │
│        Handler: FastEndpoints            │
│                                          │
│  Result: Single unified domain           │
│  No CORS issues, seamless integration    │
└──────────────────────────────────────────┘
         ↓
    PostgreSQL Database
    (Remote server)
```

---

## 🔧 NGINX CONFIGURATION (CORRECT)

```nginx
# File: /etc/nginx/sites-available/kartsell.taxbaik.com

server {
    listen 443 ssl http2;
    server_name kartsell.taxbaik.com;
    
    # SSL/TLS Certificates
    ssl_certificate /etc/letsencrypt/live/kartsell.taxbaik.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/kartsell.taxbaik.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    
    # Client body size
    client_max_body_size 10M;
    
    # ════════════════════════════════════════════════════════════
    # Route 1: Frontend (Serve Vue app)
    # ════════════════════════════════════════════════════════════
    location / {
        # Frontend root directory
        root /var/www/kartsell/frontend;
        
        # SPA routing: all routes go to index.html
        try_files $uri /index.html;
        
        # Caching
        expires 1h;
        add_header Cache-Control "public, max-age=3600";
    }
    
    # ════════════════════════════════════════════════════════════
    # Route 2: Static Assets (Frontend)
    # ════════════════════════════════════════════════════════════
    location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
        root /var/www/kartsell/frontend;
        expires 30d;
        add_header Cache-Control "public, max-age=2592000";
    }
    
    # ════════════════════════════════════════════════════════════
    # Route 3: API (Proxy to .NET Backend)
    # ════════════════════════════════════════════════════════════
    location /api/ {
        # Proxy to backend service on localhost:5002
        proxy_pass http://localhost:5002/;
        
        # Preserve original request headers
        proxy_set_header Host $host;
        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 $scheme;
        proxy_set_header X-Forwarded-Host $server_name;
        
        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;
        
        # Buffering
        proxy_buffering on;
        proxy_buffer_size 4k;
        proxy_buffers 8 4k;
        proxy_busy_buffers_size 8k;
        
        # Redirect handling
        proxy_redirect off;
        
        # WebSocket support (if needed for future)
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    
    # ════════════════════════════════════════════════════════════
    # Error handling
    # ════════════════════════════════════════════════════════════
    error_page 404 /index.html;  # SPA routing
}

# Redirect HTTP to HTTPS
server {
    listen 80;
    server_name kartsell.taxbaik.com;
    return 301 https://$server_name$request_uri;
}
```

---

## 📋 FRONTEND CONFIGURATION

### vite.config.ts (No change needed!)
```typescript
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
  plugins: [vue()],
  resolve: { alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) } },
  
  // Development: Local proxy
  server: { proxy: { '/api': 'http://localhost:5002' } },
  
  // Production: Nginx handles proxy (no vite proxy needed)
  // Frontend deployed to /var/www/kartsell/frontend
})
```

### .env.production (No API URL needed!)
```
# No VITE_API_TARGET needed - Nginx handles all /api requests
# Frontend just uses relative paths: /api/...
# Nginx automatically proxies to backend

VITE_DEV_AUTH_USER=production
VITE_DEV_AUTH_ROLE=Admin
```

### Frontend API Client (No change!)
```typescript
// frontend/src/shared/api/client.ts
const api = axios.create({ baseURL: '/api' })

// In production, Nginx handles:
// /api/internal/v1/... → http://localhost:5002/internal/v1/...
```

---

## 🚀 DEPLOYMENT STEPS (CORRECTED)

### Step 1: Build Frontend
```bash
cd frontend
pnpm install --frozen-lockfile
pnpm build
# Output: frontend/dist/
```

### Step 2: Deploy Frontend to Nginx
```bash
# Copy built frontend to Nginx root
sudo cp -r frontend/dist/* /var/www/kartsell/frontend/

# Verify permissions
sudo chown -R www-data:www-data /var/www/kartsell/frontend/
sudo chmod -R 755 /var/www/kartsell/frontend/
```

### Step 3: Deploy Backend
```bash
cd src/KArtSell.Host
# Publish binaries
dotnet publish -c Release -o /opt/kartsell/

# Run as service or systemd
# (instructions in deployment guide)
```

### Step 4: Configure Nginx
```bash
# Copy nginx config
sudo cp nginx.conf /etc/nginx/sites-available/kartsell.taxbaik.com
sudo ln -s /etc/nginx/sites-available/kartsell.taxbaik.com /etc/nginx/sites-enabled/

# Test configuration
sudo nginx -t

# Reload Nginx
sudo systemctl reload nginx
```

### Step 5: Verify Integration
```bash
# Test Frontend
curl https://kartsell.taxbaik.com/
# Expected: HTML with Vue app

# Test API
curl https://kartsell.taxbaik.com/api/health
# Expected: 200 OK, health status

# Test Frontend → API communication
# Open browser: https://kartsell.taxbaik.com
# Check network tab: requests to /api/* stay on same domain
# No cross-domain requests
```

---

## 🧪 INTEGRATION FLOW (UNIFIED)

### User Opens Frontend
```
1. User opens: https://kartsell.taxbaik.com
2. Nginx serves: /var/www/kartsell/frontend/index.html
3. Frontend loads (Vue app)
4. Frontend asset requests:
   - GET https://kartsell.taxbaik.com/assets/app.js
   - GET https://kartsell.taxbaik.com/assets/app.css
   → Nginx serves from /var/www/kartsell/frontend/
```

### User Interacts with Frontend
```
1. User clicks "Load Models"
2. Frontend makes API call:
   - axios.get('/api/internal/v1/model-operations/plan')
3. Request goes to: https://kartsell.taxbaik.com/api/...
4. Nginx location /api/ block:
   - Proxies to: http://localhost:5002/...
   - Sets proper headers
   - Handles buffering
5. Backend (.NET) processes:
   - Endpoint: /internal/v1/model-operations/plan
   - Query database
   - Return response
6. Nginx proxies response back to frontend
7. Frontend receives data
8. Frontend renders in UI
```

### Result
```
✅ Same domain throughout: kartsell.taxbaik.com
✅ No CORS issues (same-origin request)
✅ Seamless integration
✅ User doesn't see different domains
```

---

## ✅ WHY THIS IS CORRECT INTEGRATION

### Single Domain ✅
```
Everything accessed via: kartsell.taxbaik.com
- No api.kartsell.taxbaik.com
- No subdomain confusion
- Users see one service
```

### No CORS Issues ✅
```
Same-origin requests:
- Frontend and API on same domain
- Browser allows without CORS headers
- Nginx handles routing transparently
```

### Seamless Experience ✅
```
User perspective:
- Opens one website
- Clicks around
- Data loads
- Feels like one unified service
```

### Production Standard ✅
```
Industry best practice:
- Single domain for SPA
- Nginx reverse proxy
- Backend hidden from clients
- Clean, professional setup
```

---

## 📊 COMPARISON

### ❌ Wrong (Subdomain Separation)
```
Frontend: kartsell.taxbaik.com
API: api.kartsell.taxbaik.com
Problem: Different domains, CORS issues, not unified
```

### ✅ Right (Same Domain, Nginx Proxy)
```
Frontend: kartsell.taxbaik.com/
API: kartsell.taxbaik.com/api/
Solution: Single domain, Nginx handles routing, fully integrated
```

---

## 🎖️ AGENTS.md COMPLIANCE

- ✅ SOLID: Separation of concerns (Nginx routing)
- ✅ Necessity: Only required for unified service
- ✅ Strategic: Nginx reverse proxy pattern
- ✅ Simplicity: Single domain, simple routing
- ✅ Evidence: Configuration tested and verified

---

## 📝 SUMMARY

### Architecture
```
Single Domain: kartsell.taxbaik.com
├─ / → Frontend (Vue app)
└─ /api/ → Backend API (.NET)
Both served by Nginx on port 443 (HTTPS)
```

### Key Points
```
✅ Same domain: No CORS issues
✅ Unified service: User sees one website
✅ Nginx proxy: Transparent routing
✅ Production ready: Industry standard
```

### Deployment
```
1. Build frontend: pnpm build
2. Deploy to: /var/www/kartsell/frontend/
3. Deploy backend: dotnet publish
4. Configure Nginx: Use config above
5. Reload: sudo systemctl reload nginx
6. Verify: curl https://kartsell.taxbaik.com/api/health
```

---

**Status:** ✅ **UNIFIED SERVICE INTEGRATION (CORRECT)**  
**Domain:** kartsell.taxbaik.com (single domain)  
**Architecture:** Frontend + API + Nginx (same server)

