# RDM RSP API - Deployment Documentation

## Overview

| Property | Value |
|----------|-------|
| Application | RDM RSP API |
| Framework | .NET 8.0 |
| Container | Docker |
| Git Repository | https://gitlab.com/blouza-tech/rdm-rsp-api.git |

---

## Environments

### Staging Server

| Property | Value |
|----------|-------|
| URL | https://rdmapi.blouzatech.ng |
| Server IP | 92.205.104.174 |
| SSH User | timothy |
| SSH Command | `ssh timothy@vmi2578493` |
| Deployment Path | `/var/www/eirs-projects/rdmapi.blouzatech.ng` |
| Container Port | 8086 |
| Database Server | 45.13.59.80 |
| Database Name | EIRS |
| Web Server | Apache |

### Production Server (DigitalOcean)

| Property | Value |
|----------|-------|
| URL | https://rdmapi.eirs.gov.ng |
| Server IP | 137.184.192.64 |
| SSH User | root |
| SSH Command | `ssh -i ~/.ssh/do_eirs_prod root@137.184.192.64` |
| Deployment Path | `/var/www/rdmapi.eirs.gov.ng` |
| Container Port | 8086 |
| Database Server | 10.116.0.2 (Private IP) |
| Database Name | EIRS |
| Web Server | Apache |

---

## Docker Commands

### Starting the Container

```bash
# Start in detached mode
docker compose up -d

# Start with rebuild
docker compose up -d --build
```

### Stopping the Container

```bash
# Stop gracefully
docker compose down

# Stop with timeout (30 seconds)
docker compose down --timeout 30

# Stop and remove volumes (WARNING: removes data)
docker compose down -v
```

### Restarting the Container

```bash
# Restart
docker compose restart

# Restart specific service
docker compose restart api
```

### Viewing Logs

```bash
# View all logs
docker compose logs

# Follow logs in real-time
docker compose logs -f

# Follow logs for api service only
docker compose logs -f api

# View last 100 lines
docker compose logs --tail=100 api

# View logs with timestamps
docker compose logs -t api
```

### Container Status

```bash
# Show running containers
docker compose ps

# Show all containers (including stopped)
docker compose ps -a

# Detailed container info
docker inspect rdm-rsp-api
```

### Accessing Container Shell

```bash
# Open bash shell in container
docker compose exec api bash

# Run a single command
docker compose exec api ls -la /app
```

### Health Check

```bash
# Check if API is responding
curl http://localhost:8086/swagger/index.html

# Check container health status
docker inspect --format='{{.State.Health.Status}}' rdm-rsp-api
```

---

## Deployment Steps

### Initial Deployment

```bash
# 1. Clone repository
cd /var/www
git clone https://gitlab.com/blouza-tech/rdm-rsp-api.git <folder-name>
cd <folder-name>

# 2. Create directories
mkdir -p ./data/logs

# 3. Configure environment
cp .env.example .env
nano .env  # Update DB_CONNECTION and other settings
chmod 600 .env

# 4. Build and start
docker compose up -d --build

# 5. Verify
docker compose ps
curl http://localhost:8086/swagger/index.html
```

### Updating Deployment

```bash
# 1. Pull latest code
cd /var/www/<folder-name>
git pull

# 2. Rebuild and restart
docker compose up -d --build

# 3. Verify
docker compose ps
docker compose logs -f api
```

### Rollback

```bash
# 1. Check git history
git log --oneline -10

# 2. Revert to previous commit
git checkout <commit-hash>

# 3. Rebuild
docker compose up -d --build
```

---

## Apache Configuration

### Staging (rdmapi.blouzatech.ng)

Location: `/etc/apache2/sites-available/rdmapi.blouzatech.ng.conf`

```apache
<VirtualHost *:80>
    ServerName rdmapi.blouzatech.ng

    ProxyPreserveHost On
    ProxyPass / http://localhost:8086/
    ProxyPassReverse / http://localhost:8086/

    ErrorLog ${APACHE_LOG_DIR}/rdmapi.blouzatech.ng-error.log
    CustomLog ${APACHE_LOG_DIR}/rdmapi.blouzatech.ng-access.log combined
</VirtualHost>
```

### Production (rdmapi.eirs.gov.ng)

Location: `/etc/apache2/sites-available/rdmapi.eirs.gov.ng.conf`

```apache
<VirtualHost *:80>
    ServerName rdmapi.eirs.gov.ng

    ProxyPreserveHost On
    ProxyPass / http://localhost:8086/
    ProxyPassReverse / http://localhost:8086/

    ErrorLog ${APACHE_LOG_DIR}/rdmapi.eirs.gov.ng-error.log
    CustomLog ${APACHE_LOG_DIR}/rdmapi.eirs.gov.ng-access.log combined
</VirtualHost>
```

### Apache Commands

```bash
# Enable site
sudo a2ensite rdmapi.eirs.gov.ng.conf

# Disable site
sudo a2dissite rdmapi.eirs.gov.ng.conf

# Test configuration
sudo apache2ctl configtest

# Reload Apache
sudo systemctl reload apache2

# Restart Apache
sudo systemctl restart apache2

# View Apache error logs
sudo tail -f /var/log/apache2/rdmapi.eirs.gov.ng-error.log
```

### SSL Certificate (Let's Encrypt)

```bash
# Get/renew certificate
sudo certbot --apache -d rdmapi.eirs.gov.ng

# Check certificate status
sudo certbot certificates

# Dry run renewal
sudo certbot renew --dry-run
```

---

## Environment Variables

### Key Variables in `.env`

| Variable | Description | Example |
|----------|-------------|---------|
| `ASPNETCORE_ENVIRONMENT` | Runtime environment | `Production` |
| `API_PORT` | Host port mapping | `8086` |
| `TZ` | Timezone | `Africa/Lagos` |
| `DB_CONNECTION` | Database connection string | See below |
| `LOGS_VOLUME_PATH` | Log directory path | `./data/logs` |

### Database Connection String Format

```
Data Source=<server-ip>;Initial Catalog=<database>;User Id=<user>;Password=<password>;MultipleActiveResultSets=True;TrustServerCertificate=True;Encrypt=True;Connection Timeout=30
```

**Staging:**
```
Data Source=45.13.59.80;Initial Catalog=EIRS;User Id=appuser;Password=xx%Dc6z$$J$$0s;...
```

**Production:**
```
Data Source=10.116.0.2;Initial Catalog=EIRS;User Id=sa;Password=Eirs@Sql2026#Pr0d!;...
```

> **Note:** Escape `$` characters with `$$` in docker-compose environment files.

---

## Troubleshooting

### Container Won't Start

```bash
# Check logs for errors
docker compose logs api

# Check if port is in use
sudo netstat -tlnp | grep 8086

# Check Docker status
docker info
```

### Database Connection Issues

```bash
# Test connectivity from container
docker compose exec api bash
curl -v telnet://10.116.0.2:1433

# Check DNS resolution
docker compose exec api nslookup 10.116.0.2
```

### Port Already in Use

```bash
# Find what's using the port
sudo lsof -i :8086

# Kill the process or change API_PORT in .env
nano .env  # Change API_PORT=8087
docker compose up -d
```

### Out of Disk Space

```bash
# Check disk usage
df -h

# Clean up Docker
docker system prune -a
docker volume prune
```

### Container Keeps Restarting

```bash
# Check logs
docker compose logs --tail=100 api

# Check container status
docker inspect rdm-rsp-api | grep -A 10 "State"

# Check resource limits
docker stats rdm-rsp-api
```

---

## Useful Commands Reference

### Quick Reference Card

| Task | Command |
|------|---------|
| Start | `docker compose up -d` |
| Stop | `docker compose down` |
| Restart | `docker compose restart` |
| Rebuild | `docker compose up -d --build` |
| Logs | `docker compose logs -f api` |
| Status | `docker compose ps` |
| Shell | `docker compose exec api bash` |
| Health | `curl http://localhost:8086/swagger/index.html` |

### SSH Quick Access

**Staging:**
```bash
ssh timothy@vmi2578493
cd /var/www/eirs-projects/rdmapi.blouzatech.ng
```

**Production:**
```bash
ssh -i ~/.ssh/do_eirs_prod root@137.184.192.64
cd /var/www/rdmapi.eirs.gov.ng
```

---

## File Structure

```
rdmapi.eirs.gov.ng/
├── .dockerignore          # Files excluded from Docker build
├── .env                   # Environment variables (DO NOT COMMIT)
├── .env.example           # Environment template
├── Dockerfile             # Docker build instructions
├── docker-compose.yml     # Docker Compose configuration
├── RspApi.csproj          # .NET project file
├── Program.cs             # Application entry point
├── appsettings.json       # App configuration
├── build-docker.sh        # Build script
├── deploy-to-linux.sh     # Deployment script
├── run-local.sh           # Local development script
├── test-db-connection.sh  # Database connectivity test
├── DEPLOYMENT.md          # This documentation
└── data/
    └── logs/              # Application logs (volume mount)
```

---

## Contact & Support

- **Git Repository:** https://gitlab.com/blouza-tech/rdm-rsp-api.git
- **Staging URL:** https://rdmapi.blouzatech.ng
- **Production URL:** https://rdmapi.eirs.gov.ng

---

*Last Updated: January 2026*
