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| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
print(f'β οΈ Database access issue: {e}')
|
| 50 |
-
" || echo "Database check completed with warnings"
|
| 51 |
}
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
echo
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# Check port availability
|
| 65 |
-
PORT=${PORT:-8000}
|
| 66 |
-
if ! check_port $PORT; then
|
| 67 |
-
echo "π Trying alternative port..."
|
| 68 |
-
PORT=$((PORT + 1))
|
| 69 |
-
check_port $PORT || PORT=7860 # HF Spaces default
|
| 70 |
-
fi
|
| 71 |
-
|
| 72 |
-
echo "π Using port: $PORT"
|
| 73 |
-
|
| 74 |
-
# Health check function
|
| 75 |
-
health_check() {
|
| 76 |
-
local max_attempts=30
|
| 77 |
-
local attempt=1
|
| 78 |
-
|
| 79 |
-
echo "π₯ Starting health check..."
|
| 80 |
-
|
| 81 |
-
while [ $attempt -le $max_attempts ]; do
|
| 82 |
-
if curl -fs http://localhost:$PORT/health >/dev/null 2>&1; then
|
| 83 |
-
echo "β
Application is healthy!"
|
| 84 |
-
return 0
|
| 85 |
fi
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
sleep 2
|
| 89 |
-
attempt=$((attempt + 1))
|
| 90 |
done
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
echo "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
if
|
| 105 |
-
|
| 106 |
-
python3 app.py
|
| 107 |
else
|
| 108 |
-
|
| 109 |
-
uvicorn app.main:app --host 0.0.0.0 --port $PORT --workers 1
|
| 110 |
fi
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
--port $PORT \
|
| 121 |
-
--workers 1 \
|
| 122 |
-
--access-log \
|
| 123 |
-
--log-level info &
|
| 124 |
-
|
| 125 |
-
# Store PID
|
| 126 |
-
APP_PID=$!
|
| 127 |
-
echo "π Application PID: $APP_PID"
|
| 128 |
-
|
| 129 |
-
# Wait a bit then check health
|
| 130 |
-
sleep 5
|
| 131 |
-
if health_check; then
|
| 132 |
-
echo "π Application started successfully!"
|
| 133 |
-
wait $APP_PID
|
| 134 |
-
else
|
| 135 |
-
echo "π₯ Application failed to start properly"
|
| 136 |
-
kill $APP_PID 2>/dev/null
|
| 137 |
-
exit 1
|
| 138 |
-
fi
|
| 139 |
else
|
| 140 |
-
|
| 141 |
-
python3 -c "
|
| 142 |
-
import uvicorn
|
| 143 |
-
uvicorn.run('app.main:app', host='0.0.0.0', port=$PORT, workers=1)
|
| 144 |
-
"
|
| 145 |
fi
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/bin/bash
|
| 2 |
|
| 3 |
+
# Legal Dashboard Startup Script
|
| 4 |
+
# This script helps setup and start the Legal Dashboard application
|
| 5 |
|
| 6 |
+
set -e
|
| 7 |
|
| 8 |
+
echo "ποΈ Legal Dashboard - Starting Setup..."
|
| 9 |
+
|
| 10 |
+
# Colors for output
|
| 11 |
+
RED='\033[0;31m'
|
| 12 |
+
GREEN='\033[0;32m'
|
| 13 |
+
YELLOW='\033[1;33m'
|
| 14 |
+
BLUE='\033[0;34m'
|
| 15 |
+
NC='\033[0m' # No Color
|
| 16 |
+
|
| 17 |
+
# Function to print colored output
|
| 18 |
+
print_status() {
|
| 19 |
+
echo -e "${GREEN}β
$1${NC}"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
print_warning() {
|
| 23 |
+
echo -e "${YELLOW}β οΈ $1${NC}"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
print_error() {
|
| 27 |
+
echo -e "${RED}β $1${NC}"
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
print_info() {
|
| 31 |
+
echo -e "${BLUE}βΉοΈ $1${NC}"
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
# Check if Docker is installed
|
| 35 |
+
check_docker() {
|
| 36 |
+
if ! command -v docker &> /dev/null; then
|
| 37 |
+
print_error "Docker is not installed. Please install Docker first."
|
| 38 |
+
exit 1
|
| 39 |
+
fi
|
| 40 |
+
|
| 41 |
+
if ! command -v docker-compose &> /dev/null && ! docker compose version &> /dev/null; then
|
| 42 |
+
print_error "Docker Compose is not installed. Please install Docker Compose first."
|
| 43 |
+
exit 1
|
| 44 |
+
fi
|
| 45 |
+
|
| 46 |
+
print_status "Docker is available"
|
| 47 |
+
}
|
| 48 |
|
| 49 |
# Create necessary directories
|
| 50 |
+
create_directories() {
|
| 51 |
+
print_info "Creating necessary directories..."
|
| 52 |
+
|
| 53 |
+
mkdir -p data
|
| 54 |
+
mkdir -p cache
|
| 55 |
+
mkdir -p logs
|
| 56 |
+
mkdir -p uploads
|
| 57 |
+
mkdir -p backups
|
| 58 |
+
mkdir -p logs/nginx
|
| 59 |
+
|
| 60 |
+
# Set permissions
|
| 61 |
+
chmod 755 data cache logs uploads backups
|
| 62 |
+
chmod 755 logs/nginx
|
| 63 |
+
|
| 64 |
+
print_status "Directories created and permissions set"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
# Check if frontend directory exists
|
| 68 |
+
check_frontend() {
|
| 69 |
+
if [ ! -d "frontend" ]; then
|
| 70 |
+
print_warning "Frontend directory not found. Creating basic structure..."
|
| 71 |
+
mkdir -p frontend
|
| 72 |
+
|
| 73 |
+
# Create basic index.html if it doesn't exist
|
| 74 |
+
if [ ! -f "frontend/index.html" ]; then
|
| 75 |
+
print_info "Creating basic index.html..."
|
| 76 |
+
cat > frontend/index.html << 'EOF'
|
| 77 |
+
<!DOCTYPE html>
|
| 78 |
+
<html>
|
| 79 |
+
<head>
|
| 80 |
+
<title>Legal Dashboard</title>
|
| 81 |
+
<meta charset="UTF-8">
|
| 82 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 83 |
+
</head>
|
| 84 |
+
<body>
|
| 85 |
+
<h1>Legal Dashboard is Starting...</h1>
|
| 86 |
+
<p>Please wait while the application loads.</p>
|
| 87 |
+
<script>
|
| 88 |
+
setTimeout(() => {
|
| 89 |
+
window.location.reload();
|
| 90 |
+
}, 5000);
|
| 91 |
+
</script>
|
| 92 |
+
</body>
|
| 93 |
+
</html>
|
| 94 |
+
EOF
|
| 95 |
+
fi
|
| 96 |
+
fi
|
| 97 |
+
|
| 98 |
+
print_status "Frontend directory checked"
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
# Setup environment file
|
| 102 |
+
setup_environment() {
|
| 103 |
+
if [ ! -f ".env" ]; then
|
| 104 |
+
print_info "Creating .env file..."
|
| 105 |
+
cat > .env << 'EOF'
|
| 106 |
+
# Legal Dashboard Environment Configuration
|
| 107 |
+
JWT_SECRET_KEY=your-super-secret-jwt-key-change-this-in-production-$(date +%s)
|
| 108 |
+
DATABASE_DIR=/app/data
|
| 109 |
+
DATABASE_PATH=/app/data/legal_dashboard.db
|
| 110 |
+
LOG_LEVEL=INFO
|
| 111 |
+
ENVIRONMENT=production
|
| 112 |
+
PYTHONPATH=/app
|
| 113 |
+
REDIS_URL=redis://redis:6379/0
|
| 114 |
+
EOF
|
| 115 |
+
print_status ".env file created"
|
| 116 |
else
|
| 117 |
+
print_status ".env file already exists"
|
|
|
|
| 118 |
fi
|
| 119 |
}
|
| 120 |
|
| 121 |
+
# Build and start services
|
| 122 |
+
start_services() {
|
| 123 |
+
print_info "Building and starting services..."
|
| 124 |
+
|
| 125 |
+
# Stop any existing containers
|
| 126 |
+
docker-compose down 2>/dev/null || docker compose down 2>/dev/null || true
|
| 127 |
+
|
| 128 |
+
# Build and start
|
| 129 |
+
if command -v docker-compose &> /dev/null; then
|
| 130 |
+
docker-compose up --build -d
|
| 131 |
+
else
|
| 132 |
+
docker compose up --build -d
|
| 133 |
+
fi
|
| 134 |
+
|
| 135 |
+
print_status "Services started"
|
|
|
|
|
|
|
| 136 |
}
|
| 137 |
|
| 138 |
+
# Wait for services to be ready
|
| 139 |
+
wait_for_services() {
|
| 140 |
+
print_info "Waiting for services to be ready..."
|
| 141 |
+
|
| 142 |
+
# Wait for Redis
|
| 143 |
+
echo -n "Waiting for Redis..."
|
| 144 |
+
for i in {1..30}; do
|
| 145 |
+
if docker exec legal_dashboard_redis redis-cli ping > /dev/null 2>&1; then
|
| 146 |
+
echo " Ready!"
|
| 147 |
+
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
fi
|
| 149 |
+
echo -n "."
|
| 150 |
+
sleep 1
|
|
|
|
|
|
|
| 151 |
done
|
| 152 |
|
| 153 |
+
# Wait for FastAPI
|
| 154 |
+
echo -n "Waiting for FastAPI..."
|
| 155 |
+
for i in {1..60}; do
|
| 156 |
+
if curl -fs http://localhost:8000/health > /dev/null 2>&1; then
|
| 157 |
+
echo " Ready!"
|
| 158 |
+
break
|
| 159 |
+
fi
|
| 160 |
+
echo -n "."
|
| 161 |
+
sleep 1
|
| 162 |
+
done
|
| 163 |
+
|
| 164 |
+
print_status "All services are ready"
|
| 165 |
}
|
| 166 |
|
| 167 |
+
# Show status
|
| 168 |
+
show_status() {
|
| 169 |
+
echo ""
|
| 170 |
+
echo "π Legal Dashboard is now running!"
|
| 171 |
+
echo ""
|
| 172 |
+
echo "π± Access URLs:"
|
| 173 |
+
echo " Main Dashboard: http://localhost"
|
| 174 |
+
echo " Direct Access: http://localhost:8000"
|
| 175 |
+
echo " API Documentation: http://localhost:8000/api/docs"
|
| 176 |
+
echo ""
|
| 177 |
+
echo "π§ Management Commands:"
|
| 178 |
+
echo " View logs: docker-compose logs -f legal-dashboard"
|
| 179 |
+
echo " Stop services: docker-compose down"
|
| 180 |
+
echo " Restart: docker-compose restart"
|
| 181 |
+
echo ""
|
| 182 |
+
echo "π Service Status:"
|
| 183 |
|
| 184 |
+
# Check service status
|
| 185 |
+
if docker ps --format "table {{.Names}}\t{{.Status}}" | grep legal_dashboard_app > /dev/null; then
|
| 186 |
+
print_status "Legal Dashboard App: Running"
|
|
|
|
| 187 |
else
|
| 188 |
+
print_error "Legal Dashboard App: Not Running"
|
|
|
|
| 189 |
fi
|
| 190 |
+
|
| 191 |
+
if docker ps --format "table {{.Names}}\t{{.Status}}" | grep legal_dashboard_redis > /dev/null; then
|
| 192 |
+
print_status "Redis: Running"
|
| 193 |
+
else
|
| 194 |
+
print_error "Redis: Not Running"
|
| 195 |
+
fi
|
| 196 |
+
|
| 197 |
+
if docker ps --format "table {{.Names}}\t{{.Status}}" | grep legal_dashboard_nginx > /dev/null; then
|
| 198 |
+
print_status "Nginx: Running"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
else
|
| 200 |
+
print_warning "Nginx: Not Running (This is optional)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
fi
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
# Main execution
|
| 205 |
+
main() {
|
| 206 |
+
echo "π Starting Legal Dashboard Setup..."
|
| 207 |
+
echo "=================================="
|
| 208 |
+
|
| 209 |
+
check_docker
|
| 210 |
+
create_directories
|
| 211 |
+
check_frontend
|
| 212 |
+
setup_environment
|
| 213 |
+
start_services
|
| 214 |
+
wait_for_services
|
| 215 |
+
show_status
|
| 216 |
+
|
| 217 |
+
echo ""
|
| 218 |
+
echo "β¨ Setup completed successfully!"
|
| 219 |
+
echo "You can now access the Legal Dashboard at http://localhost"
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
# Handle script arguments
|
| 223 |
+
case "${1:-}" in
|
| 224 |
+
"stop")
|
| 225 |
+
print_info "Stopping Legal Dashboard..."
|
| 226 |
+
docker-compose down 2>/dev/null || docker compose down 2>/dev/null
|
| 227 |
+
print_status "Legal Dashboard stopped"
|
| 228 |
+
;;
|
| 229 |
+
"restart")
|
| 230 |
+
print_info "Restarting Legal Dashboard..."
|
| 231 |
+
docker-compose restart 2>/dev/null || docker compose restart 2>/dev/null
|
| 232 |
+
print_status "Legal Dashboard restarted"
|
| 233 |
+
;;
|
| 234 |
+
"logs")
|
| 235 |
+
print_info "Showing logs..."
|
| 236 |
+
docker-compose logs -f legal-dashboard 2>/dev/null || docker compose logs -f legal-dashboard 2>/dev/null
|
| 237 |
+
;;
|
| 238 |
+
"status")
|
| 239 |
+
show_status
|
| 240 |
+
;;
|
| 241 |
+
"clean")
|
| 242 |
+
print_warning "Cleaning up all containers and data..."
|
| 243 |
+
read -p "Are you sure? This will delete all data (y/N): " -n 1 -r
|
| 244 |
+
echo
|
| 245 |
+
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
| 246 |
+
docker-compose down -v 2>/dev/null || docker compose down -v 2>/dev/null
|
| 247 |
+
docker system prune -f
|
| 248 |
+
rm -rf data cache logs uploads backups
|
| 249 |
+
print_status "Cleanup completed"
|
| 250 |
+
fi
|
| 251 |
+
;;
|
| 252 |
+
"")
|
| 253 |
+
main
|
| 254 |
+
;;
|
| 255 |
+
*)
|
| 256 |
+
echo "Usage: $0 [stop|restart|logs|status|clean]"
|
| 257 |
+
echo ""
|
| 258 |
+
echo "Commands:"
|
| 259 |
+
echo " (no args) - Start the Legal Dashboard"
|
| 260 |
+
echo " stop - Stop all services"
|
| 261 |
+
echo " restart - Restart all services"
|
| 262 |
+
echo " logs - Show application logs"
|
| 263 |
+
echo " status - Show service status"
|
| 264 |
+
echo " clean - Clean up all containers and data (DESTRUCTIVE)"
|
| 265 |
+
exit 1
|
| 266 |
+
;;
|
| 267 |
+
esac
|