cosmo/backend/scripts/setup.sh

222 lines
5.2 KiB
Bash
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/bin/bash
# Cosmo 后端一键初始化脚本
set -e # 遇到错误立即退出
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 日志函数
log_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
log_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
log_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# 打印标题
print_header() {
echo "================================================================="
echo " Cosmo 后端初始化脚本"
echo "================================================================="
echo ""
}
# 检查 Python
check_python() {
log_info "检查 Python 环境..."
if ! command -v python3 &> /dev/null; then
log_error "未找到 Python 3请先安装 Python 3.9+"
exit 1
fi
PYTHON_VERSION=$(python3 --version | awk '{print $2}')
log_success "Python 版本: $PYTHON_VERSION"
}
# 检查 PostgreSQL
check_postgresql() {
log_info "检查 PostgreSQL..."
if ! command -v psql &> /dev/null; then
log_error "未找到 psql 命令,请先安装 PostgreSQL"
exit 1
fi
# 尝试连接 PostgreSQL
if psql -U postgres -c "SELECT version();" &> /dev/null; then
log_success "PostgreSQL 连接成功"
else
log_error "无法连接到 PostgreSQL请检查"
log_error " 1. PostgreSQL 是否正在运行"
log_error " 2. 账号密码是否为 postgres/postgres"
log_error " 3. 是否允许本地连接"
exit 1
fi
}
# 检查 Redis
check_redis() {
log_info "检查 Redis..."
if ! command -v redis-cli &> /dev/null; then
log_warning "未找到 redis-cli 命令"
log_warning "Redis 是可选的,但建议安装以获得更好的缓存性能"
return
fi
# 尝试连接 Redis
if redis-cli ping &> /dev/null; then
log_success "Redis 连接成功"
else
log_warning "无法连接到 Redis"
log_warning "应用会自动降级为仅使用内存缓存"
fi
}
# 检查依赖
check_dependencies() {
log_info "检查 Python 依赖包..."
cd "$(dirname "$0")/.." # 切换到 backend 目录
# 检查 requirements.txt 是否存在
if [ ! -f "requirements.txt" ]; then
log_error "未找到 requirements.txt 文件"
exit 1
fi
# 检查关键依赖是否已安装
if ! python3 -c "import fastapi" &> /dev/null; then
log_warning "依赖包未完全安装,正在安装..."
pip install -r requirements.txt
log_success "依赖包安装完成"
else
log_success "依赖包已安装"
fi
}
# 检查 .env 文件
check_env_file() {
log_info "检查配置文件..."
cd "$(dirname "$0")/.." # 确保在 backend 目录
if [ ! -f ".env" ]; then
log_warning ".env 文件不存在,从 .env.example 创建..."
if [ -f ".env.example" ]; then
cp .env.example .env
log_success ".env 文件创建成功"
else
log_error "未找到 .env.example 文件"
exit 1
fi
else
log_success ".env 文件已存在"
fi
}
# 创建数据库
create_database() {
log_info "创建数据库..."
cd "$(dirname "$0")/.." # 确保在 backend 目录
if python3 scripts/create_db.py; then
log_success "数据库创建完成"
else
log_error "数据库创建失败"
exit 1
fi
}
# 初始化数据库表
init_database() {
log_info "初始化数据库表结构..."
cd "$(dirname "$0")/.." # 确保在 backend 目录
if python3 scripts/init_db.py; then
log_success "数据库表结构初始化完成"
else
log_error "数据库表结构初始化失败"
exit 1
fi
}
# 创建上传目录
create_upload_dir() {
log_info "创建上传目录..."
cd "$(dirname "$0")/.." # 确保在 backend 目录
if [ ! -d "upload" ]; then
mkdir -p upload
log_success "上传目录创建成功: upload/"
else
log_success "上传目录已存在: upload/"
fi
}
# 打印完成信息
print_completion() {
echo ""
echo "================================================================="
echo -e "${GREEN} ✓ 初始化完成!${NC}"
echo "================================================================="
echo ""
echo "启动服务:"
echo " cd backend"
echo " python -m uvicorn app.main:app --reload --host 0.0.0.0 --port 8000"
echo ""
echo "或者:"
echo " python app/main.py"
echo ""
echo "访问:"
echo " - API 文档: http://localhost:8000/docs"
echo " - 健康检查: http://localhost:8000/health"
echo " - 根路径: http://localhost:8000/"
echo ""
echo "================================================================="
}
# 主函数
main() {
print_header
# 1. 检查环境
check_python
check_postgresql
check_redis
# 2. 安装依赖
check_dependencies
# 3. 配置文件
check_env_file
# 4. 数据库初始化
create_database
init_database
# 5. 创建必要目录
create_upload_dir
# 6. 完成
print_completion
}
# 执行主函数
main