#!/bin/bash # Database Render Application - Development Script # This script provides development utilities for the application set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' # No Color # Configuration SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" PROJECT_DIR="$(dirname "$SCRIPT_DIR")" BINARY_NAME="database-render" CONFIG_FILE="$PROJECT_DIR/config/config.yaml" # Functions 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" } # Check if service is running check_service() { local port=${1:-8080} if curl -sf http://localhost:$port/health >/dev/null; then return 0 else return 1 fi } # Wait for service to be ready wait_for_service() { local port=${1:-8080} local max_attempts=30 local attempt=1 log_info "Waiting for service to be ready on port $port..." while [ $attempt -le $max_attempts ]; do if check_service $port; then log_success "Service is ready!" return 0 fi log_info "Attempt $attempt/$max_attempts - waiting..." sleep 2 ((attempt++)) done log_error "Service failed to start within $max_attempts attempts" return 1 } # Initialize SQLite database init_sqlite() { local db_file="$PROJECT_DIR/data/app.db" if [ ! -f "$db_file" ]; then log_info "Creating SQLite database at $db_file..." mkdir -p "$(dirname "$db_file")" sqlite3 "$db_file" < "$PROJECT_DIR/config/schema.example.sql" log_success "SQLite database initialized" else log_info "SQLite database already exists at $db_file" fi } # Generate sample data generate_data() { local db_file="$PROJECT_DIR/data/app.db" if [ -f "$db_file" ]; then log_info "Generating sample data..." sqlite3 "$db_file" </dev/null 2>&1; then log_info "Using air for hot reload..." cd "$PROJECT_DIR" air else log_warning "air not found. Installing..." go install github.com/cosmtrek/air@latest if command -v air >/dev/null 2>&1; then cd "$PROJECT_DIR" air else log_error "Failed to install air" exit 1 fi fi } # Database utilities reset_db() { local db_file="$PROJECT_DIR/data/app.db" if [ -f "$db_file" ]; then log_info "Resetting database..." rm -f "$db_file" init_sqlite generate_data log_success "Database reset completed" else log_info "Database not found, creating new..." init_sqlite generate_data fi } # Health check health_check() { local port=${1:-8080} if check_service $port; then log_success "Service is healthy on port $port" curl -s http://localhost:$port/health | jq . 2>/dev/null || curl -s http://localhost:$port/health else log_error "Service is not responding on port $port" return 1 fi } # Show logs tail_logs() { local log_file="$PROJECT_DIR/logs/app.log" if [ -f "$log_file" ]; then log_info "Tailing application logs..." tail -f "$log_file" else log_info "Log file not found, checking stdout..." log_info "Application is logging to stdout/stderr" fi } # Open browser open_browser() { local port=${1:-8080} local url="http://localhost:$port" log_info "Opening browser at $url..." case "$(uname -s)" in Darwin*) open "$url" ;; Linux*) if command -v xdg-open >/dev/null 2>&1; then xdg-open "$url" elif command -v gnome-open >/dev/null 2>&1; then gnome-open "$url" else log_info "Please open your browser and navigate to: $url" fi ;; CYGWIN*|MINGW*|MSYS*) start "$url" ;; *) log_info "Please open your browser and navigate to: $url" ;; esac } # Show help show_help() { echo "Database Render Application - Development Script" echo "" echo "Usage: $0 [command] [options]" echo "" echo "Commands:" echo " start - Start development server" echo " watch - Start with hot reload (using air)" echo " init-db - Initialize SQLite database" echo " reset-db - Reset database with sample data" echo " health - Check service health" echo " logs - Tail application logs" echo " open - Open browser to application" echo " help - Show this help message" echo "" echo "Options:" echo " --port PORT - Specify port (default: 8080)" echo "" echo "Examples:" echo " $0 start" echo " $0 watch" echo " $0 reset-db" echo " $0 health --port 8080" echo " $0 open --port 8080" } # Parse command line arguments parse_args() { local command="$1" local port=8080 while [[ $# -gt 0 ]]; do case $1 in --port) port="$2" shift 2 ;; *) shift ;; esac done case "$command" in "start") start_dev ;; "watch") start_hot_reload ;; "init-db") init_sqlite generate_data ;; "reset-db") reset_db ;; "health") health_check $port ;; "logs") tail_logs ;; "open") open_browser $port ;; "help"|*) show_help ;; esac } # Main execution main() { cd "$PROJECT_DIR" parse_args "$@" } # Run main function main "$@"