177 lines
5.7 KiB
Bash
177 lines
5.7 KiB
Bash
#!/bin/bash
|
|
# ============================================
|
|
# Cashumints.space API - Installation Script
|
|
# ============================================
|
|
#
|
|
# Usage:
|
|
# chmod +x deploy/install.sh
|
|
# sudo ./deploy/install.sh
|
|
#
|
|
# This script:
|
|
# 1. Creates cashumints user
|
|
# 2. Installs the application to /opt/cashumints-api
|
|
# 3. Sets up systemd services
|
|
# 4. Configures basic Nginx
|
|
#
|
|
# ============================================
|
|
|
|
set -e
|
|
|
|
# Colors
|
|
RED='\033[0;31m'
|
|
GREEN='\033[0;32m'
|
|
YELLOW='\033[1;33m'
|
|
NC='\033[0m' # No Color
|
|
|
|
# Configuration
|
|
APP_USER="cashumints"
|
|
APP_DIR="/opt/cashumints-api"
|
|
CURRENT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
|
|
|
|
echo -e "${GREEN}╔═══════════════════════════════════════════════════╗${NC}"
|
|
echo -e "${GREEN}║ Cashumints.space API - Installation Script ║${NC}"
|
|
echo -e "${GREEN}╚═══════════════════════════════════════════════════╝${NC}"
|
|
echo ""
|
|
|
|
# Check if running as root
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo -e "${RED}Error: Please run as root (sudo)${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
# Check Node.js
|
|
if ! command -v node &> /dev/null; then
|
|
echo -e "${RED}Error: Node.js is not installed${NC}"
|
|
echo "Install with: curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - && sudo apt install -y nodejs"
|
|
exit 1
|
|
fi
|
|
|
|
NODE_VERSION=$(node -v | cut -d'v' -f2 | cut -d'.' -f1)
|
|
if [ "$NODE_VERSION" -lt 18 ]; then
|
|
echo -e "${RED}Error: Node.js 18+ required (found v${NODE_VERSION})${NC}"
|
|
exit 1
|
|
fi
|
|
|
|
echo -e "${GREEN}✓${NC} Node.js $(node -v) detected"
|
|
|
|
# Create user
|
|
echo -e "${YELLOW}Creating application user...${NC}"
|
|
if id "$APP_USER" &>/dev/null; then
|
|
echo -e "${GREEN}✓${NC} User $APP_USER already exists"
|
|
else
|
|
useradd -r -s /bin/false $APP_USER
|
|
echo -e "${GREEN}✓${NC} Created user $APP_USER"
|
|
fi
|
|
|
|
# Create application directory
|
|
echo -e "${YELLOW}Setting up application directory...${NC}"
|
|
if [ -d "$APP_DIR" ]; then
|
|
echo -e "${YELLOW}⚠${NC} Directory $APP_DIR exists, updating..."
|
|
else
|
|
mkdir -p $APP_DIR
|
|
fi
|
|
|
|
# Copy application files
|
|
cp -r "$CURRENT_DIR"/* $APP_DIR/
|
|
rm -rf $APP_DIR/node_modules 2>/dev/null || true
|
|
|
|
# Create data directory
|
|
mkdir -p $APP_DIR/data
|
|
chown -R $APP_USER:$APP_USER $APP_DIR/data
|
|
|
|
echo -e "${GREEN}✓${NC} Application files copied to $APP_DIR"
|
|
|
|
# Install dependencies
|
|
echo -e "${YELLOW}Installing dependencies...${NC}"
|
|
cd $APP_DIR
|
|
npm install --production --silent
|
|
echo -e "${GREEN}✓${NC} Dependencies installed"
|
|
|
|
# Setup configuration
|
|
if [ ! -f "$APP_DIR/.env" ]; then
|
|
echo -e "${YELLOW}Creating configuration...${NC}"
|
|
cp $APP_DIR/env.example $APP_DIR/.env
|
|
|
|
# Generate admin key
|
|
ADMIN_KEY=$(openssl rand -hex 32)
|
|
sed -i "s/^ADMIN_API_KEY=$/ADMIN_API_KEY=$ADMIN_KEY/" $APP_DIR/.env
|
|
|
|
echo -e "${GREEN}✓${NC} Configuration created"
|
|
echo -e "${YELLOW}⚠ IMPORTANT: Your admin API key is:${NC}"
|
|
echo -e "${GREEN}$ADMIN_KEY${NC}"
|
|
echo ""
|
|
echo "Save this key! You'll need it for admin operations."
|
|
echo ""
|
|
else
|
|
echo -e "${GREEN}✓${NC} Configuration already exists"
|
|
fi
|
|
|
|
# Set permissions
|
|
chown -R root:$APP_USER $APP_DIR
|
|
chmod -R 750 $APP_DIR
|
|
chmod 640 $APP_DIR/.env
|
|
|
|
# Initialize database
|
|
echo -e "${YELLOW}Initializing database...${NC}"
|
|
sudo -u $APP_USER node $APP_DIR/src/db/migrate.js
|
|
echo -e "${GREEN}✓${NC} Database initialized"
|
|
|
|
# Install systemd services
|
|
echo -e "${YELLOW}Installing systemd services...${NC}"
|
|
cp $APP_DIR/deploy/cashumints-api.service /etc/systemd/system/
|
|
cp $APP_DIR/deploy/cashumints-workers.service /etc/systemd/system/
|
|
systemctl daemon-reload
|
|
systemctl enable cashumints-api cashumints-workers
|
|
echo -e "${GREEN}✓${NC} Systemd services installed and enabled"
|
|
|
|
# Install nginx config if nginx is available
|
|
if command -v nginx &> /dev/null; then
|
|
echo -e "${YELLOW}Installing Nginx configuration...${NC}"
|
|
cp $APP_DIR/deploy/nginx.conf /etc/nginx/sites-available/cashumints-api
|
|
|
|
if [ ! -L /etc/nginx/sites-enabled/cashumints-api ]; then
|
|
ln -s /etc/nginx/sites-available/cashumints-api /etc/nginx/sites-enabled/
|
|
fi
|
|
|
|
echo -e "${GREEN}✓${NC} Nginx configuration installed"
|
|
echo -e "${YELLOW}⚠ Note: Update server_name and SSL paths in /etc/nginx/sites-available/cashumints-api${NC}"
|
|
else
|
|
echo -e "${YELLOW}⚠${NC} Nginx not found, skipping Nginx setup"
|
|
fi
|
|
|
|
# Summary
|
|
echo ""
|
|
echo -e "${GREEN}╔═══════════════════════════════════════════════════╗${NC}"
|
|
echo -e "${GREEN}║ Installation Complete! ║${NC}"
|
|
echo -e "${GREEN}╚═══════════════════════════════════════════════════╝${NC}"
|
|
echo ""
|
|
echo "Next steps:"
|
|
echo ""
|
|
echo "1. Review configuration:"
|
|
echo " sudo nano $APP_DIR/.env"
|
|
echo ""
|
|
echo "2. Start services:"
|
|
echo " sudo systemctl start cashumints-api"
|
|
echo " sudo systemctl start cashumints-workers"
|
|
echo ""
|
|
echo "3. Check status:"
|
|
echo " sudo systemctl status cashumints-api"
|
|
echo " sudo systemctl status cashumints-workers"
|
|
echo ""
|
|
echo "4. View logs:"
|
|
echo " sudo journalctl -u cashumints-api -f"
|
|
echo " sudo journalctl -u cashumints-workers -f"
|
|
echo ""
|
|
echo "5. Test API:"
|
|
echo " curl http://localhost:3000/v1/health"
|
|
echo ""
|
|
if command -v nginx &> /dev/null; then
|
|
echo "6. Setup SSL (Let's Encrypt):"
|
|
echo " sudo certbot --nginx -d api.cashumints.space"
|
|
echo " sudo systemctl reload nginx"
|
|
echo ""
|
|
fi
|
|
echo -e "${GREEN}Documentation: http://localhost:3000/docs${NC}"
|
|
echo ""
|
|
|