360 lines
8.6 KiB
Bash
Executable File
360 lines
8.6 KiB
Bash
Executable File
#!/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" <<EOF
|
|
-- Insert sample data if tables are empty
|
|
INSERT OR IGNORE INTO users (username, email, full_name, role) VALUES
|
|
('admin', 'admin@example.com', 'System Administrator', 'admin'),
|
|
('developer', 'dev@example.com', 'Developer User', 'user'),
|
|
('editor', 'editor@example.com', 'Content Editor', 'user');
|
|
|
|
INSERT OR IGNORE INTO posts (title, content, category, tags, author_id, status) VALUES
|
|
('Welcome to Database Render', '# Welcome to Database Render
|
|
|
|
This is your first post created by the development script!
|
|
|
|
## Features
|
|
|
|
- Easy database visualization
|
|
- Modern card-based UI
|
|
- Responsive design
|
|
- Multiple database support
|
|
|
|
## Getting Started
|
|
|
|
1. Configure your database connection in config/config.yaml
|
|
2. Run the application
|
|
3. Start exploring your data!', 'Technology', 'welcome,getting-started', 1, 'published'),
|
|
|
|
('Development Tips', '# Development Tips
|
|
|
|
Here are some useful development tips:
|
|
|
|
## Hot Reload
|
|
|
|
Use air for hot reloading during development:
|
|
```bash
|
|
make watch
|
|
```
|
|
|
|
## Database Setup
|
|
|
|
The development script can initialize your SQLite database automatically.
|
|
|
|
## Configuration
|
|
|
|
Edit config/config.yaml to customize your setup.', 'Programming', 'development,tips', 2, 'published');
|
|
EOF
|
|
log_success "Sample data generated"
|
|
else
|
|
log_error "Database file not found: $db_file"
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
# Start development server
|
|
start_dev() {
|
|
log_info "Starting development server..."
|
|
|
|
# Ensure config exists
|
|
if [ ! -f "$CONFIG_FILE" ]; then
|
|
log_info "Creating development configuration..."
|
|
cp "$PROJECT_DIR/config/config.example.yaml" "$CONFIG_FILE"
|
|
|
|
# Update config for development
|
|
sed -i.bak 's/path: "data\/app.db"/path: "data\/app.db"/' "$CONFIG_FILE"
|
|
sed -i.bak 's/debug: false/debug: true/' "$CONFIG_FILE"
|
|
sed -i.bak 's/level: "info"/level: "debug"/' "$CONFIG_FILE"
|
|
rm -f "$CONFIG_FILE.bak"
|
|
|
|
log_success "Development configuration created"
|
|
fi
|
|
|
|
# Initialize database
|
|
init_sqlite
|
|
|
|
# Generate sample data
|
|
generate_data
|
|
|
|
# Build and run
|
|
cd "$PROJECT_DIR"
|
|
if [ -f "Makefile" ]; then
|
|
log_info "Building with Makefile..."
|
|
make build
|
|
|
|
if [ -f "$PROJECT_DIR/bin/$BINARY_NAME" ]; then
|
|
log_success "Starting application..."
|
|
CONFIG_FILE="$CONFIG_FILE" "$PROJECT_DIR/bin/$BINARY_NAME"
|
|
else
|
|
log_error "Binary not found: $PROJECT_DIR/bin/$BINARY_NAME"
|
|
exit 1
|
|
fi
|
|
else
|
|
log_error "Makefile not found"
|
|
exit 1
|
|
fi
|
|
}
|
|
|
|
# Start with hot reload
|
|
start_hot_reload() {
|
|
log_info "Starting with hot reload..."
|
|
|
|
# Check if air is installed
|
|
if command -v air >/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 "$@" |