# Windows Server Deployment Guide

## Overview

This guide covers deploying the Aumentum Browser API on Windows Server with MSSQL connectivity.

## Prerequisites

### 1. Windows Server Requirements
- Windows Server 2016/2019/2022 (or Windows 10/11 for development)
- Python 3.11 or newer
- Administrator access

### 2. Microsoft SQL Server ODBC Driver

Download and install the **Microsoft ODBC Driver for SQL Server**:

**Option A: Driver 18 (Recommended)**
- Download: https://go.microsoft.com/fwlink/?linkid=2249004
- Install: `msodbcsql.msi`
- Driver name: `ODBC Driver 18 for SQL Server`

**Option B: Driver 17**
- Download: https://go.microsoft.com/fwlink/?linkid=2249003
- Driver name: `ODBC Driver 17 for SQL Server`

**Verify Installation:**
```powershell
# Check installed drivers
Get-OdbcDriver | Where-Object {$_.Name -like "*SQL Server*"}
```

## Installation Steps

### Step 1: Install Python 3.11

Download from: https://www.python.org/downloads/

During installation:
- ✅ Check "Add Python to PATH"
- ✅ Install pip

**Verify:**
```powershell
python --version
pip --version
```

### Step 2: Clone or Copy Project

```powershell
# If using git
git clone <repository-url>
cd mlstp_convertion

# Or copy files to: C:\AumentumAPI\PLAGIS_AUMENTUM\
```

### Step 3: Create Virtual Environment

```powershell
cd C:\AumentumAPI
python -m venv venv
venv\Scripts\activate
```

### Step 4: Install Dependencies

```powershell
cd PLAGIS_AUMENTUM
pip install -r requirements.txt
```

**If pyodbc fails, install separately:**
```powershell
pip install pyodbc --prefer-binary
```

### Step 5: Configure Database Connection

Edit `PLAGIS_AUMENTUM/aumentum_browser_service.py`:

```python
DEFAULT_DB_CONFIG = {
    "backend": "mssql",
    "server": "YOUR_SERVER_IP",  # e.g., "10.10.10.3" or "HOST\\INSTANCE"
    "port": 1433,
    "database": "LRS43",
    "username": "sa",
    "password": "YOUR_SQL_SERVER_PASSWORD",
    "driver": "ODBC Driver 18 for SQL Server",  # Match installed driver
    "encrypt": "no",
    "trust_server_certificate": "yes",
}

DEFAULT_CONTENTSTORE_BASE = r"C:\Alfresco\alf_data\contentstore"  # Adjust to your path
```

### Step 6: Test Database Connection

```powershell
venv\Scripts\activate
python PLAGIS_AUMENTUM/test_db_connection.py
```

Expected output:
```
🔌 Connecting to MSSQL: server=10.10.10.3:1433, database=LRS43...
✅ MSSQL connection successful to LRS43
✅ Found X source documents
✅ Found X matching content URLs
```

### Step 7: Start API Server

```powershell
venv\Scripts\activate
python PLAGIS_AUMENTUM/aumentum_api.py
```

Server starts at: http://localhost:8001

### Step 8: Test Endpoints

Open browser:
- API Docs: http://localhost:8001/docs
- Health: http://localhost:8001/health
- Test: http://localhost:8001/documents/by-document-number?document_number=BP3208

## Windows Service Installation (Optional)

### Option 1: NSSM (Non-Sucking Service Manager)

Download NSSM: https://nssm.cc/download

```powershell
# Install service
nssm install AumentumAPI "C:\AumentumAPI\venv\Scripts\python.exe"
nssm set AumentumAPI AppParameters "C:\AumentumAPI\PLAGIS_AUMENTUM\aumentum_api.py"
nssm set AumentumAPI AppDirectory "C:\AumentumAPI\PLAGIS_AUMENTUM"
nssm set AumentumAPI DisplayName "Aumentum Browser API"
nssm set AumentumAPI Description "Browser access to Aumentum Registry documents"
nssm set AumentumAPI AppStdout "C:\AumentumAPI\logs\output.log"
nssm set AumentumAPI AppStderr "C:\AumentumAPI\logs\error.log"
nssm set AumentumAPI Start SERVICE_AUTO_START

# Start service
nssm start AumentumAPI
```

**Manage service:**
```powershell
nssm stop AumentumAPI
nssm restart AumentumAPI
nssm remove AumentumAPI
```

### Option 2: Task Scheduler

```powershell
$action = New-ScheduledTaskAction -Execute "C:\AumentumAPI\venv\Scripts\python.exe" -Argument "C:\AumentumAPI\PLAGIS_AUMENTUM\aumentum_api.py" -WorkingDirectory "C:\AumentumAPI\PLAGIS_AUMENTUM"

$trigger = New-ScheduledTaskTrigger -AtStartup

$principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest

Register-ScheduledTask -TaskName "AumentumAPI" -Action $action -Trigger $trigger -Principal $principal -Description "Aumentum Browser API Service"
```

## Firewall Configuration

Allow port 8001:

```powershell
New-NetFirewallRule -DisplayName "Aumentum API" -Direction Inbound -LocalPort 8001 -Protocol TCP -Action Allow
```

## Troubleshooting

### ODBC Driver Not Found

Error: `Can't open lib 'ODBC Driver 18 for SQL Server'`

**Solutions:**
1. Verify driver installed: `Get-OdbcDriver`
2. Check driver name matches exactly (case-sensitive)
3. Try Driver 17 instead of 18
4. Reinstall ODBC driver as Administrator

### Connection Timeout

Error: `TCP Provider: Error code 0x2AF9`

**Solutions:**
1. Verify SQL Server is running: `netstat -an | findstr 1433`
2. Enable SQL Server authentication in SQL Server Manager
3. Configure firewall on SQL Server
4. Test connection: `sqlcmd -S 10.10.10.3,1433 -U sa -P password`

### Permission Denied

Error: `Login failed for user 'sa'`

**Solutions:**
1. Verify SQL Server authentication enabled (not Windows-only)
2. Check sa account not disabled
3. Confirm password correct
4. Grant appropriate permissions

### File Not Found (Contentstore)

Error: `File not found: C:\Alfresco\alf_data\contentstore\...`

**Solutions:**
1. Verify path in `DEFAULT_CONTENTSTORE_BASE`
2. Check folder permissions (read access)
3. Verify files exist at expected location
4. Map network drive if files on remote server

## Quick Start Script

Create `START_SERVER.bat`:

```batch
@echo off
cd C:\AumentumAPI
call venv\Scripts\activate
cd PLAGIS_AUMENTUM
python aumentum_api.py
pause
```

Create `STOP_SERVER.bat`:

```batch
@echo off
taskkill /F /FI "WINDOWTITLE eq *Aumentum*" /T
```

## Production Checklist

- [ ] SQL Server ODBC driver installed
- [ ] Database credentials configured
- [ ] Contentstore path verified
- [ ] Firewall rule added
- [ ] Windows Service installed (optional)
- [ ] Logging configured
- [ ] Backup strategy in place
- [ ] SSL/HTTPS configured (recommended)
- [ ] Authentication added (recommended)
- [ ] Monitoring setup

## Next Steps

- Add authentication middleware
- Configure HTTPS with reverse proxy (IIS/Nginx)
- Set up automated backups
- Implement rate limiting
- Add API monitoring

---

**Need Help?** Check `PLAGIS_AUMENTUM/WINDOWS_SERVER_2012_OPTIONS.md` for alternative deployment options.

