#!/bin/bash

# 启动后端服务脚本
# 作者: 张翔
# 日期: 2026-04-15

set -e

echo "=========================================="
echo "启动后端服务 (网关 + 应用)"
echo "=========================================="

# 检查Java是否安装
if ! command -v java &> /dev/null; then
    echo "错误: Java 未安装，请安装JDK 21或更高版本"
    exit 1
fi

# 检查Maven是否安装
if ! command -v mvn &> /dev/null; then
    echo "错误: Maven 未安装，请安装Maven 3.8+"
    exit 1
fi

# 进入后端项目目录
cd "$(dirname "$0")/../novalon-manage-api"

echo "1. 清理并编译项目..."
mvn clean compile -q

echo "2. 启动网关和应用服务..."
echo "   网关服务: http://localhost:8080"
echo "   应用服务: http://localhost:8084"
echo "   API文档: http://localhost:8084/swagger-ui.html"
echo "   健康检查: http://localhost:8084/actuator/health"
echo ""
echo "正在启动服务，请等待..."

# 使用Maven同时启动网关和应用
mvn spring-boot:run -pl manage-gateway,manage-app -am \
  -Dspring-boot.run.profiles=local \
  -Dspring-boot.run.jvmArguments="-Xmx512m -Xms256m"