refactor: simplify deployment - unified env and startup script

- Remove Docker deployment files (1panel doesn't use Docker)
- Unify .env files: .env.example -> .env (single config file)
- Merge start.sh and start-prod.sh into unified start.sh with --prod flag
- Update setup_dev_env.sh to use .env instead of .env.local
- Add DEPLOY.md with 1panel deployment guide

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
egg
2025-12-14 15:16:26 +08:00
parent e255039419
commit f46402f6c9
13 changed files with 375 additions and 841 deletions

122
start.sh
View File

@@ -1,9 +1,10 @@
#!/bin/bash
# Tool_OCR - Unified Development Server Startup Script
# Tool_OCR - Unified Server Startup Script
# Usage:
# ./start.sh Start all services (backend + frontend)
# ./start.sh backend Start only backend
# ./start.sh frontend Start only frontend
# ./start.sh --prod Start in production mode (no hot-reload)
# ./start.sh --stop Stop all services
# ./start.sh --status Show service status
# ./start.sh --help Show help
@@ -27,6 +28,10 @@ BACKEND_PORT=${BACKEND_PORT:-8000}
FRONTEND_HOST=${FRONTEND_HOST:-0.0.0.0}
FRONTEND_PORT=${FRONTEND_PORT:-5173}
# Production settings
PROD_MODE=false
UVICORN_WORKERS=${UVICORN_WORKERS:-4}
# Create PID directory
mkdir -p "$PID_DIR"
@@ -34,15 +39,19 @@ mkdir -p "$PID_DIR"
print_header() {
echo ""
echo -e "${BLUE}================================${NC}"
echo -e "${BLUE} Tool_OCR Development Server${NC}"
if [ "$PROD_MODE" = true ]; then
echo -e "${BLUE} Tool_OCR Server (Production)${NC}"
else
echo -e "${BLUE} Tool_OCR Server (Development)${NC}"
fi
echo -e "${BLUE}================================${NC}"
echo ""
}
show_help() {
echo "Tool_OCR - Development Server Manager"
echo "Tool_OCR - Server Manager"
echo ""
echo "Usage: ./start.sh [command]"
echo "Usage: ./start.sh [options] [command]"
echo ""
echo "Commands:"
echo " (none) Start all services (backend + frontend)"
@@ -52,10 +61,19 @@ show_help() {
echo " --status Show status of services"
echo " --help Show this help message"
echo ""
echo "Options:"
echo " --prod Run in production mode (no hot-reload, multiple workers)"
echo ""
echo "Environment Variables:"
echo " BACKEND_PORT Backend port (default: 8000)"
echo " FRONTEND_PORT Frontend port (default: 5173)"
echo " UVICORN_WORKERS Number of workers in prod mode (default: 4)"
echo ""
echo "Examples:"
echo " ./start.sh # Start everything"
echo " ./start.sh backend # Start only backend"
echo " ./start.sh --stop # Stop all services"
echo " ./start.sh # Start in dev mode"
echo " ./start.sh --prod # Start in production mode"
echo " BACKEND_PORT=8088 ./start.sh # Use custom port"
echo " ./start.sh --stop # Stop all services"
echo ""
}
@@ -76,10 +94,10 @@ check_requirements() {
missing=1
fi
# Check .env.local
if [ ! -f "$SCRIPT_DIR/.env.local" ]; then
echo -e "${RED}Error: .env.local not found${NC}"
echo "Please copy .env.example to .env.local and configure"
# Check .env
if [ ! -f "$SCRIPT_DIR/.env" ]; then
echo -e "${RED}Error: .env not found${NC}"
echo "Please copy .env.example to .env and configure"
missing=1
fi
@@ -87,12 +105,12 @@ check_requirements() {
}
load_env() {
# Load environment variables from root .env.local (if present).
# Load environment variables from root .env (if present).
# This keeps backend/frontend config in sync without hardcoding ports/URLs in scripts.
if [ -f "$SCRIPT_DIR/.env.local" ]; then
if [ -f "$SCRIPT_DIR/.env" ]; then
set -a
# shellcheck disable=SC1090
source "$SCRIPT_DIR/.env.local"
source "$SCRIPT_DIR/.env"
set +a
fi
}
@@ -124,7 +142,11 @@ start_backend() {
return 0
fi
echo -e "${GREEN}Starting backend server...${NC}"
if [ "$PROD_MODE" = true ]; then
echo -e "${GREEN}Starting backend server (production mode)...${NC}"
else
echo -e "${GREEN}Starting backend server (development mode)...${NC}"
fi
# Activate virtual environment
source "$SCRIPT_DIR/venv/bin/activate"
@@ -141,8 +163,18 @@ start_backend() {
mkdir -p models/paddleocr
mkdir -p logs
# Start uvicorn
nohup uvicorn app.main:app --reload --host "$BACKEND_HOST" --port "$BACKEND_PORT" > "$SCRIPT_DIR/.pid/backend.log" 2>&1 &
# Start uvicorn (different modes)
if [ "$PROD_MODE" = true ]; then
nohup uvicorn app.main:app \
--host "$BACKEND_HOST" \
--port "$BACKEND_PORT" \
--workers "$UVICORN_WORKERS" \
--access-log \
--log-level info \
> "$SCRIPT_DIR/.pid/backend.log" 2>&1 &
else
nohup uvicorn app.main:app --reload --host "$BACKEND_HOST" --port "$BACKEND_PORT" > "$SCRIPT_DIR/.pid/backend.log" 2>&1 &
fi
local pid=$!
echo $pid > "$BACKEND_PID_FILE"
@@ -151,7 +183,11 @@ start_backend() {
# Wait a moment and verify
sleep 2
if is_running "$BACKEND_PID_FILE"; then
echo -e "${GREEN}Backend started (PID: $pid)${NC}"
if [ "$PROD_MODE" = true ]; then
echo -e "${GREEN}Backend started (PID: $pid, Workers: $UVICORN_WORKERS)${NC}"
else
echo -e "${GREEN}Backend started (PID: $pid)${NC}"
fi
echo -e " API Docs: http://localhost:$BACKEND_PORT/docs"
echo -e " Health: http://localhost:$BACKEND_PORT/health"
else
@@ -166,7 +202,11 @@ start_frontend() {
return 0
fi
echo -e "${GREEN}Starting frontend server...${NC}"
if [ "$PROD_MODE" = true ]; then
echo -e "${GREEN}Starting frontend server (production mode)...${NC}"
else
echo -e "${GREEN}Starting frontend server (development mode)...${NC}"
fi
# Load environment variables so Vite config can use FRONTEND_PORT/FRONTEND_HOST/etc.
load_env
@@ -177,8 +217,19 @@ start_frontend() {
cd "$SCRIPT_DIR/frontend"
# Start vite in background
nohup npm run dev > "$SCRIPT_DIR/.pid/frontend.log" 2>&1 &
# Start frontend (different modes)
if [ "$PROD_MODE" = true ]; then
# Build if needed
if [ ! -d "dist" ] || [ "$(find src -newer dist -type f 2>/dev/null | head -1)" ]; then
echo -e "${YELLOW}Building frontend...${NC}"
npm run build
fi
# Start production preview server
nohup npm run preview -- --host "$FRONTEND_HOST" --port "$FRONTEND_PORT" > "$SCRIPT_DIR/.pid/frontend.log" 2>&1 &
else
# Start vite dev server
nohup npm run dev > "$SCRIPT_DIR/.pid/frontend.log" 2>&1 &
fi
local pid=$!
echo $pid > "$FRONTEND_PID_FILE"
@@ -294,8 +345,31 @@ show_status() {
echo ""
}
# Parse arguments
COMMAND=""
while [[ $# -gt 0 ]]; do
case $1 in
--prod)
PROD_MODE=true
shift
;;
--help|-h|--stop|--status|backend|frontend|all)
COMMAND=$1
shift
;;
*)
echo -e "${RED}Unknown option: $1${NC}"
show_help
exit 1
;;
esac
done
# Default command
COMMAND=${COMMAND:-all}
# Main
case "${1:-all}" in
case "$COMMAND" in
--help|-h)
show_help
;;
@@ -321,7 +395,7 @@ case "${1:-all}" in
echo -e "${YELLOW}Press Ctrl+C to stop (or use ./start.sh --stop)${NC}"
echo -e "${YELLOW}Logs: tail -f .pid/frontend.log${NC}"
;;
all|"")
all)
print_header
check_requirements || exit 1
start_backend
@@ -343,7 +417,7 @@ case "${1:-all}" in
echo " Frontend: tail -f .pid/frontend.log"
;;
*)
echo -e "${RED}Unknown command: $1${NC}"
echo -e "${RED}Unknown command: $COMMAND${NC}"
show_help
exit 1
;;