topfans/backend/start.sh
2026-04-07 22:29:48 +08:00

152 lines
5.5 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# TopFans Backend 一键启动脚本
# 自动编译并启动所有微服务和网关,支持指定数据库连接
#
# 使用方式:
# 1. 默认数据库localhost/top-fans ./start.sh
# 2. 通过环境变量指定数据库(推荐): DB_HOST=192.168.1.1 DB_USER=myuser DB_PASSWORD=secret DB_NAME=mydb ./start.sh
# 3. 先导出再启动: export DB_HOST=192.168.1.1 DB_USER=myuser DB_PASSWORD=secret DB_NAME=mydb && ./start.sh
#
# 可选环境变量(未设置时使用默认值):
# DB_HOST 数据库主机(默认 localhost
# DB_PORT 数据库端口(默认 5432
# DB_USER 数据库用户(默认 haihuizhu
# DB_PASSWORD 数据库密码(默认 admin
# DB_NAME 数据库名称(默认 top-fans
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
# ---------- 加载 .env 文件 ----------
ENV_FILE="$SCRIPT_DIR/.env"
if [ -f "$ENV_FILE" ]; then
echo -e "${GREEN}📄 加载 .env 文件...${NC}"
set -a # 自动导出所有变量
source "$ENV_FILE"
set +a
fi
# ---------- 数据库配置(可从环境变量覆盖)----------
DB_HOST="${DB_HOST:-localhost}"
DB_PORT="${DB_PORT:-5432}"
DB_USER="${DB_USER:-haihuizhu}"
DB_PASSWORD="${DB_PASSWORD:-admin}"
DB_NAME="${DB_NAME:-top-fans}"
# 供需要连接数据库的服务使用的参数(使用数组以便密码中含空格时也能正确传递)
DB_ARGS=(-db-host="$DB_HOST" -db-port="$DB_PORT" -db-user="$DB_USER" -db-password="$DB_PASSWORD" -db-name="$DB_NAME")
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} TopFans Backend 一键启动${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}数据库: ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}${NC}"
echo ""
# 停止现有服务
echo -e "${YELLOW}🛑 停止现有服务...${NC}"
for service in gateway userService socialService assetService galleryService activityService; do
pkill -9 -f "$service" 2>/dev/null || true
done
sleep 1
# ---------- 编译服务 ----------
echo ""
echo -e "${YELLOW}🔨 编译服务...${NC}"
build_service() {
local name=$1
local dir=$2
local binary=$3
echo -e " 编译 $name..."
if [ ! -d "$SCRIPT_DIR/$dir" ]; then
echo -e " ${RED}$dir 目录不存在${NC}"
return 1
fi
cd "$SCRIPT_DIR/$dir"
if go build -o "$SCRIPT_DIR/$binary" . 2>&1; then
echo -e " ${GREEN}$name 编译成功${NC}"
else
echo -e " ${RED}$name 编译失败${NC}"
return 1
fi
}
build_service "gateway" "gateway" "gateway/gateway"
build_service "userService" "services/userService" "services/userService/userService"
build_service "assetService" "services/assetService" "services/assetService/assetService"
build_service "socialService" "services/socialService" "services/socialService/socialService"
build_service "galleryService" "services/galleryService" "services/galleryService/galleryService"
build_service "activityService" "services/activityService" "services/activityService/activityService"
cd "$SCRIPT_DIR"
# 服务配置: name, binary, port, wait, use_db (1=需要传 DB 参数, 0=不需要)
# Gateway 不连数据库,其余四个服务都需要 DB 参数
start_service() {
local name=$1
local binary=$2
local port=$3
local wait_time=$4
local use_db=$5
echo ""
echo -e "${GREEN}🚀 启动 $name...${NC}"
if [ "$use_db" = "1" ]; then
"$SCRIPT_DIR/$binary" -port=$port "${DB_ARGS[@]}" > "/tmp/${name}.log" 2>&1 &
else
"$SCRIPT_DIR/$binary" -port=$port > "/tmp/${name}.log" 2>&1 &
fi
local pid=$!
sleep $wait_time
if ps -p $pid > /dev/null 2>&1; then
echo -e "${GREEN}$name 已启动 (PID: $pid, 端口: $port)${NC}"
else
echo -e "${RED}$name 启动失败${NC}"
echo -e "${YELLOW}查看日志: tail -f /tmp/${name}.log${NC}"
fi
}
# 启动顺序UserService -> SocialService / AssetService / GalleryService / ActivityService -> Gateway
start_service "userService" "services/userService/userService" 20000 3 1
start_service "assetService" "services/assetService/assetService" 20003 3 1
start_service "socialService" "services/socialService/socialService" 20002 3 1
start_service "galleryService" "services/galleryService/galleryService" 20004 3 1
start_service "activityService" "services/activityService/activityService" 20005 3 1
start_service "gateway" "gateway/gateway" 8080 5 0
echo ""
echo -e "${GREEN}========================================${NC}"
echo -e "${GREEN} 所有服务启动完成!${NC}"
echo -e "${GREEN}========================================${NC}"
echo ""
echo -e "${YELLOW}数据库:${NC} ${DB_USER}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
echo ""
echo -e "${YELLOW}服务地址:${NC}"
echo " - Gateway: http://localhost:8080"
echo " - Swagger UI: http://localhost:8080/swagger/index.html"
echo " - User Service: tri://localhost:20000"
echo " - Social Service: tri://localhost:20002"
echo " - Asset Service: tri://localhost:20003"
echo " - Gallery Service: tri://localhost:20004"
echo " - Activity Service: tri://localhost:20005"
echo ""
echo -e "${YELLOW}常用命令:${NC}"
echo " 查看日志: tail -f /tmp/gateway.log 或 tail -f /tmp/<服务名>.log"
echo " 停止服务: ./stop.sh"
echo ""