#\!/bin/bash
set -e

echo "Building pg-api..."
cargo build --release

echo "Creating systemd service..."
cat > /etc/systemd/system/pg-api.service << 'SERVICE'
[Unit]
Description=PostgreSQL API Service
After=network.target postgresql.service

[Service]
Type=simple
User=root
WorkingDirectory=/opt/pg-api
Environment="RUST_LOG=pg_api=info"
ExecStart=/opt/pg-api/target/release/pg-api
Restart=always
RestartSec=10

# Security
NoNewPrivileges=true
PrivateTmp=true

# Resource limits
LimitNOFILE=65536
MemoryMax=1G
CPUQuota=200%

# Logging
StandardOutput=journal
StandardError=journal
SyslogIdentifier=pg-api

[Install]
WantedBy=multi-user.target
SERVICE

echo "Reloading systemd..."
systemctl daemon-reload

echo "Build complete\!"
echo "To start: systemctl start pg-api"
echo "To enable: systemctl enable pg-api"
