Really-amin commited on
Commit
8af52bf
Β·
verified Β·
1 Parent(s): 272dc7c

Update start.sh

Browse files
Files changed (1) hide show
  1. start.sh +245 -124
start.sh CHANGED
@@ -1,146 +1,267 @@
1
  #!/bin/bash
2
 
3
- # Start script for Legal Dashboard
4
- # Suitable for Hugging Face Spaces and Docker environments
5
 
6
- echo "πŸš€ Starting Legal Dashboard..."
7
 
8
- # Set default environment variables
9
- export PYTHONPATH=/app
10
- export PYTHONUNBUFFERED=1
11
- export DATABASE_DIR=${DATABASE_DIR:-/app/data}
12
- export LOG_LEVEL=${LOG_LEVEL:-INFO}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  # Create necessary directories
15
- echo "πŸ“ Creating directories..."
16
- mkdir -p /app/data /app/cache /app/logs /app/uploads /app/backups /tmp/app_fallback
17
-
18
- # Set permissions if possible (ignore errors)
19
- chmod -R 755 /app/data /app/cache /app/logs /app/uploads /app/backups 2>/dev/null || true
20
- chmod -R 777 /tmp/app_fallback 2>/dev/null || true
21
-
22
- # Function to check if port is available
23
- check_port() {
24
- local port=${1:-8000}
25
- if lsof -Pi :$port -sTCP:LISTEN -t >/dev/null 2>&1; then
26
- echo "⚠️ Port $port is already in use"
27
- return 1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  else
29
- echo "βœ… Port $port is available"
30
- return 0
31
  fi
32
  }
33
 
34
- # Function to wait for dependencies
35
- wait_for_deps() {
36
- echo "⏳ Checking dependencies..."
37
-
38
- # Check if we can create database
39
- python3 -c "
40
- import sqlite3
41
- import os
42
- db_path = os.environ.get('DATABASE_DIR', '/app/data') + '/test.db'
43
- try:
44
- conn = sqlite3.connect(db_path)
45
- conn.close()
46
- os.remove(db_path)
47
- print('βœ… Database access OK')
48
- except Exception as e:
49
- print(f'⚠️ Database access issue: {e}')
50
- " || echo "Database check completed with warnings"
51
  }
52
 
53
- # Check environment
54
- echo "πŸ” Environment check..."
55
- echo " - Python: $(python3 --version)"
56
- echo " - Working directory: $(pwd)"
57
- echo " - Database dir: $DATABASE_DIR"
58
- echo " - User: $(whoami)"
59
- echo " - UID: $(id -u)"
60
-
61
- # Wait for dependencies
62
- wait_for_deps
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
- echo "⏳ Health check attempt $attempt/$max_attempts..."
88
- sleep 2
89
- attempt=$((attempt + 1))
90
  done
91
 
92
- echo "❌ Health check failed after $max_attempts attempts"
93
- return 1
 
 
 
 
 
 
 
 
 
 
94
  }
95
 
96
- # Start application
97
- echo "🎯 Starting application on port $PORT..."
98
-
99
- # For Hugging Face Spaces
100
- if [ "$SPACE_ID" != "" ]; then
101
- echo "πŸ€— Running in Hugging Face Spaces environment"
 
 
 
 
 
 
 
 
 
 
102
 
103
- # Use gradio or fastapi depending on setup
104
- if [ -f "app.py" ]; then
105
- echo "πŸ“± Starting with Gradio interface..."
106
- python3 app.py
107
  else
108
- echo "πŸš€ Starting FastAPI server..."
109
- uvicorn app.main:app --host 0.0.0.0 --port $PORT --workers 1
110
  fi
111
- else
112
- # Standard Docker/local environment
113
- echo "🐳 Running in standard environment"
114
-
115
- # Start with uvicorn
116
- if command -v uvicorn >/dev/null 2>&1; then
117
- echo "πŸš€ Starting with uvicorn..."
118
- uvicorn app.main:app \
119
- --host 0.0.0.0 \
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
- echo "❌ uvicorn not found, trying with python..."
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
- fi
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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