feat: add build_service and start_service functions
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
73d9b96998
commit
eb5097516c
@ -53,3 +53,52 @@ DB_USER="${DB_USER:-haihuizhu}"
|
|||||||
DB_PASSWORD="${DB_PASSWORD:-admin}"
|
DB_PASSWORD="${DB_PASSWORD:-admin}"
|
||||||
DB_NAME="${DB_NAME:-top-fans}"
|
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")
|
DB_ARGS=(-db-host="$DB_HOST" -db-port="$DB_PORT" -db-user="$DB_USER" -db-password="$DB_PASSWORD" -db-name="$DB_NAME")
|
||||||
|
|
||||||
|
# 启动一个服务
|
||||||
|
# 用法: start_service name binary port use_db
|
||||||
|
start_service() {
|
||||||
|
local name=$1
|
||||||
|
local binary=$2
|
||||||
|
local port=$3
|
||||||
|
local use_db=$4
|
||||||
|
|
||||||
|
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 2
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
# 构建一个服务(在服务目录下执行 go build)
|
||||||
|
# 用法: build_service name dir binary
|
||||||
|
build_service() {
|
||||||
|
local name=$1
|
||||||
|
local dir=$2
|
||||||
|
local binary=$3
|
||||||
|
|
||||||
|
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}"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
echo -e "${RED}❌ $name 编译失败${NC}"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user